Skip to content

--remap-path-prefix FROM=TO creates invalid paths if TO doesn't start with / #137016

Open
@xobs

Description

@xobs

The documentation for rustc describes the --remap-path-prefix option as:

        --remap-path-prefix FROM=TO
                        Remap source names in all output (compiler messages
                        and output files)

This appears to only work if TO begins with /. Otherwise, it seems to sometimes miss the remapping.

For example, let's set up a test case with no remapping that only logs a message using the log crate:

$ mkdir remap-example
$ cd remap-example/
$ cargo init
    Creating binary (application) package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
$ cargo add -q log
$ echo 'fn main() { log::info!("Hello, world!"); }' > src/main.rs
$ cargo +nightly build
   Compiling log v0.4.25
   Compiling remap-example v0.1.0 (/opt/Xous/remap-example)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.30s
$ llvm-dwarfdump-15 --show-sources target/debug/remap-example | grep log
/home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/log-0.4.25/src/__private_api.rs
/home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/log-0.4.25/src/lib.rs
/rustc/a567209daab72b7ea59eac533278064396bb0534/library/core/src/num/int_log10.rs
$

If this is built with the replacement directory as example, it works:

$ RUSTFLAGS=--remap-path-prefix=/home/user/.cargo/registry/src=/example cargo +nightly
 build
   Compiling log v0.4.25
   Compiling remap-example v0.1.0 (/opt/Xous/remap-example)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.37s
$ llvm-dwarfdump-15 --show-sources target/debug/remap-example | grep log
/example/index.crates.io-1949cf8c6b5b557f/log-0.4.25/src/__private_api.rs
/example/index.crates.io-1949cf8c6b5b557f/log-0.4.25/src/lib.rs
/rustc/a567209daab72b7ea59eac533278064396bb0534/library/core/src/num/int_log10.rs
$

However, if the replacement path becomes example (without the leading /) it does something funny:

$ RUSTFLAGS=--remap-path-prefix=/home/user/.cargo/registry/src=example cargo +nightly build
   Compiling log v0.4.25
   Compiling remap-example v0.1.0 (/opt/Xous/remap-example)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.32s
$ llvm-dwarfdump-15 --show-sources target/debug/remap-example | grep log
/opt/Xous/remap-example/example/index.crates.io-1949cf8c6b5b557f/log-0.4.25/src/__private_api.rs
/opt/Xous/remap-example/example/index.crates.io-1949cf8c6b5b557f/log-0.4.25/src/lib.rs
/rustc/a567209daab72b7ea59eac533278064396bb0534/library/core/src/num/int_log10.rs
example/index.crates.io-1949cf8c6b5b557f/log-0.4.25/src/__private_api.rs
example/index.crates.io-1949cf8c6b5b557f/log-0.4.25/src/lib.rs
$

Notice how it created a second copy of the entries in the current directory, in addition to performing the replacement.

Meta

rustc --version --verbose:

rustc 1.86.0-nightly (a567209da 2025-02-13)
binary: rustc
commit-hash: a567209daab72b7ea59eac533278064396bb0534
commit-date: 2025-02-13
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-debuginfoArea: Debugging information in compiled programs (DWARF, PDB, etc.)A-path-remappingArea: path remapping, --remap-path-prefix, --remap-cwd-prefix, --remap-diagnostics-scope etc.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions