Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/bootstrap/src/bin/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,11 @@ fn main() {
}
}

if let Ok(map) = env::var("RUSTC_DEBUGINFO_MAP") {
cmd.arg("--remap-path-prefix").arg(&map);
// The remap flags for the compiler and standard library sources.
if let Ok(maps) = env::var("RUSTC_DEBUGINFO_MAP") {
for map in maps.split('\t') {
cmd.arg("--remap-path-prefix").arg(map);
}
}
// The remap flags for Cargo registry sources need to be passed after the remapping for the
// Rust source code directory, to handle cases when $CARGO_HOME is inside the source directory.
Expand Down
26 changes: 23 additions & 3 deletions src/bootstrap/src/core/builder/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,15 +1025,26 @@ impl Builder<'_> {
if let Some(ref map_to) =
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
{
// Tell the compiler which prefix was used for remapping the standard library
cargo.env("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR", map_to);
}

if let Some(ref map_to) =
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::Compiler)
{
// When building compiler sources, we want to apply the compiler remap scheme.
cargo.env("RUSTC_DEBUGINFO_MAP", format!("compiler/={map_to}/compiler"));
// Tell the compiler which prefix was used for remapping the compiler it-self
cargo.env("CFG_VIRTUAL_RUSTC_DEV_SOURCE_BASE_DIR", map_to);

// When building compiler sources, we want to apply the compiler remap scheme.
let map = [
// Cargo use relative paths for workspace members, so let's remap those.
format!("compiler/={map_to}/compiler"),
// rustc creates absolute paths (in part bc of the `rust-src` unremap
// and for working directory) so let's remap the build directory as well.
format!("{}={map_to}", self.build.src.display()),
]
.join("\t");
cargo.env("RUSTC_DEBUGINFO_MAP", map);
}
}
Mode::Std
Expand All @@ -1044,7 +1055,16 @@ impl Builder<'_> {
if let Some(ref map_to) =
self.build.debuginfo_map_to(GitRepo::Rustc, RemapScheme::NonCompiler)
{
cargo.env("RUSTC_DEBUGINFO_MAP", format!("library/={map_to}/library"));
// When building the standard library sources, we want to apply the std remap scheme.
let map = [
// Cargo use relative paths for workspace members, so let's remap those.
format!("library/={map_to}/library"),
// rustc creates absolute paths (in part bc of the `rust-src` unremap
// and for working directory) so let's remap the build directory as well.
format!("{}={map_to}", self.build.src.display()),
]
.join("\t");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: technically, I think filename parts of Windows(?)/Linux(?) paths can actually contain tabs, however, other tools have the tendency to also break on those, so I'm not worried about that possibility...

Copy link
Member

@ChrisDenton ChrisDenton Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, paths can contain any character on platforms that use unix paths. Well except \0.

cargo.env("RUSTC_DEBUGINFO_MAP", map);
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/run-make/remap-path-prefix-std/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This test makes sure that we do not leak paths to the checkout
// (ie. /checkout in CI) in the distributed `libstd` debuginfo.
//
// This test only runs on Linux and dist builder (or with `rust.remap-debuginfo = true`
// set in your `bootstrap.toml`).

//@ needs-std-remap-debuginfo
//@ only-linux

use std::path::PathBuf;

use run_make_support::{llvm_dwarfdump, rfs, rustc, shallow_find_files, source_root};

fn main() {
// Find the target libdir for the current target
let target_libdir = {
let output = rustc().print("target-libdir").run();
let stdout = output.stdout_utf8();
let path = PathBuf::from(stdout.trim());

// Assert that the target-libdir path exists
assert!(path.exists(), "target-libdir: {path:?} does not exists");

path
};

// Find all the `libstd-.*.rlib` files under the libdir
let libstd_rlibs = shallow_find_files(&target_libdir, |p| {
if let Some(filename) = p.file_name()
&& let filename = filename.to_string_lossy()
{
filename.starts_with("libstd-") && filename.ends_with(".rlib")
} else {
false
}
});

// Assert that there is only one rlib for the `libstd`
let [libstd_rlib] = &libstd_rlibs[..] else {
unreachable!("multiple libstd rlib: {libstd_rlibs:?} in {target_libdir:?}");
};

// Symlink the libstd rlib here to avoid absolute paths from llvm-dwarfdump own output
// and not from the debuginfo it-self
rfs::symlink_file(libstd_rlib, "libstd.rlib");

// Check that there is only `/rustc/` paths and no `/checkout`, `/home`, or whatever
llvm_dwarfdump()
.input("libstd.rlib")
.run()
.assert_stdout_contains("/rustc/")
.assert_stdout_not_contains(source_root().to_string_lossy());
}
Loading