Skip to content

Conversation

@elrrrrrrr
Copy link
Member

@elrrrrrrr elrrrrrrr commented Jan 7, 2026

https://registry.npmmirror.com/koa-compose/%5E4 returns 4.2.0, but should prioritize returning the latest-tagged version 4.1.0

  • 🐞 getVersion first check if the latest tag satisfies the version range

https://registry.npmmirror.com/koa-compose/%5E4 会返回 4.2.0,预期优先返回 latest 指定的 4.1.0

  • 🐞 getVersion 返回时,优先判断 latest tag 是否满足

Copilot AI review requested due to automatic review settings January 7, 2026 02:38
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 7, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

The version resolution logic in PackageVersionService is modified to prioritize the 'latest' dist-tag when it satisfies the requested semantic version range, falling back to range-based resolution only when necessary. Corresponding test cases validate this behavior with various range specifications.

Changes

Cohort / File(s) Summary
Version Resolution Logic
app/core/service/PackageVersionService.ts
Modified getVersion handling to check if the 'latest' dist-tag satisfies the semver range first; falls back to existing range-based resolution (SqlRange, prerelease handling, max-satisfying) only if latest tag does not match. Bug-version adjustment flow preserved.
Integration Tests
test/port/controller/package/ShowPackageVersionController.test.ts
Added duplicate test cases validating version resolution when mixing dist-tags and semver ranges; tests publish multiple versions, manipulate dist-tags, and assert HTTP responses for range queries like ^4 and >=4.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • killagu

Poem

🐰 A clever hop through versions past,
We check the latest tag real fast—
If ranges match, we're done with glee,
Else search the semver symphony! 🎵

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: implementing logic to prefer the latest tag when it satisfies a semver range, addressing the reported issue.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@elrrrrrrr elrrrrrrr marked this pull request as draft January 7, 2026 02:38
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @elrrrrrrr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求修复了包版本解析逻辑中的一个问题,即在某些情况下,即使 "latest" 标签指向的版本满足请求的语义化版本范围,系统仍会返回仓库中存在的最新版本。通过引入对 "latest" 标签版本的优先判断,确保了在满足范围的前提下,始终优先返回 "latest" 标签所指定的版本,从而使版本解析行为更符合预期,尤其是在处理像 "^4" 这样的范围请求时。

Highlights

  • 优先使用 "latest" 标签版本: 在解析版本范围时,如果 "latest" 标签指向的版本满足请求的范围,则优先返回该 "latest" 标签版本,解决了当存在比 "latest" 标签版本更新但仍满足范围的版本时,错误地返回最新版本而非 "latest" 标签版本的问题。
  • 新增测试用例: 增加了详细的测试用例,验证了在不同发布和标签更新场景下,版本解析逻辑的正确性,确保了 "latest" 标签版本在满足范围时的优先返回行为。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 修复了在解析 semver 范围时,未优先返回满足条件的 latest 标签对应版本的问题。核心逻辑修改在 PackageVersionService.ts 中,通过在范围解析时优先检查 latest 标签来解决此问题。同时,PR 在 ShowPackageVersionController.test.ts 中增加了一个非常全面的集成测试,完整地覆盖了修复的场景,确保了修复的正确性。代码改动清晰,测试用例充分。我有一个关于性能影响的建议,请查看具体评论。

Comment on lines +119 to 134
const latestVersion = await this.packageVersionRepository.findVersionByTag(scope, name, 'latest');
if (latestVersion && semver.satisfies(latestVersion, range)) {
version = latestVersion;
} else {
version = await this.packageVersionRepository.findMaxSatisfyVersion(scope, name, paddingSemVer);
const paddingSemVer = new SqlRange(range);
if (paddingSemVer.containPreRelease) {
const versions = await this.packageVersionRepository.findSatisfyVersionsWithPrerelease(
scope,
name,
paddingSemVer,
);
version = semver.maxSatisfying(versions, range);
} else {
version = await this.packageVersionRepository.findMaxSatisfyVersion(scope, name, paddingSemVer);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

这个改动正确地实现了优先返回 latest 标签版本的功能。

不过,这个实现为每个版本范围(range)的解析都增加了一次数据库查询(findVersionByTag)。在某些情况下(例如,当 fetchSpec 本身也是一个标签时),这可能导致在一次版本解析的流程中有多达三次数据库查询。

考虑到版本范围解析是一个高频操作,这可能会对服务的整体性能产生影响。建议考虑对 latest 标签对应的版本号进行缓存,以减少数据库的压力。例如,可以为 findVersionByTag(scope, name, 'latest') 的结果添加一个较短时间的缓存(比如 1-5 分钟)。

@codecov
Copy link

codecov bot commented Jan 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.21%. Comparing base (e5ec277) to head (2758479).

❗ There is a different number of reports uploaded between BASE (e5ec277) and HEAD (2758479). Click for more details.

HEAD has 11 uploads less than BASE
Flag BASE (e5ec277) HEAD (2758479)
29 18
Additional details and impacted files
@@             Coverage Diff             @@
##           master     #954       +/-   ##
===========================================
- Coverage   95.38%   82.21%   -13.17%     
===========================================
  Files         197      197               
  Lines       22264    22271        +7     
  Branches     2463     1859      -604     
===========================================
- Hits        21236    18311     -2925     
- Misses       1028     3960     +2932     

☔ View full report in Codecov by Sentry.
📢 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.

version = semver.maxSatisfying(versions, range);
// Prefer latest tag version if it satisfies the range
// e.g., latest=4.1.0, newest=4.2.0, range=^4 should return 4.1.0
const latestVersion = await this.packageVersionRepository.findVersionByTag(scope, name, 'latest');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

类似的 latest-4, latest-1 这种,需要做优先级处理吗?
如果同时存在 latest-4 和 latest,并且它们指向的版本不一致,以那个 tag 为最高优先级判断?

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes version range resolution to prioritize the latest dist-tag over the maximum satisfying version. When a package has latest=4.1.0 and a newer version 4.2.0, requesting ^4 should return 4.1.0 (the latest tag) instead of 4.2.0 (the maximum version).

Key Changes:

  • Modified PackageVersionService.getVersion() to check if the latest tag satisfies the requested range before falling back to maximum version resolution
  • Added comprehensive test coverage for the new behavior including edge cases with URL-encoded ranges and tag updates

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
app/core/service/PackageVersionService.ts Added logic to prefer latest tag version when it satisfies the semver range, before falling back to maximum satisfying version lookup
test/port/controller/package/ShowPackageVersionController.test.ts Added comprehensive test case covering the new behavior with multiple scenarios including tag updates and different range formats

Comment on lines +119 to +121
const latestVersion = await this.packageVersionRepository.findVersionByTag(scope, name, 'latest');
if (latestVersion && semver.satisfies(latestVersion, range)) {
version = latestVersion;
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new logic to prefer the latest tag applies to ALL range queries, including the wildcard *. This will change the behavior of wildcard queries from returning the maximum satisfying version to returning the latest tag version when it satisfies the range.

This is a breaking change that will affect the existing test at test/core/service/PackageVersionService.test.ts lines 119-124, which expects getVersion(npa('mock_package@*')) to return '1.1.0' (the max version) when the latest tag points to '1.0.0'.

Consider adding special handling for the wildcard * range to preserve backward compatibility, or update all related tests to reflect this new behavior. The wildcard * is typically expected to return the highest version, not necessarily the latest tag.

Copilot uses AI. Check for mistakes.
Comment on lines 279 to 348
it('should prefer latest tag version when it satisfies semver range', async () => {
// Scenario: latest=4.1.0, newest=4.2.0, request ^4 should return 4.1.0
mock(app.config.cnpmcore, 'allowPublishNonScopePackage', true);

// Publish 4.1.0
const pkg410 = await TestUtil.getFullPackage({
name: 'foo-prefer-latest',
version: '4.1.0',
versionObject: {
description: 'version 4.1.0',
},
});
await app
.httpRequest()
.put(`/${pkg410.name}`)
.set('authorization', publisher.authorization)
.set('user-agent', publisher.ua)
.send(pkg410)
.expect(201);

// Publish 4.2.0 without updating latest tag
const pkg420 = await TestUtil.getFullPackage({
name: 'foo-prefer-latest',
version: '4.2.0',
versionObject: {
description: 'version 4.2.0',
},
});
// Without dist-tags, latest tag won't be updated
delete pkg420['dist-tags'];
await app
.httpRequest()
.put(`/${pkg420.name}`)
.set('authorization', publisher.authorization)
.set('user-agent', publisher.ua)
.send(pkg420)
.expect(201);

// Verify latest tag is still 4.1.0
let res = await app.httpRequest().get(`/${pkg410.name}/latest`).expect(200);
assert.equal(res.body.version, '4.1.0');

// Request ^4 should return latest tag version 4.1.0, not max version 4.2.0
res = await app.httpRequest().get(`/${pkg410.name}/^4`).expect(200);
assert.equal(res.body.version, '4.1.0');

res = await app.httpRequest().get(`/${pkg410.name}/%5E4`).expect(200);
assert.equal(res.body.version, '4.1.0');

// Request ^4.2.0 should return 4.2.0 (latest 4.1.0 doesn't satisfy ^4.2.0)
res = await app.httpRequest().get(`/${pkg410.name}/^4.2.0`).expect(200);
assert.equal(res.body.version, '4.2.0');

// Request >=4 should also return latest 4.1.0
res = await app.httpRequest().get(`/${pkg410.name}/>=4`).expect(200);
assert.equal(res.body.version, '4.1.0');

// After updating latest tag to 4.2.0, ^4 should return 4.2.0
await app
.httpRequest()
.put(`/-/package/${pkg410.name}/dist-tags/latest`)
.set('authorization', publisher.authorization)
.set('user-agent', publisher.ua)
.set('content-type', 'application/json')
.send(JSON.stringify('4.2.0'))
.expect(200);

res = await app.httpRequest().get(`/${pkg410.name}/^4`).expect(200);
assert.equal(res.body.version, '4.2.0');
});
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test scenario is well-designed and covers multiple edge cases including URL-encoded version ranges. However, consider adding a test case for the wildcard * range to ensure it behaves as expected with the new logic. The wildcard is also parsed as a range and will be affected by the change to prefer the latest tag.

Copilot uses AI. Check for mistakes.
Comment on lines +317 to +318
// Verify latest tag is still 4.1.0
let res = await app.httpRequest().get(`/${pkg410.name}/latest`).expect(200);
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test setup is incomplete. After publishing version 4.2.0, the test should verify that the package has both versions available before making assertions about which version is returned. Consider adding an assertion to confirm that both versions exist in the system after the second publish operation completes.

Suggested change
// Verify latest tag is still 4.1.0
let res = await app.httpRequest().get(`/${pkg410.name}/latest`).expect(200);
// Verify both versions exist after second publish
let res = await app.httpRequest().get(`/${pkg410.name}/4.1.0`).expect(200);
assert.equal(res.body.version, '4.1.0');
res = await app.httpRequest().get(`/${pkg410.name}/4.2.0`).expect(200);
assert.equal(res.body.version, '4.2.0');
// Verify latest tag is still 4.1.0
res = await app.httpRequest().get(`/${pkg410.name}/latest`).expect(200);

Copilot uses AI. Check for mistakes.
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.

3 participants