-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: reset $CARGO if the running program is real cargo[.exe]
#15208
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -391,10 +391,13 @@ fn cargo_subcommand_env() { | |
.canonicalize() | ||
.unwrap(); | ||
let envtest_bin = envtest_bin.to_str().unwrap(); | ||
// Previously, `$CARGO` would be left at `envtest_bin`. However, with the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seeing this test change and I wonder if using Maybe we can detect iff the current There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @epage as you proposed the current solution There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no problem with implementing this if @epage agrees. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is fair. Thanks Ed. |
||
// fix for #15099, `$CARGO` is now overwritten with the path to the current | ||
// exe when it is detected to be a cargo binary. | ||
cargo_process("envtest") | ||
.env("PATH", &path) | ||
.env(cargo::CARGO_ENV, &envtest_bin) | ||
.with_stdout_data(format!("{}\n", envtest_bin).raw().raw()) | ||
.with_stdout_data(format!("{}\n", cargo.display()).raw()) | ||
.run(); | ||
} | ||
|
||
|
@@ -570,3 +573,87 @@ fn full_did_you_mean() { | |
"#]]) | ||
.run(); | ||
} | ||
|
||
#[cargo_test] | ||
fn overwrite_cargo_environment_variable() { | ||
// If passed arguments `arg1 arg2 ...`, this program runs them as a command. | ||
// If passed no arguments, this program simply prints `$CARGO`. | ||
let p = project() | ||
.file("Cargo.toml", &basic_manifest("foo", "1.0.0")) | ||
.file( | ||
"src/main.rs", | ||
r#" | ||
fn main() { | ||
let mut args = std::env::args().skip(1); | ||
if let Some(arg1) = args.next() { | ||
let status = std::process::Command::new(arg1) | ||
.args(args) | ||
.status() | ||
.unwrap(); | ||
assert!(status.success()); | ||
} else { | ||
eprintln!("{}", std::env::var("CARGO").unwrap()); | ||
} | ||
} | ||
"#, | ||
) | ||
.build(); | ||
|
||
// Create two other cargo binaries in the project root, one with the wrong | ||
// name and one with the right name. | ||
let cargo_exe = cargo_test_support::cargo_exe(); | ||
let wrong_name_path = p | ||
.root() | ||
.join(format!("wrong_name{}", env::consts::EXE_SUFFIX)); | ||
let other_cargo_path = p.root().join(cargo_exe.file_name().unwrap()); | ||
std::fs::hard_link(&cargo_exe, &wrong_name_path).unwrap(); | ||
std::fs::hard_link(&cargo_exe, &other_cargo_path).unwrap(); | ||
|
||
// The output of each of the following commands should be `path-to-cargo`: | ||
// ``` | ||
// cargo run | ||
// cargo run -- cargo run | ||
// cargo run -- wrong_name run | ||
// ``` | ||
|
||
let cargo = cargo_exe.display().to_string(); | ||
let wrong_name = wrong_name_path.display().to_string(); | ||
let stderr_cargo = format!( | ||
"{}[EXE]\n", | ||
cargo_exe | ||
.canonicalize() | ||
.unwrap() | ||
.with_extension("") | ||
.to_str() | ||
.unwrap() | ||
); | ||
|
||
for cmd in [ | ||
"run", | ||
&format!("run -- {cargo} run"), | ||
&format!("run -- {wrong_name} run"), | ||
] { | ||
p.cargo(cmd).with_stderr_contains(&stderr_cargo).run(); | ||
} | ||
|
||
// The output of the following command should be `path-to-other-cargo`: | ||
// ``` | ||
// cargo run -- other_cargo run | ||
// ``` | ||
|
||
let other_cargo = other_cargo_path.display().to_string(); | ||
let stderr_other_cargo = format!( | ||
"{}[EXE]\n", | ||
other_cargo_path | ||
.canonicalize() | ||
.unwrap() | ||
.with_extension("") | ||
.to_str() | ||
.unwrap() | ||
.replace(p.root().parent().unwrap().to_str().unwrap(), "[ROOT]") | ||
); | ||
|
||
p.cargo(&format!("run -- {other_cargo} run")) | ||
.with_stderr_contains(stderr_other_cargo) | ||
.run(); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.