Skip to content

Don't canonicalize executable path in cargo_exe #15355

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

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ use crate::sources::CRATES_IO_REGISTRY;
use crate::util::errors::CargoResult;
use crate::util::network::http::configure_http_handle;
use crate::util::network::http::http_handle;
use crate::util::try_canonicalize;
use crate::util::{internal, CanonicalUrl};
use crate::util::{Filesystem, IntoUrl, IntoUrlWithBase, Rustc};
use anyhow::{anyhow, bail, format_err, Context as _};
Expand Down Expand Up @@ -457,11 +456,10 @@ impl GlobalContext {
// commands that use Cargo as a library to inherit (via `cargo <subcommand>`)
// or set (by setting `$CARGO`) a correct path to `cargo` when the current exe
// is not actually cargo (e.g., `cargo-*` binaries, Valgrind, `ld.so`, etc.).
let exe = try_canonicalize(
self.get_env_os(crate::CARGO_ENV)
.map(PathBuf::from)
.ok_or_else(|| anyhow!("$CARGO not set"))?,
)?;
let exe = self
.get_env_os(crate::CARGO_ENV)
.map(PathBuf::from)
.ok_or_else(|| anyhow!("$CARGO not set"))?;
Ok(exe)
};

Expand All @@ -470,7 +468,7 @@ impl GlobalContext {
// The method varies per operating system and might fail; in particular,
// it depends on `/proc` being mounted on Linux, and some environments
// (like containers or chroots) may not have that available.
let exe = try_canonicalize(env::current_exe()?)?;
let exe = env::current_exe()?;
Ok(exe)
}

Expand All @@ -481,8 +479,6 @@ impl GlobalContext {
// Otherwise, it has multiple components and is either:
// - a relative path (e.g., `./cargo`, `target/debug/cargo`), or
// - an absolute path (e.g., `/usr/local/bin/cargo`).
// In either case, `Path::canonicalize` will return the full absolute path
// to the target if it exists.
let argv0 = env::args_os()
.map(PathBuf::from)
.next()
Expand Down
7 changes: 2 additions & 5 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,9 @@ fn custom_build_env_vars() {
)
.file("bar/src/lib.rs", "pub fn hello() {}");

let cargo = cargo_exe().canonicalize().unwrap();
let cargo = cargo_exe();
let cargo = cargo.to_str().unwrap();
let rustc = paths::resolve_executable("rustc".as_ref())
.unwrap()
.canonicalize()
.unwrap();
let rustc = paths::resolve_executable("rustc".as_ref()).unwrap();
let rustc = rustc.to_str().unwrap();
let file_content = format!(
r##"
Expand Down
10 changes: 2 additions & 8 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ fn cargo_subcommand_env() {
p.cargo("build").run();
assert!(p.bin("cargo-envtest").is_file());

let cargo = cargo_exe().canonicalize().unwrap();
let cargo = cargo_exe();
let mut path = path();
path.push(target_dir.clone());
let path = env::join_paths(path.iter()).unwrap();
Expand All @@ -388,9 +388,7 @@ fn cargo_subcommand_env() {
// Check that subcommands inherit an overridden $CARGO
let envtest_bin = target_dir
.join("cargo-envtest")
.with_extension(std::env::consts::EXE_EXTENSION)
.canonicalize()
.unwrap();
.with_extension(std::env::consts::EXE_EXTENSION);
let envtest_bin = envtest_bin.to_str().unwrap();
// Previously, `$CARGO` would be left at `envtest_bin`. However, with the
// fix for #15099, `$CARGO` is now overwritten with the path to the current
Expand Down Expand Up @@ -623,8 +621,6 @@ fn overwrite_cargo_environment_variable() {
let stderr_cargo = format!(
"{}[EXE]\n",
cargo_exe
.canonicalize()
.unwrap()
.with_extension("")
.to_str()
.unwrap()
Expand All @@ -648,8 +644,6 @@ fn overwrite_cargo_environment_variable() {
let stderr_other_cargo = format!(
"{}[EXE]\n",
other_cargo_path
.canonicalize()
.unwrap()
.with_extension("")
.to_str()
.unwrap()
Expand Down
4 changes: 0 additions & 4 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3890,8 +3890,6 @@ fn cargo_test_env() {
let cargo = format!(
"{}[EXE]",
cargo_exe()
.canonicalize()
.unwrap()
.with_extension("")
.to_str()
.unwrap()
Expand All @@ -3913,8 +3911,6 @@ test env_test ... ok
let stderr_other_cargo = format!(
"{}[EXE]",
other_cargo_path
.canonicalize()
.unwrap()
.with_extension("")
.to_str()
.unwrap()
Expand Down