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

document external cargo subcommand PATH precedence #13194

Closed
eval-exec opened this issue Dec 22, 2023 · 6 comments · Fixed by #13203
Closed

document external cargo subcommand PATH precedence #13194

eval-exec opened this issue Dec 22, 2023 · 6 comments · Fixed by #13203
Assignees
Labels
A-custom-subcommands Area: custom 3rd party subcommand plugins A-documenting-cargo-itself Area: Cargo's documentation S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review

Comments

@eval-exec
Copy link
Contributor

eval-exec commented Dec 22, 2023

Problem

I have encountered an issue with the cargo nextest subcommand where it does not select the expected subcommand's version.

Steps

Here are the details of the problem:

  1. I have cargo-nextest in my PATH, located at /nix/store/3yl7vgm2qkp7skr5gskbfd457f6g43cg-cargo-nextest-0.9.66/bin/cargo-nextest, and its version is 0.9.66.
bash-5.2$ which cargo-nextest
/nix/store/3yl7vgm2qkp7skr5gskbfd457f6g43cg-cargo-nextest-0.9.66/bin/cargo-nextest
  1. Additionally, I have cargo-nextest installed in ~/.cargo/bin, and its version is 0.9.59.
bash-5.2$ ~/.cargo/bin/cargo-nextest --version
cargo-nextest 0.9.59
  1. Running ~/.cargo/bin/cargo-nextest --version confirms that the version installed in ~/.cargo/bin is indeed 0.9.59.

  2. However, when I execute cargo nextest --version, it incorrectly selects version 0.9.59 from ~/.cargo/bin, instead of the desired version 0.9.66 from /nix/store/3yl7vgm2qkp7skr5gskbfd457f6g43cg-cargo-nextest-0.9.66/bin/cargo-nextest.

bash-5.2$ cargo nextest --version
cargo-nextest-nextest 0.9.59

This behavior seems inconsistent and unexpected. It would be helpful if the cargo command could prioritize the version in the path /nix/store/3yl7vgm2qkp7skr5gskbfd457f6g43cg-cargo-nextest-0.9.66/bin when invoking the cargo nextest subcommand.

Thank you for your attention to this issue, and please let me know if you need any further information to reproduce or investigate the problem.

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.72.1 (103a7ff2e 2023-08-15)
release: 1.72.1
commit-hash: 103a7ff2ee7678d34f34d778614c5eb2525ae9de
commit-date: 2023-08-15
host: x86_64-unknown-linux-gnu
libgit2: 1.6.4 (sys:0.17.2 vendored)
libcurl: 8.1.2-DEV (sys:0.4.63+curl-8.1.2 vendored ssl:OpenSSL/1.1.1u)
ssl: OpenSSL 1.1.1u  30 May 2023
os: NixOS 24.5.0 [64-bit]
@eval-exec eval-exec added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Dec 22, 2023
@linyihai
Copy link
Contributor

Hello,

cargo-nextest is not the cargo built-in command and this issue may not be related to cargo.

And I see you desired cargo is in /nix/store/..., is there something like .env to activate it ?

@epage
Copy link
Contributor

epage commented Dec 22, 2023

This is a concern not with cargo-nextest but with how third-party subcommands are loaded.

On mobile so can't look everything up but cargo intentionally prefers the copy of packages it has installed. You can customize this by putting cargo's bin directory in PATH. If cargo sees it there, it will respect the users precedence.

@ehuss
Copy link
Contributor

ehuss commented Dec 22, 2023

Thanks for the report! As @epage mentioned, I believe cargo looks at PATH to determine the priority, so you can put ~/.cargo/bin later in the PATH. Closing as a duplicate of #11020 and #6507.

@ehuss ehuss closed this as not planned Won't fix, can't repro, duplicate, stale Dec 22, 2023
@weihanglo
Copy link
Member

Have we documented this behavior in The Cargo Book? If not, I would like to propose this as a documentation issue.

cargo/src/bin/cargo/main.rs

Lines 247 to 270 in 363a2d1

fn search_directories(config: &Config) -> Vec<PathBuf> {
let mut path_dirs = if let Some(val) = config.get_env_os("PATH") {
env::split_paths(&val).collect()
} else {
vec![]
};
let home_bin = config.home().clone().into_path_unlocked().join("bin");
// If any of that PATH elements contains `home_bin`, do not
// add it again. This is so that the users can control priority
// of it using PATH, while preserving the historical
// behavior of preferring it over system global directories even
// when not in PATH at all.
// See https://github.com/rust-lang/cargo/issues/11020 for details.
//
// Note: `p == home_bin` will ignore trailing slash, but we don't
// `canonicalize` the paths.
if !path_dirs.iter().any(|p| p == &home_bin) {
path_dirs.insert(0, home_bin);
};
path_dirs
}

@ehuss
Copy link
Contributor

ehuss commented Dec 22, 2023

Updating the documentation sounds good to me!

@ehuss ehuss reopened this Dec 22, 2023
@ehuss ehuss changed the title Inconsistent Version Selection for cargo's Subcommand document extern cargo subcommand PATH precedence Dec 22, 2023
@ehuss ehuss added A-documenting-cargo-itself Area: Cargo's documentation S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review and removed C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Dec 22, 2023
@ehuss ehuss changed the title document extern cargo subcommand PATH precedence document external cargo subcommand PATH precedence Dec 22, 2023
@weihanglo weihanglo added the A-custom-subcommands Area: custom 3rd party subcommand plugins label Dec 22, 2023
@linyihai
Copy link
Contributor

@rustbot claim

I plan to add this behavior in the custom-subcommands section. (a note block in the last )

@weihanglo weihanglo linked a pull request Dec 26, 2023 that will close this issue
bors added a commit that referenced this issue Dec 27, 2023
chore(doc): doc for custom subcommands look up.

### What does this PR try to resolve?

as the #13194 metions, the lookup rules for custom subcommands are only reflected in comments inside the code, and it is time to inform users of this behavior through documentation.

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

### Additional information
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-custom-subcommands Area: custom 3rd party subcommand plugins A-documenting-cargo-itself Area: Cargo's documentation S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants