Skip to content

fix(plugin-version): align suggestStrategy base with stableVersion-aware applyStrategy#7110

Open
KimHyeongRae0 wants to merge 1 commit intoyarnpkg:masterfrom
KimHyeongRae0:fix/7025-yarn-version-literal-semver-prerelease-source
Open

fix(plugin-version): align suggestStrategy base with stableVersion-aware applyStrategy#7110
KimHyeongRae0 wants to merge 1 commit intoyarnpkg:masterfrom
KimHyeongRae0:fix/7025-yarn-version-literal-semver-prerelease-source

Conversation

@KimHyeongRae0
Copy link
Copy Markdown

What's the problem this PR addresses?

Closes #7025.

When a workspace has a prerelease version shadowed by a stableVersion, running yarn version <semver> against a literal target bumps the wrong way. Exact repro from the issue:

```json
{
"name": "test-version",
"version": "1.3.1-alpha",
"stableVersion": "1.2.1"
}
```

```
$ yarn version 1.3.1
➤ YN0000: test-version@workspace:.: Bumped to 1.2.2 ← wrong; should be 1.3.1
```

Root cause is a base-version divergence between two internal paths:

  • packages/plugin-version/sources/commands/version.ts picked a release strategy by calling suggestStrategy(workspace.manifest.version, this.strategy). With version="1.3.1-alpha" and arg "1.3.1", semver.inc("1.3.1-alpha", "patch") === "1.3.1" matched, so the suggestion came back as patch.
  • packages/plugin-version/sources/versionUtils.ts (inside resolveVersionFiles) deliberately bases the bump on workspace.manifest.raw.stableVersion ?? workspace.manifest.version, per the existing comment "If there's a stableVersion field, then we assume that version contains a prerelease version and that we need to base the version bump relative to the latest stable instead." With stableVersion="1.2.1", semver.inc("1.2.1", "patch") === "1.2.2" — hence the wrong bump.

The two paths disagreed on which version to treat as the base.

How did you fix it?

Mirror the stableVersion-aware base in the suggest step so both paths agree on the baseline:

```ts
// packages/plugin-version/sources/commands/version.ts
const baseVersion = workspace.manifest.raw.stableVersion ?? workspace.manifest.version;
const suggestedStrategy = versionUtils.suggestStrategy(baseVersion, this.strategy);
```

When no pre-defined strategy matches the stable base (as in the #7025 case), the literal semver argument is preserved — applyStrategy already short-circuits on semver.valid(strategy) and returns it verbatim.

For the #7025 scenario this restores the expected flow:

  • baseVersion = "1.2.1", no strategy fits → releaseStrategy = "1.3.1" (literal).
  • applyStrategy(..., "1.3.1")"1.3.1". Post-bump applyReleases removes the now-obsolete stableVersion field because semver.prerelease("1.3.1") === null.

A regression test is added under packages/acceptance-tests/pkg-tests-specs/sources/commands/version.test.ts covering the exact repro. All existing tests in that file (20 pre-existing + the new one) pass. Changeset shape follows #6879 (the most recent plugin-version-scoped PR): @yarnpkg/cli: patch + @yarnpkg/plugin-version: patch.

Checklist

  • I have read the Contributing Guide.
  • I have set the packages that need to be released for my changes to be effective.
  • I will check that all automated PR checks pass before the PR gets reviewed.

…are applyStrategy

When a workspace has a prerelease `version` shadowed by a `stableVersion`,
`yarn version <semver>` would suggest a strategy against the prerelease
base (`1.3.1-alpha`) but apply it against the stable base (`1.2.1`),
producing an unexpected patch bump (`1.2.2`) instead of honoring the
literal target (`1.3.1`). Mirror the stableVersion-aware base in the
suggest step so both paths agree on which version to bump from; when no
strategy fits, the literal semver arg is preserved (applyStrategy already
short-circuits on `semver.valid(strategy)`).

Fixes yarnpkg#7025
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.

[Bug?]: Updating yarn version from pre-release to release with same number does a 'patch' instead of correct release

1 participant