Skip to content

Fix changeset version when run from a subdirectory - #2349

Open
MaxFreedomPollard wants to merge 1 commit into
changesets:mainfrom
MaxFreedomPollard:fix/version-from-subdirectory
Open

MaxFreedomPollard wants to merge 1 commit into
changesets:mainfrom
MaxFreedomPollard:fix/version-from-subdirectory

Conversation

@MaxFreedomPollard

Copy link
Copy Markdown
Contributor

Running changeset version from a package directory fails in v3, while changeset/add, status, pre and publish work from the same directory:

$ cd packages/pkg-a && changeset version
Error: There is no .changeset directory in this project

This was reported in #2248 (comment). Running the CLI from nested directories was added in 2.30.0 (#1806).

The cause is in packages/cli/src/commands/version/index.ts. Since the cac refactor (#2009), version() resolves the project itself with getPackages(cwd) (line 30), and checks for .changeset and reads the config from packages.rootDir (lines 31-32). But it still passes the raw cwd to readChangesets and readPreState (lines 68-69), and to getCommitFunctions (line 129). Before the refactor, run() passed the project root in as cwd, so these calls were correct. Now readChangesets looks for <cwd>/.changeset, which doesn't exist in a package directory. A relative commit module path (e.g. "commit": ["./commit.mjs", null]) is also resolved against <cwd>/.changeset. That lookup would only fail after the package.json and changelog files had already been written.

The fix passes packages.rootDir to those three calls, which is what status (readChangesets/readPreState) and add (getCommitFunctions) already do. The git calls keep cwd, since git works from any directory in the repository.

I added a test to packages/cli/src/commands/version/version.test.ts that runs version({ cwd }) from packages/pkg-a and checks that both packages are bumped. It mirrors the existing "should be able to add a changeset when called from subdirectory" test in add.test.ts. A changeset (patch for @changesets/cli) is included.

Testing: I could not run vitest, typecheck, lint or oxfmt, because I couldn't install the workspace dependencies in my environment (no access to the npm registry). Instead, I loaded version.test.ts with plain Node 22 type stripping and a small vitest-compatible shim. The workspace packages ran from their src/ files, with the real semver and picomatch that ship with Node's npm. The remaining third-party modules were replaced with small local stand-ins: @clack/prompts, human-id and @changesets/git are automocked as in the test file, and valibot, yaml, jsonc-parser, @manypkg/get-packages, import-meta-resolve and @changesets/format got minimal implementations. As a check, the valibot stand-in reproduces the default-config snapshots in packages/config/src/parse.test.ts.

  • Without the fix, the new test fails with Error: There is no .changeset directory in this project from readChangesets. With the fix, it passes.
  • I ran all 81 tests in the file before and after the change. The only difference is the new test going from fail to pass. The tests the shim can't evaluate (inline snapshots and fake timers) gave the same results both times.
  • I also ran two extra checks that are not part of this PR. With the fix, a project in pre mode versions to 1.1.0-next.0 when version runs from a subdirectory. A relative commit module in .changeset/ is also found. With only the readChangesets/readPreState change, the commit module lookup fails.

🤖 Generated with Claude Code

Since the CLI moved to cac, each command resolves the project root itself
with `getPackages(cwd)`. `version` does that too, but it still passes the
raw `cwd` to `readChangesets` and `readPreState`, so running it from a
package directory fails with "There is no .changeset directory in this
project", while `add`, `status`, `pre` and `publish` work from the same
directory. Before the refactor `cwd` was already the project root here, and
running from nested directories has been supported since 2.30.0.

`version` now reads the changesets, the pre state and the commit module from
`packages.rootDir`, like the other commands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Oct 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 04f332b

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

This PR includes changesets to release 1 package
Name Type
@changesets/cli 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 23, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for changesets ready!

Name Link
🔨 Latest commit 04f332b
🔍 Latest deploy log https://app.netlify.com/projects/changesets/deploys/6adab7b712adc93784813ed6
😎 Deploy Preview https://deploy-preview-2349--changesets.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.48%. Comparing base (e2ee472) to head (04f332b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2349   +/-   ##
=======================================
  Coverage   87.48%   87.48%           
=======================================
  Files          78       78           
  Lines        2973     2973           
  Branches      851      851           
=======================================
  Hits         2601     2601           
  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.

1 participant