Skip to content

fix: preserve build metadata when constructor options change - #932

Open
KennyMcSimpson wants to merge 1 commit into
npm:mainfrom
KennyMcSimpson:fix/preserve-build-on-option-changes
Open

KennyMcSimpson wants to merge 1 commit into
npm:mainfrom
KennyMcSimpson:fix/preserve-build-on-option-changes

Conversation

@KennyMcSimpson

Copy link
Copy Markdown

compareBuild() and the sorting helpers lose build metadata when given SemVer objects whose options differ from the comparison options. For example:

const a = new semver.SemVer('1.0.0+2', { loose: true })
const b = new semver.SemVer('1.0.0+1', { loose: true })
semver.compareBuild(a, b) // currently 0; should be 1

The constructor reparses an existing object's .version when its flags differ, but .version deliberately omits build metadata. Reattach the existing build identifiers to that normalized string before reparsing. Keep the same-options fast path and normalization of loose input when changing to strict options.

Add regressions for both flags in both directions, normalized loose input, and compareBuild(), sort() and rsort() on version objects. Formatting and ordinary SemVer precedence retain their existing behavior: .version, format(), valid() and toString() still omit build metadata, as discussed in #264 and #684.

Validation on Windows, Node 24.11.0:

  • All four new regression groups fail against unchanged source and pass with the fix.
  • Official npm test --ignore-scripts: all 53 test files pass; 100% statement, branch, function and line coverage.
  • npm run lint --ignore-scripts, npm run postlint --ignore-scripts, and git diff --check pass.
  • Independent review checked option combinations, absent builds, leading zeros in build identifiers and loose-to-strict normalization.

Developed with OpenAI Codex assistance.

@KennyMcSimpson
KennyMcSimpson requested a review from a team as a code owner October 24, 2026 13:52
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