rust-lang / rust-lang/rust

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

Open
#137,016 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-path-remapping C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with rustc's --remap-path-prefix and the provided RUSTFLAGS commands, then inspect the compiler code handling that option. Use llvm-dwarfdump --show-sources to compare absolute and relative replacements; done means a relative TO does not produce duplicate or invalid paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.