diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 82e0d548fc7..8a83643c4cf 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -225,12 +225,16 @@ impl TargetInfo { } // See rust-lang/cargo#4490, rust-lang/cargo#4960. - // - Only uplift debuginfo for binaries. - // Tests are run directly from `target/debug/deps/` - // and examples are inside target/debug/examples/ which already have symbols next to them, - // so no need to do anything. - if *kind == TargetKind::Bin { - if target_triple.contains("-apple-") { + // Only uplift debuginfo for binaries. + // - Tests are run directly from `target/debug/deps/` with the + // metadata hash still in the filename. + // - Examples are only uplifted for apple because the symbol file + // needs to match the executable file name to be found (i.e., it + // needs to remove the hash in the filename). On Windows, the path + // to the .pdb with the hash is embedded in the executable. + let is_apple = target_triple.contains("-apple-"); + if *kind == TargetKind::Bin || (*kind == TargetKind::ExampleBin && is_apple) { + if is_apple { ret.push(FileType { suffix: ".dSYM".to_string(), prefix: prefix.clone(), diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index f6aabe544e2..14e5263b929 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -4132,10 +4132,8 @@ fn building_a_dependent_crate_witout_bin_should_fail() { } #[test] +#[cfg(any(target_os = "macos", target_os = "ios"))] fn uplift_dsym_of_bin_on_mac() { - if !cfg!(any(target_os = "macos", target_os = "ios")) { - return; - } let p = project() .file("src/main.rs", "fn main() { panic!(); }") .file("src/bin/b.rs", "fn main() { panic!(); }") @@ -4144,23 +4142,17 @@ fn uplift_dsym_of_bin_on_mac() { .build(); p.cargo("build --bins --examples --tests").run(); - assert!(p.bin("foo.dSYM").is_dir()); - assert!(p.bin("b.dSYM").is_dir()); - assert!(p - .bin("b.dSYM") - .symlink_metadata() - .expect("read metadata from b.dSYM") - .file_type() - .is_symlink()); - assert!(!p.bin("c.dSYM").is_dir()); - assert!(!p.bin("d.dSYM").is_dir()); + assert!(p.target_debug_dir().join("foo.dSYM").is_dir()); + assert!(p.target_debug_dir().join("b.dSYM").is_dir()); + assert!(p.target_debug_dir().join("b.dSYM").is_symlink()); + assert!(p.target_debug_dir().join("examples/c.dSYM").is_symlink()); + assert!(!p.target_debug_dir().join("c.dSYM").exists()); + assert!(!p.target_debug_dir().join("d.dSYM").exists()); } #[test] +#[cfg(all(target_os = "windows", target_env = "msvc"))] fn uplift_pdb_of_bin_on_windows() { - if !cfg!(all(target_os = "windows", target_env = "msvc")) { - return; - } let p = project() .file("src/main.rs", "fn main() { panic!(); }") .file("src/bin/b.rs", "fn main() { panic!(); }") @@ -4171,8 +4163,10 @@ fn uplift_pdb_of_bin_on_windows() { p.cargo("build --bins --examples --tests").run(); assert!(p.target_debug_dir().join("foo.pdb").is_file()); assert!(p.target_debug_dir().join("b.pdb").is_file()); - assert!(!p.target_debug_dir().join("c.pdb").is_file()); - assert!(!p.target_debug_dir().join("d.pdb").is_file()); + assert!(!p.target_debug_dir().join("examples/c.pdb").exists()); + assert_eq!(p.glob("target/debug/examples/c-*.pdb").count(), 1); + assert!(!p.target_debug_dir().join("c.pdb").exists()); + assert!(!p.target_debug_dir().join("d.pdb").exists()); } // Ensure that `cargo build` chooses the correct profile for building diff --git a/tests/testsuite/support/paths.rs b/tests/testsuite/support/paths.rs index 3cc3f280e45..0b7120e2556 100644 --- a/tests/testsuite/support/paths.rs +++ b/tests/testsuite/support/paths.rs @@ -70,6 +70,8 @@ pub trait CargoPathExt { fn move_in_time(&self, travel_amount: F) where F: Fn(i64, u32) -> (i64, u32); + + fn is_symlink(&self) -> bool; } impl CargoPathExt for Path { @@ -142,6 +144,10 @@ impl CargoPathExt for Path { }); } } + + fn is_symlink(&self) -> bool { + fs::symlink_metadata(self).map(|m| m.file_type().is_symlink()).unwrap_or(false) + } } fn do_op(path: &Path, desc: &str, mut f: F)