Skip to content

prepare --dry-run writes automatic version bumps into the original checkout #953

Closed

Description

@sentry-junior

Summary

craft prepare --dry-run writes the automatic version bump into the checkout Craft was started from, not the temporary dry-run worktree. A dry run of a workspace that uses automatic bumping (no preReleaseCommand, npm target) leaves package.json modified in the user's checkout.

Seen with Craft 3.7.0 (055aa3c), but the code path predates 3.7.0.

Reproduction

In getsentry/toolkit, where packages/logger has no preReleaseCommand, no logger@ tags, and package.json at 1.0.0-dev.0:

craft prepare minor --workspace packages/logger --dry-run --no-push --no-git-checks --no-changelog --rev HEAD
git status   # packages/logger/package.json is now modified (1.0.0)
craft prepare patch --workspace packages/logger --dry-run --no-push --no-git-checks --no-changelog --rev HEAD
# computes 1.0.1 instead of 1.0.0, because the previous dry run changed the real package.json

Expected: the checkout is untouched, and each dry run computes the same version.

Likely cause (from reading the source, not confirmed with a debugger)

  1. prepare resolves config before isolation, so findConfigFile() (src/config.ts) caches the original checkout's .craft.yml path in _configPathCache.
  2. createDryRunIsolation() (src/utils/dryRun.ts) adds the worktree, calls enableWorktreeMode(), and process.chdir(worktreePath).
  3. runPreReleaseCommand({ rootDir: getReleaseUnitDir() }) (src/commands/prepare.ts): getReleaseUnitDir() → getConfigFileDir() → findConfigFile() returns the cached original path because it still exists. So rootDir is the original checkout's workspace directory, not the worktree.
  4. NpmTarget.bumpVersion(rootDir, …) runs npm version with cwd: rootDir. spawnProcess lets it run because isInWorktreeMode() is true. So the real package.json gets written.

A custom preReleaseCommand is likely unaffected, since it runs in process.cwd() (the worktree). Every automatic bumpVersion target that uses rootDir is probably affected, as is readProjectVersion (#947), though that one only reads. postReleaseBump (#950) only logs in dry-run mode.

Possible fix

Resolve the release unit directory against the worktree in dry-run mode. For example: clear _configPathCache when entering or leaving the worktree, or map getReleaseUnitDir() onto isolation.worktreePath, relative to the original config dir. Add a test where a dry-run automatic npm bump leaves the original package.json unchanged.

Found while moving getsentry/toolkit to Craft 3.7.0 (getsentry/toolkit#1731).

via @BYK.

--

View Session in Sentry

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions