-
Notifications
You must be signed in to change notification settings - Fork 185
fix(store): resolve partial semver versions like "1.20" in findBestVersion #481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
lloydpick
wants to merge
1
commit into
arabold:main
from
lloydpick:fix/480-partial-semver-versions-unmatchable
+61
−3
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -268,7 +268,11 @@ export class DocumentManagementService { | |
| */ | ||
| async listVersions(library: string): Promise<string[]> { | ||
| const versions = await this.store.queryUniqueVersions(library); | ||
| const validVersions = versions.filter((v) => semver.valid(v)); | ||
| // Accept anything coercible to semver (e.g. "1.20", "5"), not just strict | ||
| // X.Y.Z strings — otherwise real-world partial versions are silently | ||
| // dropped and become invisible to findBestVersion(). Non-version labels | ||
| // like "stable"/"latest" correctly still fail coercion and stay excluded. | ||
| const validVersions = versions.filter((v) => semver.coerce(v) !== null); | ||
| return sortVersionsDescending(validVersions); | ||
| } | ||
|
|
||
|
|
@@ -320,8 +324,22 @@ export class DocumentManagementService { | |
|
|
||
| let bestMatch: string | null = null; | ||
|
|
||
| // semver.maxSatisfying() requires every candidate to itself be a fully | ||
| // valid X.Y.Z semver string — a stored version like "1.20" fails that | ||
| // even though listVersions() now accepts it. Match on coerced versions, | ||
| // then map the winner back to its original stored string so downstream | ||
| // lookups (e.g. checkDocumentExists) use the string that's actually in | ||
| // the store. | ||
| const coercedToOriginal = new Map<string, string>(); | ||
| for (const v of versionStrings) { | ||
| const coerced = semver.coerce(v); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This destroys prerelease labels ( |
||
| if (coerced) coercedToOriginal.set(coerced.version, v); | ||
| } | ||
| const coercedCandidates = Array.from(coercedToOriginal.keys()); | ||
|
|
||
| if (!targetVersion || targetVersion === "latest") { | ||
| bestMatch = semver.maxSatisfying(versionStrings, "*"); | ||
| const coercedBest = semver.maxSatisfying(coercedCandidates, "*"); | ||
| bestMatch = coercedBest ? (coercedToOriginal.get(coercedBest) ?? null) : null; | ||
| } else { | ||
| const versionRegex = /^(\d+)(?:\.(?:x(?:\.x)?|\d+(?:\.(?:x|\d+))?))?$|^$/; | ||
| if (!semver.valid(targetVersion) && !versionRegex.test(targetVersion)) { | ||
|
|
@@ -338,7 +356,8 @@ export class DocumentManagementService { | |
| range = `${range} || <=${targetVersion}`; | ||
| } | ||
| // If it was already a valid range (like '1.x'), use it directly | ||
| bestMatch = semver.maxSatisfying(versionStrings, range); | ||
| const coercedBest = semver.maxSatisfying(coercedCandidates, range); | ||
| bestMatch = coercedBest ? (coercedToOriginal.get(coercedBest) ?? null) : null; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we avoid keeping just one original label for each normalized version? If both a partial version and its full form are stored, one replaces the other here, and looking up the partial form can return a different version label. Please add coverage for both stored forms.
Something like this: