Skip to content

feat: bump package.json version - #1

Open
iamgio wants to merge 2 commits into
mainfrom
feat/bump-package-json
Open

feat: bump package.json version#1
iamgio wants to merge 2 commits into
mainfrom
feat/bump-package-json

Conversation

@iamgio

@iamgio iamgio commented Sep 5, 2026

Copy link
Copy Markdown
Owner

Problem

version-file writes the bare version string over its target (printf '%s' "$version" > "$file"), so pointing it at package.json would replace the manifest with 1.2.3. npm-shaped projects had to bump the manifest themselves, outside the action — and if that step went missing, releases silently shipped with a stale version.

Change

bump-package-json (default false) sets the version field of the root package.json, and adds the manifest to the commit's file_pattern when commit: 'true'.

Only the version line changes. Rather than parse-and-re-serialize — which would reformat the whole manifest — the script detects the file's own indentation and replaces the version anchored to it. Run against a real 176-line package.json:

6c6
<   "version": "1.2.2",
---
>   "version": "1.3.0",

Byte-identical otherwise, and idempotent on re-run.

Anchoring to the top-level indent also keeps a nested "version" — inside an engines block, say — from being mistaken for the manifest's own.

Two decisions worth a look

  • keep-v-prefix deliberately does not apply. npm requires the version field to be plain semver, so v1.2.3 would be invalid. The v is always stripped. Documented on both inputs.
  • Unusual manifests fall back to re-serializing. If the anchored match fails — non-standard formatting, or no version field at all — the script parses, sets .version, and writes back with the detected indent. That path can reformat, and the script header says so. Failing a release over formatting seemed worse. Both paths are tested.

Node does the JSON work — guaranteed present wherever Actions run, and the script errors clearly if it isn't. Invalid JSON exits 2 rather than writing a corrupted manifest.

Tests

12 new bats tests; suite is 57/57. action.yml parses, and all six referenced scripts exist and are executable (mirroring the CI check).

Note for consumers

This adds an input, so it only takes effect once released and v1 moves. Passing bump-package-json to a v1 that predates it is a warning, not an error — the bump would silently not happen.

🤖 Generated with Claude Code

iamgio and others added 2 commits September 4, 2026 19:59
`version-file` overwrites its target with the bare version string, so it
cannot be pointed at a JSON manifest without destroying it. npm-shaped
projects were left to bump `package.json` themselves, outside the action.

`bump-package-json` sets the manifest's `version` field instead, and adds
it to the commit when `commit` is enabled.

Only the version line is rewritten. Rather than parse and re-serialize,
which would reformat the whole manifest, the script detects the file's own
indentation and replaces the version anchored to it. Anchoring also keeps a
nested `version` (in an `engines` block, say) from being mistaken for the
manifest's own. A manifest whose version cannot be located that way, or
which has no version field at all, falls back to a re-serialize that may
reformat it; the script header says so.

`keep-v-prefix` deliberately does not apply here: npm requires the version
field to be plain semver, so the leading `v` is always stripped.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KKtvw5nBDdx6GoGaQWZaGe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant