Skip to content
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

Consider rust-version when selecting packages for cargo add #12078

Merged
merged 4 commits into from
May 23, 2023

Conversation

cassaundra
Copy link
Contributor

When -Zmsrv-policy is enabled, try to select dependencies which satisfy the target package's rust-version field (if present). If the selected version is not also the latest, emit a warning to the user about this discrepancy.

Dependency versions without a rust-version are considered compatible by default.

One remaining question is whether we should go into more detail when explaining the discrepancy and ways to resolve it to the user. For example:

warning: selecting older version of `fancy-dep` to satisfy the minimum supported rust version
note: version 0.1.2 of `fancy-dep` has an MSRV of 1.72, which is greater than this package's MSRV of 1.69 

Implements #10653.

r? @epage

@rustbot rustbot added Command-add S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 2, 2023
@cassaundra cassaundra changed the title Cargo add rust version Consider rust-version when selecting packages for cargo add May 2, 2023
Copy link
Contributor

@epage epage left a comment

Choose a reason for hiding this comment

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

Thanks for moving this forward!

src/cargo/ops/cargo_add/mod.rs Outdated Show resolved Hide resolved
src/cargo/ops/cargo_add/mod.rs Outdated Show resolved Hide resolved
src/cargo/ops/cargo_add/mod.rs Outdated Show resolved Hide resolved
src/cargo/ops/cargo_add/mod.rs Outdated Show resolved Hide resolved
src/cargo/ops/cargo_add/mod.rs Outdated Show resolved Hide resolved
src/cargo/ops/cargo_add/mod.rs Outdated Show resolved Hide resolved
src/cargo/ops/cargo_add/mod.rs Outdated Show resolved Hide resolved
tests/testsuite/cargo_add/mod.rs Outdated Show resolved Hide resolved
tests/testsuite/cargo_add/rust_version/stderr.log Outdated Show resolved Hide resolved
tests/testsuite/cargo_add/rust_version/stderr.log Outdated Show resolved Hide resolved
@rustbot rustbot added the A-cli Area: Command-line interface, option parsing, etc. label May 4, 2023
@cassaundra
Copy link
Contributor Author

Made changes as requested, including the new unstable --ignore-rust-version flag for cargo add. I'll post a few review comments for things that stood out to me in a second...

src/cargo/ops/cargo_add/mod.rs Outdated Show resolved Hide resolved
@@ -518,8 +531,10 @@ fn get_existing_dependency(
}

fn get_latest_dependency(
spec: &Package,
dependency: &Dependency,
_flag_allow_prerelease: bool,
Copy link
Contributor Author

@cassaundra cassaundra May 4, 2023

Choose a reason for hiding this comment

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

side note: no clue what this _flag_allow_prerelease is. doesn't appear to ever have been used in cargo. I'm guessing a holdover from cargo-edit?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that is a vestige of an old cargo-edit version of cargo-add and can be removed.

if !config.cli_unstable().msrv_policy && !honor_rust_version {
return Err(CliError::new(
anyhow::format_err!(
"the `ignore-rust-version` flag is unstable, pass -Z msrv-policy to enable it"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I'd suggest something more like

"`--ignore-rust-version` is unstable; pass `-Zmsrv-policy` to enable support for it"

or look to https://docs.rs/cargo/latest/src/cargo/core/features.rs.html#1034-1057 to see what other unstable errors are like

Copy link
Contributor Author

@cassaundra cassaundra May 12, 2023

Choose a reason for hiding this comment

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

fail_if_stable_opt seems more suitable. Should we open a tracking issue separate from the #12043 PR so that it can be referenced in the message?

edit: same goes for the flag documentation as well

Copy link
Contributor

Choose a reason for hiding this comment

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

fail_if_stable_opt encourages -Zunstable-opt rather than letting us use -Zmsrv-policy, right?

Copy link
Contributor Author

@cassaundra cassaundra May 15, 2023

Choose a reason for hiding this comment

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

fail_if_stable_opt does encourage -Zunstable-options, but it does also link to an issue that includes information about the unstable flag.

src/bin/cargo/commands/add.rs Outdated Show resolved Hide resolved
@rustbot rustbot added A-cli-help Area: built-in command-line help A-documenting-cargo-itself Area: Cargo's documentation labels May 15, 2023
@cassaundra
Copy link
Contributor Author

cassaundra commented May 15, 2023

Apologies for the late update, I got pretty sick. In any case, here's what's new:

  • Don't assume rust-version minor is present (probably against spec, but to ensure compatibility).
  • Select the latest version with the lowest rust-version, not the first version. Update the rust_version_older test to reflect this.
  • Provide more information in the error/warning messages.
  • Move large closure to a rust_version_incompat_error function.
  • Document the --ignore-rust-version flag.
  • Gate --ignore-rust-version behind -Zunstable-options instead of -Zmsrv-policy.
  • Other cleanups.

Note: fail_if_stable_opt currently has its tracking issue set to 0. We can either pick an issue for that, or just write our own error message that does not include a link to an issue. Of course, don't merge until this has been resolved.

// Determine whether or not the rust-versions are compatible by
// comparing the lowest possible versions each of them could
// represent.
(req.0, req.1, req.2) >= (major, minor, patch)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can we do req >= (major, minor, patch)?

Comment on lines 619 to 620
"ignoring `{dependency}` {latest_version} to satisfy this package's \
rust-version of {rust_version}",
Copy link
Contributor

Choose a reason for hiding this comment

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

At minimum, we need to include the rust-version for latest

use crate::cargo_add::init_registry;
use cargo_test_support::curr_dir;

#[cargo_test(nightly, reason = "-Zmsrv-policy is unstable")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we filtering these tests for nightly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah I misunderstood what #[cargo_test(nightly)] was actually for (external nightly features), will fix

@@ -0,0 +1,3 @@
Updating `dummy-registry` index
warning: ignoring `rust-version-user` 0.2.1 to satisfy this package's rust-version of 1.70 (use `--ignore-rust-version` to override)
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, just had a thought. We could specify crates as rust-version-user@0.2.1. That is the command-line syntax.

Thoughts?

if ignore_rust_version {
config
.cli_unstable()
.fail_if_stable_opt("ignore-rust-version", 0)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: fail_if_stable_opt currently has its tracking issue set to 0. We can either pick an issue for that, or just write our own error message that does not include a link to an issue. Of course, don't merge until this has been resolved.

One option is that this PR doesn't close out #10653 but we keep it open to track to stabilization, so it acts as its own tracking issue.

@cassaundra
Copy link
Contributor Author

Fixed some of the issues raised (sans tracking issue), and cleaned things up a bit.

When `-Zmsrv-policy` is enabled, try to select dependencies which satisfy the
target package's `rust-version` field (if present). If the selected version is
not also the latest, emit a warning to the user about this discrepancy.

Dependency versions without a `rust-version` are considered compatible by
default.

Implements rust-lang#10653.
@cassaundra
Copy link
Contributor Author

Updated to now use unstable error message as suggested.

@epage
Copy link
Contributor

epage commented May 23, 2023

@bors r+

@bors
Copy link
Collaborator

bors commented May 23, 2023

📌 Commit 8df391b has been approved by epage

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 23, 2023
@bors
Copy link
Collaborator

bors commented May 23, 2023

⌛ Testing commit 8df391b with merge 64fb38c...

@bors
Copy link
Collaborator

bors commented May 23, 2023

☀️ Test successful - checks-actions
Approved by: epage
Pushing 64fb38c to master...

@bors bors merged commit 64fb38c into rust-lang:master May 23, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request May 23, 2023
Update cargo

10 commits in 09276c703a473ab33daaeb94917232e80eefd628..64fb38c97ac4d3a327fc9032c862dd28c8833b17
2023-05-16 21:43:35 +0000 to 2023-05-23 18:53:23 +0000
- Consider rust-version when selecting packages for cargo add (rust-lang/cargo#12078)
- fix(lints): Switch to -Zlints so stable projects can experiment (rust-lang/cargo#12168)
- Automatically inherit workspace fields when running cargo new/init (rust-lang/cargo#12069)
- ci: check if any version bump needed for member crates (rust-lang/cargo#12126)
- feat: `lints` feature (rust-lang/cargo#12148)
- fix: pass `-C debuginfo` after weakening if explicitly set (rust-lang/cargo#12165)
- Tweak build help to clarify role of --bin (rust-lang/cargo#12157)
- fix: Pass CI on nightly (rust-lang/cargo#12160)
- docs(source): doc comments for Source and its impls (rust-lang/cargo#12159)
- docs(source): doc comments for `Source` and friends (rust-lang/cargo#12153)

r? `@ghost`
saethlin pushed a commit to saethlin/miri that referenced this pull request May 26, 2023
Update cargo

10 commits in 09276c703a473ab33daaeb94917232e80eefd628..64fb38c97ac4d3a327fc9032c862dd28c8833b17
2023-05-16 21:43:35 +0000 to 2023-05-23 18:53:23 +0000
- Consider rust-version when selecting packages for cargo add (rust-lang/cargo#12078)
- fix(lints): Switch to -Zlints so stable projects can experiment (rust-lang/cargo#12168)
- Automatically inherit workspace fields when running cargo new/init (rust-lang/cargo#12069)
- ci: check if any version bump needed for member crates (rust-lang/cargo#12126)
- feat: `lints` feature (rust-lang/cargo#12148)
- fix: pass `-C debuginfo` after weakening if explicitly set (rust-lang/cargo#12165)
- Tweak build help to clarify role of --bin (rust-lang/cargo#12157)
- fix: Pass CI on nightly (rust-lang/cargo#12160)
- docs(source): doc comments for Source and its impls (rust-lang/cargo#12159)
- docs(source): doc comments for `Source` and friends (rust-lang/cargo#12153)

r? `@ghost`
@ehuss ehuss added this to the 1.71.0 milestone May 30, 2023
bors added a commit that referenced this pull request Aug 25, 2023
refactor: Pull out cargo-add MSRV code for reuse

### What does this PR try to resolve?

#12078 added MSRV code in `cargo add`. Our assumption when writing it is that we'd need to generalize the code before reusing it in other places, like `cargo install`.  This PR focused purely on that refactor because I'm hopeful it will be useful for other work I'm doing.  Despite not having a user for this yet, I think the `cargo install` case is inevitable and I feel this does a bit to clean up MSRV related code by using a more specific type everywhere.

### How should we test and review this PR?

Each commit gradually progresses things along
bors added a commit that referenced this pull request Apr 1, 2024
Remove useless parameters

### What does this PR try to resolve?

From #12078 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cli Area: Command-line interface, option parsing, etc. A-cli-help Area: built-in command-line help A-documenting-cargo-itself Area: Cargo's documentation Command-add S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants