Skip to content

Run preversion and postversion scripts in changeset version - #2355

Open
dgesteves wants to merge 2 commits into
changesets:mainfrom
dgesteves:version-lifecycle-scripts
Open

dgesteves wants to merge 2 commits into
changesets:mainfrom
dgesteves:version-lifecycle-scripts

Conversation

@dgesteves

Copy link
Copy Markdown

Fixes #982

changeset version bumps versions but never runs the packages' preversion or postversion scripts, so setups that relied on them with npm version (like react-native-version updating the native app versions in postversion) have no hook to use. @Andarist said on the issue this should be supported with an option to opt out, so this adds that.

What changes:

  • For every package that gets a new version in the release plan (none releases are skipped), changeset version runs its preversion script before any file is changed, and its postversion script after the versions and changelogs are written, and after the commit when commit is enabled. That's the same order as npm version.
  • Scripts run through the project's package manager (npm run, pnpm run, yarn run, bun run, detected with package-manager-detector, which the CLI already uses) from the package's directory, with output inherited. npm_lifecycle_event and npm_package_version are set as usual. Packages run one at a time so their output doesn't interleave.
  • If a script fails, the command logs The preversion script of pkg-a failed. and exits with code 1. A failing preversion stops it before anything is written.
  • New config option runVersionScripts (default true) to opt out. Added to @changesets/types (also scripts on PackageJSON), the valibot schema and the regenerated schema.json.
  • The version script itself is not run, because it's often the script that calls changeset version ("version": "changeset version"), and running it would recurse.

Docs: new runVersionScripts section in site/guide/config.md and docs/config-file-options.md, plus a short "Lifecycle scripts" note under the version command in site/guide/cli.md. Changeset included, minor for @changesets/cli, @changesets/config and @changesets/types.

Two things you may want to weigh in on:

  • The default is on, following the "option to opt-out" comment. That means repos that already have a preversion like npm test will start running it during changeset version. If you'd rather ship it opt-in first, it's a one-line default change plus the docs.
  • In a single-package repo that runs changeset version from its own version script via npm run version, npm already runs preversion/postversion around it, so they'd run twice. runVersionScripts: false covers that; I didn't add special handling for it.

How I tested it:

  • New tests in packages/cli/src/commands/version/version.test.ts (tagged slow, as they spawn npm) using real scripts that write the package's current version to a file:
    • preversion sees 1.0.0 and postversion sees 1.1.0, and an unreleased package with the same scripts runs neither.
    • With commit enabled, postversion hasn't run yet when the commit is made.
    • runVersionScripts: false runs neither script.
    • A failing preversion throws ExitError, logs the message above, leaves package.json at 1.0.0, keeps the changeset file and doesn't run postversion.
  • packages/config/src/parse.test.ts: updated the default config snapshots, added a valid case (runVersionScripts: false) and an invalid one (non-boolean).
  • pnpm build, pnpm test run (39 files, 822 passed, 2 todo), pnpm types:check, pnpm lint and pnpm format all pass.
  • Ran the built CLI on a small npm workspace and a small pnpm workspace with both scripts. In both, preversion ran with npm_lifecycle_event=preversion and version 1.0.0, then postversion ran with 1.1.0 after the files were written.

Happy to take suggested changes, including on the option name.

`changeset version` now runs the `preversion` and `postversion` scripts
of the packages it versions, with the project's package manager, like
`npm version` does. `preversion` runs before any file is changed, so a
failing script stops the command before anything is written.
`postversion` runs after the files are written and committed.

The new `runVersionScripts` config option (default `true`) turns this off.

Fixes changesets#982
@changeset-bot

changeset-bot Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 54b628f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
@changesets/cli Minor
@changesets/config Minor
@changesets/types Minor
@changesets/apply-release-plan Patch
@changesets/assemble-release-plan Patch
@changesets/get-release-plan Patch
@changesets/changelog-git Patch
@changesets/changelog-github Patch
@changesets/get-dependents-graph Patch
@changesets/git Patch
@changesets/parse Patch
@changesets/pre Patch
@changesets/read Patch
@changesets/release-utils Patch
@changesets/should-skip-package Patch
@changesets/write Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify

netlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

👷 Deploy request for changesets pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 54b628f

@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.56%. Comparing base (9a035ec) to head (54b628f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2355      +/-   ##
==========================================
+ Coverage   87.47%   87.56%   +0.08%     
==========================================
  Files          78       78              
  Lines        2971     2991      +20     
  Branches      851      859       +8     
==========================================
+ Hits         2599     2619      +20     
  Misses        337      337              
  Partials       35       35              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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.

Changesets does not run pre and post scripts.

1 participant