@@ -94,6 +94,7 @@ For the latest nightly, see the [nightly version] of this page.
9494 * [ per-package-target] ( #per-package-target ) --- Sets the ` --target ` to use for each individual package.
9595 * [ artifact dependencies] ( #artifact-dependencies ) --- Allow build artifacts to be included into other build artifacts and build them for different targets.
9696 * [ ` [lints] ` ] ( #lints ) --- Configure lint levels for various linter tools.
97+ * [ Profile ` trim-paths ` option] ( #profile-trim-paths-option ) --- Control the sanitisation of file paths in build outputs.
9798* Information and metadata
9899 * [ Build-plan] ( #build-plan ) --- Emits JSON information on which commands will be run.
99100 * [ unit-graph] ( #unit-graph ) --- Emits JSON for Cargo's internal graph structure.
@@ -1665,6 +1666,96 @@ version = "0.1.0"
16651666workspace = true
16661667```
16671668
1669+ ## Profile `trim-paths` option
1670+
1671+ * Tracking Issue: [rust-lang/cargo#12137](https://github.com/rust-lang/cargo/issues/12137)
1672+ * Tracking Rustc Issue: [rust-lang/rust#111540](https://github.com/rust-lang/rust/issues/111540)
1673+
1674+ This adds a new profile setting to control how paths are sanitised in the resulting binary.
1675+ This can be enabled like so:
1676+
1677+ ```toml
1678+ cargo-features = ["trim-paths"]
1679+
1680+ [package]
1681+ # ...
1682+
1683+ [profile.release]
1684+ trim-paths = ["all"]
1685+ ```
1686+
1687+ To set this in a profile in Cargo configuration, you need to use either
1688+ `-Z trim-paths` or `[unstable]` table to enable it. For example,
1689+
1690+ ```toml
1691+ # .cargo/config.toml
1692+ [unstable]
1693+ trim-paths = true
1694+
1695+ [profile.release]
1696+ rustflags = ["diagnostics"]
1697+ ```
1698+
1699+ ### Documentation updates
1700+
1701+ #### trim-paths
1702+
1703+ *as a new ["Profiles settings" entry](./profiles.html#profile-settings)*
1704+
1705+ `trim-paths` is a profile setting which enables and controls the sanitisation of file paths in build outputs.
1706+ It takes an array of the following values:
1707+
1708+ - `"none"` and `false` --- disable path sanitisation
1709+ - `"macro"` --- sanitise paths in the expansion of `std::file!()` macro.
1710+ This is where paths in embedded panic messages come from
1711+ - `"diagnostics"` --- sanitise paths in printed compiler diagnostics
1712+ - `"object"` --- sanitise paths in compiled executables or libraries
1713+ - `"all"` and `true` --- sanitise paths in all possible locations
1714+
1715+ It is defaulted to `none` for the `dev` profile, and `object` for the `release` profile.
1716+ You can manually override it by specifying this option in `Cargo.toml`:
1717+
1718+ ```toml
1719+ [profile.dev]
1720+ trim-paths = ["all"]
1721+
1722+ [profile.release]
1723+ trim-paths = ["none"]
1724+ ```
1725+
1726+ The default `release` profile setting (`object`) sanitises only the paths in emitted executable or library files.
1727+ It always affects paths from macros such as panic messages, and in debug information only if they will be embedded together with the binary
1728+ (the default on platforms with ELF binaries, such as Linux and windows-gnu),
1729+ but will not touch them if they are in separate files (the default on Windows MSVC and macOS).
1730+ But the paths to these separate files are sanitised.
1731+
1732+ If `trim-paths` is not `none` or `false`, then the following paths are sanitised if they appear in a selected scope:
1733+
1734+ 1. Path to the source files of the standard and core library (sysroot) will begin with `/rustc/[rustc commit hash]`,
1735+ e.g. `/home/username/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs` ->
1736+ `/rustc/fe72845f7bb6a77b9e671e6a4f32fe714962cec4/library/core/src/result.rs`
1737+ 2. Path to the current package will be stripped, e.g. `/home/username/crate/src/lib.rs` -> `src/lib.rs`.
1738+ 3. Path to dependency packages will be replaced with `[package name]-[version]`. E.g. `/home/username/deps/foo/src/lib.rs` -> `foo-0.1.0/src/lib.rs`
1739+
1740+ When a path to the source files of the standard and core library is *not* in scope for sanitisation,
1741+ the emitted path will depend on if `rust-src` component is present.
1742+ If it is, then some paths will point to the copy of the source files on your file system;
1743+ if it isn' t, then they will show up as ` /rustc/[rustc commit hash]/library/...`
1744+ (just like when it is selected for sanitisation).
1745+ Paths to all other source files will not be affected.
1746+
1747+ This will not affect any hard- coded paths in the source code, such as in strings.
1748+
1749+ #### Environment variable
1750+
1751+ * as a new entry of [" Environment variables Cargo sets for build scripts" ](./ environment- variables .md #environment- variables- cargo- sets- for - crates)*
1752+
1753+ * ` CARGO_TRIM_PATHS` -- - The value of ` trim-paths` profile option.
1754+ If the build script introduces absolute paths to built artefacts (such as by invoking a compiler),
1755+ the user may request them to be sanitised in different types of artefacts.
1756+ Common paths requiring sanitisation include ` OUT_DIR` and ` CARGO_MANIFEST_DIR` ,
1757+ plus any other introduced by the build script, such as include directories.
1758+
16681759# Stabilized and removed features
16691760
16701761## Compile progress
0 commit comments