How to properly use --remap-path-prefix?

I want to strip references to the build directory and the general setup on the computer that build the binary. As I understand it, I'm supposed to use --remap-path-prefix for this. This only works partially:

In my script calling cargo I tried to use:

RUSTFLAGS="--remap-path-prefix $(pwd)=/build/" cargo build --release

That stripped references to the path of the source tree, however using the strings command on the binary reveals there are still references to /home/<username>/.cargo/registry/src/[...] (these seem to be panic messages).

  1. What is the proper way to deal with those?
  2. And how do I do so in a cross platform way that will work in CI? As far as I know the path will be different on Windows, Linux and MacOS for example.
  3. Is there some easy way to discover the path to the registry?

You can pass multiple of these args.

Thanks, but how do I figure out the (platform dependant?) path to the cargo registry where the dependencies are?

The registry can be found in .cargo in the home dir by default, but this dir can be moved using the CARGO_HOME env var.

Thanks. I believe it is needlessly complicated to set this up (especially if you care about cross-platform), but with that information I see a solution.

I have run into another issue though with this, related to paths with spaces in them. It doesn't seem possible to properly quote the paths in RUSTFLAGS so when they contain spaces. This is rarely an issue on Linux, but on Windows such paths do come up in user names sometimes.

There is a cargo feature in the work to make this a lot easier.

You can use CARGO_ENCODED_RUSTFLAGS instead which uses ASCII Unit Separator (0x1f) instead of space as argument separator. You can also use the new --config option to set the build.rustflags config to an array formatted as toml.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.