diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index 21d3e081d8f..3a1d13757c4 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -35,10 +35,23 @@ macro_rules! t { #[macro_export] macro_rules! curr_dir { () => { - std::path::Path::new(file!()).parent().unwrap() + $crate::_curr_dir(std::path::Path::new(file!())); }; } +#[doc(hidden)] +pub fn _curr_dir(mut file_path: &'static Path) -> &'static Path { + if !file_path.exists() { + // HACK: Must be running in the rust-lang/rust workspace, adjust the paths accordingly. + let prefix = PathBuf::from("src").join("tools").join("cargo"); + if let Ok(crate_relative) = file_path.strip_prefix(prefix) { + file_path = crate_relative + } + } + assert!(file_path.exists(), "{} does not exist", file_path.display()); + file_path.parent().unwrap() +} + #[track_caller] pub fn panic_error(what: &str, err: impl Into) -> ! { let err = err.into();