Skip to content

Commit a82794e

Browse files
committed
Fix path issues for running rustup wrapper on Windows.
Cargo likes to modify PATH, which circumvents the ability to choose the correct "cargo" executable to run on Windows (because Windows uses PATH for both binary and shared library searching).
1 parent ccaa118 commit a82794e

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

crates/cargo-test-macro/src/lib.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use proc_macro::*;
2+
use std::path::Path;
23
use std::process::Command;
34
use std::sync::Once;
45

@@ -210,8 +211,9 @@ fn version() -> (u32, bool) {
210211
unsafe { VERSION }
211212
}
212213

213-
fn check_command(command_name: &str, args: &[&str]) -> bool {
214-
let mut command = Command::new(command_name);
214+
fn check_command(command_path: &Path, args: &[&str]) -> bool {
215+
let mut command = Command::new(command_path);
216+
let command_name = command.get_program().to_str().unwrap().to_owned();
215217
command.args(args);
216218
let output = match command.output() {
217219
Ok(output) => output,
@@ -220,7 +222,7 @@ fn check_command(command_name: &str, args: &[&str]) -> bool {
220222
// environments like Docker. Consider installing it if Cargo
221223
// gains more hg support, but otherwise it isn't critical.
222224
// * lldb is not pre-installed on Ubuntu and Windows, so skip.
223-
if is_ci() && !matches!(command_name, "hg" | "lldb") {
225+
if is_ci() && !matches!(command_name.as_str(), "hg" | "lldb") {
224226
panic!("expected command `{command_name}` to be somewhere in PATH: {e}",);
225227
}
226228
return false;
@@ -240,7 +242,7 @@ fn check_command(command_name: &str, args: &[&str]) -> bool {
240242
}
241243

242244
fn has_command(command: &str) -> bool {
243-
check_command(command, &["--version"])
245+
check_command(Path::new(command), &["--version"])
244246
}
245247

246248
fn has_rustup_stable() -> bool {
@@ -255,7 +257,16 @@ fn has_rustup_stable() -> bool {
255257
// https://github.com/rust-lang/rustup/issues/3036 is resolved.
256258
return false;
257259
}
258-
check_command("cargo", &["+stable", "--version"])
260+
// Cargo mucks with PATH on Windows, adding sysroot host libdir, which is
261+
// "bin", which circumvents the rustup wrapper. Use the path directly from
262+
// CARGO_HOME.
263+
let home = match option_env!("CARGO_HOME") {
264+
Some(home) => home,
265+
None if is_ci() => panic!("expected to run under rustup"),
266+
None => return false,
267+
};
268+
let cargo = Path::new(home).join("bin/cargo");
269+
check_command(&cargo, &["+stable", "--version"])
259270
}
260271

261272
/// Whether or not this running in a Continuous Integration environment.

tests/testsuite/global_cache_tracker.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ use cargo::GlobalContext;
1414
use cargo_test_support::paths::{self, CargoPathExt};
1515
use cargo_test_support::registry::{Package, RegistryBuilder};
1616
use cargo_test_support::{
17-
basic_manifest, cargo_process, execs, git, project, retry, sleep_ms, thread_wait_timeout,
18-
Project,
17+
basic_manifest, cargo_process, execs, git, process, project, retry, sleep_ms,
18+
thread_wait_timeout, Execs, Project,
1919
};
2020
use itertools::Itertools;
2121
use std::fmt::Write;
22+
use std::path::Path;
2223
use std::path::PathBuf;
2324
use std::process::Stdio;
2425
use std::time::{Duration, SystemTime};
@@ -153,6 +154,14 @@ fn populate_cache(
153154
(cache_dir, src_dir)
154155
}
155156

157+
fn rustup_cargo() -> Execs {
158+
// Get the path to the rustup cargo wrapper. This is necessary because
159+
// cargo adds the "deps" directory into PATH on Windows, which points to
160+
// the wrong cargo.
161+
let rustup_cargo = Path::new(&std::env::var_os("CARGO_HOME").unwrap()).join("bin/cargo");
162+
execs().with_process_builder(process(rustup_cargo))
163+
}
164+
156165
#[cargo_test]
157166
fn auto_gc_gated() {
158167
// Requires -Zgc to both track last-use data and to run auto-gc.
@@ -1915,12 +1924,11 @@ fn compatible_with_older_cargo() {
19151924
middle = "1.0"
19161925
"#,
19171926
);
1918-
p.process("cargo")
1927+
rustup_cargo()
19191928
.args(&["+stable", "check", "-Zgc"])
1929+
.cwd(p.root())
19201930
.masquerade_as_nightly_cargo(&["gc"])
19211931
.env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(2))
1922-
// Necessary since `process` removes rustup.
1923-
.env("PATH", std::env::var_os("PATH").unwrap())
19241932
.run();
19251933
assert_eq!(get_registry_names("src"), ["middle-1.0.0", "new-1.0.0"]);
19261934
assert_eq!(
@@ -1978,11 +1986,10 @@ fn forward_compatible() {
19781986
.file("src/lib.rs", "")
19791987
.build();
19801988

1981-
p.process("cargo")
1989+
rustup_cargo()
19821990
.args(&["+stable", "check", "-Zgc"])
1991+
.cwd(p.root())
19831992
.masquerade_as_nightly_cargo(&["gc"])
1984-
// Necessary since `process` removes rustup.
1985-
.env("PATH", std::env::var_os("PATH").unwrap())
19861993
.run();
19871994

19881995
let config = GlobalContextBuilder::new().unstable_flag("gc").build();

0 commit comments

Comments
 (0)