Skip to content

Commit dd0aea3

Browse files
committed
feat(trim-paths): set env CARGO_TRIM_PATHS for build scripts
1 parent 4aee12c commit dd0aea3

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
307307
cmd.env("CARGO_MANIFEST_LINKS", links);
308308
}
309309

310+
if let Some(trim_paths) = unit.profile.trim_paths.as_ref() {
311+
cmd.env("CARGO_TRIM_PATHS", trim_paths.to_string());
312+
}
313+
310314
// Be sure to pass along all enabled features for this package, this is the
311315
// last piece of statically known information that we have.
312316
for feat in &unit.features {

src/cargo/core/profiles.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ impl Profiles {
327327
result.root = for_unit_profile.root;
328328
result.debuginfo = for_unit_profile.debuginfo;
329329
result.opt_level = for_unit_profile.opt_level;
330+
result.trim_paths = for_unit_profile.trim_paths.clone();
330331
result
331332
}
332333

tests/testsuite/profile_trim_paths.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,69 @@ fn object_works() {
511511
assert!(memchr::memmem::find(&stdout, registry_src_bytes).is_none());
512512
assert!(memchr::memmem::find(&stdout, pkg_root).is_none());
513513
}
514+
515+
// TODO: might want to move to test/testsuite/build_script.rs once stabilized.
516+
#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
517+
fn custom_build_env_var_trim_paths() {
518+
let p = project()
519+
.file(
520+
"Cargo.toml",
521+
r#"
522+
[package]
523+
name = "foo"
524+
version = "0.0.1"
525+
"#,
526+
)
527+
.file("src/lib.rs", "")
528+
.file("build.rs", "")
529+
.build();
530+
531+
let test_cases = [
532+
("[]", "none"),
533+
("\"all\"", "all"),
534+
("\"diagnostics\"", "diagnostics"),
535+
("\"macro\"", "macro"),
536+
("\"none\"", "none"),
537+
("\"object\"", "object"),
538+
("false", "none"),
539+
("true", "all"),
540+
(
541+
r#"["diagnostics", "macro", "object"]"#,
542+
"diagnostics,macro,object",
543+
),
544+
];
545+
546+
for (opts, expected) in test_cases {
547+
p.change_file(
548+
"Cargo.toml",
549+
&format!(
550+
r#"
551+
[package]
552+
name = "foo"
553+
version = "0.0.1"
554+
555+
[profile.dev]
556+
trim-paths = {opts}
557+
"#
558+
),
559+
);
560+
561+
p.change_file(
562+
"build.rs",
563+
&format!(
564+
r#"
565+
fn main() {{
566+
assert_eq!(
567+
std::env::var("CARGO_TRIM_PATHS").unwrap().as_str(),
568+
"{expected}",
569+
);
570+
}}
571+
"#
572+
),
573+
);
574+
575+
p.cargo("build -Ztrim-paths")
576+
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
577+
.run();
578+
}
579+
}

0 commit comments

Comments
 (0)