Skip to content

Commit aba839c

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

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,39 @@ 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+
[profile.dev]
527+
trim-paths = ["diagnostics", "macro", "object"]
528+
"#,
529+
)
530+
.file("src/lib.rs", "")
531+
.file(
532+
"build.rs",
533+
r#"
534+
fn main() {{
535+
assert_eq!(
536+
std::env::var("CARGO_TRIM_PATHS").unwrap().as_str(),
537+
"diagnostics,macro,object"
538+
);
539+
}}
540+
"#,
541+
)
542+
.build();
543+
544+
p.cargo("build -v -Ztrim-paths")
545+
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
546+
.with_stderr_contains("[..]-Zremap-path-scope=diagnostics,macro,object[..]")
547+
.with_stderr_contains("[..]--remap-path-prefix[..]")
548+
.run();
549+
}

0 commit comments

Comments
 (0)