From d1375ba70efbe6aaea06e069a215fb0bed43f308 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 5 Dec 2024 15:20:55 -0600 Subject: [PATCH 1/4] test(rustflags): Put remap success test first --- tests/testsuite/rustflags.rs | 82 ++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/tests/testsuite/rustflags.rs b/tests/testsuite/rustflags.rs index 0d73f4b99aa..74fd2a19586 100644 --- a/tests/testsuite/rustflags.rs +++ b/tests/testsuite/rustflags.rs @@ -1479,6 +1479,47 @@ fn env_rustflags_misspelled_build_script() { .run(); } +#[cargo_test] +fn remap_path_prefix_works() { + // Check that remap-path-prefix works. + Package::new("bar", "0.1.0") + .file("src/lib.rs", "pub fn f() -> &'static str { file!() }") + .publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [dependencies] + bar = "0.1" + "#, + ) + .file( + "src/main.rs", + r#" + fn main() { + println!("{}", bar::f()); + } + "#, + ) + .build(); + + p.cargo("run") + .env( + "RUSTFLAGS", + format!("--remap-path-prefix={}=/foo", paths::root().display()), + ) + .with_stdout_data(str![[r#" +/foo/home/.cargo/registry/src/-[HASH]/bar-0.1.0/src/lib.rs + +"#]]) + .run(); +} + #[cargo_test] fn remap_path_prefix_ignored() { let get_c_metadata_re = @@ -1521,47 +1562,6 @@ fn remap_path_prefix_ignored() { assert_data_eq!(rustc_c_metadata, build_c_metadata); } -#[cargo_test] -fn remap_path_prefix_works() { - // Check that remap-path-prefix works. - Package::new("bar", "0.1.0") - .file("src/lib.rs", "pub fn f() -> &'static str { file!() }") - .publish(); - - let p = project() - .file( - "Cargo.toml", - r#" - [package] - name = "foo" - version = "0.1.0" - - [dependencies] - bar = "0.1" - "#, - ) - .file( - "src/main.rs", - r#" - fn main() { - println!("{}", bar::f()); - } - "#, - ) - .build(); - - p.cargo("run") - .env( - "RUSTFLAGS", - format!("--remap-path-prefix={}=/foo", paths::root().display()), - ) - .with_stdout_data(str![[r#" -/foo/home/.cargo/registry/src/-[HASH]/bar-0.1.0/src/lib.rs - -"#]]) - .run(); -} - #[cargo_test] fn host_config_rustflags_with_target() { // regression test for https://github.com/rust-lang/cargo/issues/10206 From 9fcb5de16e5dc442b11be3642e2c83e1600225d6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 5 Dec 2024 15:22:18 -0600 Subject: [PATCH 2/4] test(rustflags): Pull out metadata extraction --- tests/testsuite/rustflags.rs | 39 ++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/tests/testsuite/rustflags.rs b/tests/testsuite/rustflags.rs index 74fd2a19586..25c28bfa56b 100644 --- a/tests/testsuite/rustflags.rs +++ b/tests/testsuite/rustflags.rs @@ -1522,25 +1522,6 @@ fn remap_path_prefix_works() { #[cargo_test] fn remap_path_prefix_ignored() { - let get_c_metadata_re = - regex::Regex::new(r".* (--crate-name [^ ]+).* (-C ?metadata=[^ ]+).*").unwrap(); - let get_c_metadata = |output: RawOutput| { - let stderr = String::from_utf8(output.stderr).unwrap(); - let mut c_metadata = get_c_metadata_re - .captures_iter(&stderr) - .map(|c| { - let (_, [name, c_metadata]) = c.extract(); - format!("{name} {c_metadata}") - }) - .collect::>(); - assert!( - !c_metadata.is_empty(), - "`{get_c_metadata_re:?}` did not match:\n```\n{stderr}\n```" - ); - c_metadata.sort(); - c_metadata.join("\n") - }; - let p = project().file("src/lib.rs", "").build(); let build_output = p @@ -1562,6 +1543,26 @@ fn remap_path_prefix_ignored() { assert_data_eq!(rustc_c_metadata, build_c_metadata); } +fn get_c_metadata(output: RawOutput) -> String { + let get_c_metadata_re = + regex::Regex::new(r".* (--crate-name [^ ]+).* (-C ?metadata=[^ ]+).*").unwrap(); + + let stderr = String::from_utf8(output.stderr).unwrap(); + let mut c_metadata = get_c_metadata_re + .captures_iter(&stderr) + .map(|c| { + let (_, [name, c_metadata]) = c.extract(); + format!("{name} {c_metadata}") + }) + .collect::>(); + assert!( + !c_metadata.is_empty(), + "`{get_c_metadata_re:?}` did not match:\n```\n{stderr}\n```" + ); + c_metadata.sort(); + c_metadata.join("\n") +} + #[cargo_test] fn host_config_rustflags_with_target() { // regression test for https://github.com/rust-lang/cargo/issues/10206 From 31a17b62ab86fdc2347288c756cbea74f6669385 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 5 Dec 2024 15:22:54 -0600 Subject: [PATCH 3/4] test(rustflags): Clarify remap tests The tests were comparing rustc vs RUSTFLAGS which was obscuring the case it was trying to test which was that different remaps shouldn't cause different results. --- tests/testsuite/rustflags.rs | 103 +++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 5 deletions(-) diff --git a/tests/testsuite/rustflags.rs b/tests/testsuite/rustflags.rs index 25c28bfa56b..195d7a07fea 100644 --- a/tests/testsuite/rustflags.rs +++ b/tests/testsuite/rustflags.rs @@ -1521,7 +1521,7 @@ fn remap_path_prefix_works() { } #[cargo_test] -fn remap_path_prefix_ignored() { +fn rustflags_remap_path_prefix_ignored_for_c_metadata() { let p = project().file("src/lib.rs", "").build(); let build_output = p @@ -1531,16 +1531,89 @@ fn remap_path_prefix_ignored() { "--remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo", ) .run(); - let build_c_metadata = dbg!(get_c_metadata(build_output)); + let first_c_metadata = dbg!(get_c_metadata(build_output)); p.cargo("clean").run(); - let rustc_output = p + let build_output = p + .cargo("build -v") + .env( + "RUSTFLAGS", + "--remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo", + ) + .run(); + let second_c_metadata = dbg!(get_c_metadata(build_output)); + + assert_data_eq!(first_c_metadata, second_c_metadata); +} + +#[cargo_test] +fn rustc_remap_path_prefix_ignored_for_c_metadata() { + let p = project().file("src/lib.rs", "").build(); + + let build_output = p + .cargo("rustc -v -- --remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo") + .run(); + let first_c_metadata = dbg!(get_c_metadata(build_output)); + + p.cargo("clean").run(); + + let build_output = p + .cargo("rustc -v -- --remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo") + .run(); + let second_c_metadata = dbg!(get_c_metadata(build_output)); + + assert_data_eq!(first_c_metadata, second_c_metadata); +} + +// `--remap-path-prefix` is meant to take two different binaries and make them the same but the +// rlib name, including `-Cextra-filename`, can still end up in the binary so it can't change +#[cargo_test] +fn rustflags_remap_path_prefix_ignored_for_c_extra_filename() { + let p = project().file("src/lib.rs", "").build(); + + let build_output = p + .cargo("build -v") + .env( + "RUSTFLAGS", + "--remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo", + ) + .run(); + let first_c_extra_filename = dbg!(get_c_extra_filename(build_output)); + + p.cargo("clean").run(); + + let build_output = p + .cargo("build -v") + .env( + "RUSTFLAGS", + "--remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo", + ) + .run(); + let second_c_extra_filename = dbg!(get_c_extra_filename(build_output)); + + assert_data_eq!(first_c_extra_filename, second_c_extra_filename); +} + +// `--remap-path-prefix` is meant to take two different binaries and make them the same but the +// rlib name, including `-Cextra-filename`, can still end up in the binary so it can't change +#[cargo_test] +fn rustc_remap_path_prefix_ignored_for_c_extra_filename() { + let p = project().file("src/lib.rs", "").build(); + + let build_output = p .cargo("rustc -v -- --remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo") .run(); - let rustc_c_metadata = dbg!(get_c_metadata(rustc_output)); + let first_c_extra_filename = dbg!(get_c_extra_filename(build_output)); + + p.cargo("clean").run(); + + let build_output = p + .cargo("rustc -v -- --remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo") + .run(); + let second_c_extra_filename = dbg!(get_c_extra_filename(build_output)); - assert_data_eq!(rustc_c_metadata, build_c_metadata); + assert_data_eq!(first_c_extra_filename, second_c_extra_filename); } fn get_c_metadata(output: RawOutput) -> String { @@ -1563,6 +1636,26 @@ fn get_c_metadata(output: RawOutput) -> String { c_metadata.join("\n") } +fn get_c_extra_filename(output: RawOutput) -> String { + let get_c_extra_filename_re = + regex::Regex::new(r".* (--crate-name [^ ]+).* (-C ?extra-filename=[^ ]+).*").unwrap(); + + let stderr = String::from_utf8(output.stderr).unwrap(); + let mut c_extra_filename = get_c_extra_filename_re + .captures_iter(&stderr) + .map(|c| { + let (_, [name, c_extra_filename]) = c.extract(); + format!("{name} {c_extra_filename}") + }) + .collect::>(); + assert!( + !c_extra_filename.is_empty(), + "`{get_c_extra_filename_re:?}` did not match:\n```\n{stderr}\n```" + ); + c_extra_filename.sort(); + c_extra_filename.join("\n") +} + #[cargo_test] fn host_config_rustflags_with_target() { // regression test for https://github.com/rust-lang/cargo/issues/10206 From ce74bced32339108ccc4c2cf7177d68d41c7dbbc Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 5 Dec 2024 16:02:06 -0600 Subject: [PATCH 4/4] docs(fingerprint): cargo-rustc extra flags do not affect the metadata As the prior test updates show, RUSTFLAGS and extra-flags have the same behavior: they don't affect `-Cextra-filename` or `-Cmetadata`. I also verified this by code inspection. I'm not sure why the table says this. --- src/cargo/core/compiler/fingerprint/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/core/compiler/fingerprint/mod.rs b/src/cargo/core/compiler/fingerprint/mod.rs index d994830c630..f4cb2e0c3e2 100644 --- a/src/cargo/core/compiler/fingerprint/mod.rs +++ b/src/cargo/core/compiler/fingerprint/mod.rs @@ -66,7 +66,7 @@ //! -------------------------------------------|-------------|---------- //! rustc | ✓ | ✓ //! [`Profile`] | ✓ | ✓ -//! `cargo rustc` extra args | ✓ | ✓ +//! `cargo rustc` extra args | ✓ | //! [`CompileMode`] | ✓ | ✓ //! Target Name | ✓ | ✓ //! `TargetKind` (bin/lib/etc.) | ✓ | ✓