Skip to content

feat(add): improve cargo add X@latest error message#16406

Open
0xPoe wants to merge 7 commits into
rust-lang:masterfrom
0xPoe:poe-add-latest
Open

feat(add): improve cargo add X@latest error message#16406
0xPoe wants to merge 7 commits into
rust-lang:masterfrom
0xPoe:poe-add-latest

Conversation

@0xPoe

@0xPoe 0xPoe commented Dec 16, 2025

Copy link
Copy Markdown
Member

View all comments

What does this PR try to resolve?

refs #10741

This PR improves the error message for cargo add <name>@latest.

  • cargo add <name>@<latest> for the latest published version, excluding pre-releases and yanked versions

Original proposal: #10741 (comment)

How to test and review this PR?

Review it commit by commit.

@0xPoe 0xPoe changed the title test: add the test case for cargo add X@latest test(add): add the test case for cargo add X@latest Dec 16, 2025
@0xPoe 0xPoe force-pushed the poe-add-latest branch 2 times, most recently from d741881 to 72ea89c Compare April 2, 2026 21:04
@0xPoe 0xPoe changed the title test(add): add the test case for cargo add X@latest NOT READY FOR REVIEW: test(add): add the test case for cargo add X@latest Apr 2, 2026
@0xPoe 0xPoe force-pushed the poe-add-latest branch 5 times, most recently from 8f6212b to 2c8d357 Compare April 6, 2026 20:45
@0xPoe 0xPoe force-pushed the poe-add-latest branch 3 times, most recently from 2f63114 to b80ebde Compare April 6, 2026 21:03
@0xPoe 0xPoe changed the title NOT READY FOR REVIEW: test(add): add the test case for cargo add X@latest feat(add): improve cargo add X@latest error message Apr 6, 2026
@0xPoe 0xPoe changed the title feat(add): improve cargo add X@latest error message NOT READY FOR REVIEW: feat(add): improve cargo add X@latest error message Apr 7, 2026
Signed-off-by: 0xPoe <techregister@pm.me>
Signed-off-by: 0xPoe <techregister@pm.me>

@0xPoe 0xPoe left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🔢 Self-check (PR reviewed by myself and ready for feedback)

  • Code compiles successfully

  • Unit tests added

  • No AI-generated elegant nonsense in PR.

  • Comments added where necessary

  • PR title and description updated

  • Documentation PR created (or confirmed not needed)

  • PR size is reasonable

Some changes I would like to discuss with you first. What are your thoughts before I implement them?

r? @epage

View changes since this review

Comment thread src/cargo/ops/cargo_add/crate_spec.rs
Comment thread tests/testsuite/cargo_add/add_latest_alt_registry/stderr.term.svg
@0xPoe 0xPoe marked this pull request as ready for review April 13, 2026 21:51
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 13, 2026
@0xPoe 0xPoe changed the title NOT READY FOR REVIEW: feat(add): improve cargo add X@latest error message feat(add): improve cargo add X@latest error message Apr 13, 2026
Comment thread src/cargo/ops/cargo_add/crate_spec.rs Outdated
@0xPoe 0xPoe force-pushed the poe-add-latest branch 3 times, most recently from d963531 to bba51a3 Compare April 20, 2026 21:02
@0xPoe 0xPoe marked this pull request as draft April 20, 2026 21:17
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 20, 2026
@0xPoe 0xPoe force-pushed the poe-add-latest branch 4 times, most recently from e7fc3dc to 21d9cba Compare April 21, 2026 21:28
Comment thread src/cargo/ops/cargo_add/crate_spec.rs Outdated
Comment on lines +353 to +355
let request_latest = crate_spec
.as_ref()
.is_some_and(|crate_spec| matches!(crate_spec.version(), Some(VersionSpec::Latest)));

@epage epage Apr 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

how come you pulled this out up here?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is used in the following if-else statement.

Comment on lines +529 to +530
// Get the exact version that `cargo add <name>` would resolve to,
// respecting MSRV and existing version constraints.

@epage epage Apr 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What other constraints are there?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I believe it includes:

  • any existing version requirements
  • the selected registry or source
  • registry resolution behavior (path, yanked filtering, prefer stable version)

Basically, it's just what we would select by running the command without the latest version selector.

Comment on lines +529 to +530
// Get the exact version that `cargo add <name>` would resolve to,
// respecting MSRV and existing version constraints.

@epage epage Apr 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we reporting the MSRV-compatible version?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

From #10741 (comment)

$ cargo add clap@latest
error: invalid version requirement `latest`

help: to use X, run `cargo add clap`
help: to use the latest version, run `cargo add clap@4.5.38`

I thought we usually selected X by running cargo add clap?

@0xPoe 0xPoe marked this pull request as ready for review April 22, 2026 20:46
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 22, 2026
let resolved_version = resolved
.version()
.expect("resolved dependency should have version");
// Get the actual latest non-prerelease, non-yanked version from the registry,

@0xPoe 0xPoe Apr 22, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this comment is not entirely accurate. I discovered that the behavior of get_latest_dependency is to prefer the stable version. Only if there is no stable version available will it select the pre-release versions.

@epage Form you comment: #10741 (comment)

Yes, I was suggesting we suggest the latest version as that would be the literal meaning of latest and would therefore match the users intent. The user could have done this themselves. We likely should skip pre-release and yanked as we generally don't consider them the latest.

It may also make sense to inherit the same behavior: if there is no stable release, we suggest the pre-release version here?

View changes since the review

@0xPoe 0xPoe requested a review from epage April 22, 2026 20:52
Comment thread src/cargo/ops/cargo_add/crate_spec.rs Outdated
Comment thread src/cargo/ops/cargo_add/crate_spec.rs Outdated
Comment thread src/cargo/ops/cargo_add/crate_spec.rs Outdated
@0xPoe 0xPoe force-pushed the poe-add-latest branch 3 times, most recently from 184fb4e to 9ec528b Compare April 27, 2026 19:29
@0xPoe 0xPoe requested a review from epage April 27, 2026 19:39
@0xPoe 0xPoe force-pushed the poe-add-latest branch from 9ec528b to ff782c8 Compare June 7, 2026 20:35
Signed-off-by: 0xPoe <techregister@pm.me>
@0xPoe 0xPoe force-pushed the poe-add-latest branch from ff782c8 to d350679 Compare June 7, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Command-add S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants