Skip to content

Commit 54ab1dd

Browse files
committed
doc(trim-paths): unstable doc
1 parent 9bf67a1 commit 54ab1dd

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

src/doc/src/reference/unstable.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
* [Edition 2024](#edition-2024) — Adds support for the 2024 Edition.
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.
@@ -1289,6 +1290,96 @@ edition that may break your build.
12891290

12901291
[edition]: ../../edition-guide/index.html
12911292

1293+
## Profile `trim-paths` option
1294+
1295+
* Tracking Issue: [rust-lang/cargo#12137](https://github.com/rust-lang/cargo/issues/12137)
1296+
* Tracking Rustc Issue: [rust-lang/rust#111540](https://github.com/rust-lang/rust/issues/111540)
1297+
1298+
This adds a new profile setting to control how paths are sanitised in the resulting binary.
1299+
This can be enabled like so:
1300+
1301+
```toml
1302+
cargo-features = ["trim-paths"]
1303+
1304+
[package]
1305+
# ...
1306+
1307+
[profile.release]
1308+
trim-paths = ["all"]
1309+
```
1310+
1311+
To set this in a profile in Cargo configuration, you need to use either
1312+
`-Z trim-paths` or `[unstable]` table to enable it. For example,
1313+
1314+
```toml
1315+
# .cargo/config.toml
1316+
[unstable]
1317+
trim-paths = true
1318+
1319+
[profile.release]
1320+
rustflags = ["diagnostics"]
1321+
```
1322+
1323+
### Documentation updates
1324+
1325+
#### trim-paths
1326+
1327+
*as a new ["Profiles settings" entry](./profiles.html#profile-settings)*
1328+
1329+
`trim-paths` is a profile setting which enables and controls the sanitisation of file paths in build outputs.
1330+
It takes either string or an array of the following values:
1331+
1332+
- `"none"` and `false` --- disable path sanitisation
1333+
- `"macro"` --- sanitise paths in the expansion of `std::file!()` macro.
1334+
This is where paths in embedded panic messages come from
1335+
- `"diagnostics"` --- sanitise paths in printed compiler diagnostics
1336+
- `"object"` --- sanitise paths in compiled executables or libraries
1337+
- `"all"` and `true` --- sanitise paths in all possible locations
1338+
1339+
It is defaulted to `none` for the `dev` profile, and `object` for the `release` profile.
1340+
You can manually override it by specifying this option in `Cargo.toml`:
1341+
1342+
```toml
1343+
[profile.dev]
1344+
trim-paths = "all"
1345+
1346+
[profile.release]
1347+
trim-paths = ["object", "diagnostics"]
1348+
```
1349+
1350+
The default `release` profile setting (`object`) sanitises only the paths in emitted executable or library files.
1351+
It always affects paths from macros such as panic messages, and in debug information only if they will be embedded together with the binary
1352+
(the default on platforms with ELF binaries, such as Linux and windows-gnu),
1353+
but will not touch them if they are in separate files (the default on Windows MSVC and macOS).
1354+
But the paths to these separate files are sanitised.
1355+
1356+
If `trim-paths` is not `none` or `false`, then the following paths are sanitised if they appear in a selected scope:
1357+
1358+
1. Path to the source files of the standard and core library (sysroot) will begin with `/rustc/[rustc commit hash]`,
1359+
e.g. `/home/username/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs` ->
1360+
`/rustc/fe72845f7bb6a77b9e671e6a4f32fe714962cec4/library/core/src/result.rs`
1361+
2. Path to the current package will be stripped, e.g. `/home/username/crate/src/lib.rs` -> `src/lib.rs`.
1362+
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`
1363+
1364+
When a path to the source files of the standard and core library is *not* in scope for sanitisation,
1365+
the emitted path will depend on if `rust-src` component is present.
1366+
If it is, then some paths will point to the copy of the source files on your file system;
1367+
if it isn't, then they will show up as `/rustc/[rustc commit hash]/library/...`
1368+
(just like when it is selected for sanitisation).
1369+
Paths to all other source files will not be affected.
1370+
1371+
This will not affect any hard-coded paths in the source code, such as in strings.
1372+
1373+
#### Environment variable
1374+
1375+
*as a new entry of ["Environment variables Cargo sets for build scripts"](./environment-variables.md#environment-variables-cargo-sets-for-crates)*
1376+
1377+
* `CARGO_TRIM_PATHS` --- The value of `trim-paths` profile option.
1378+
If the build script introduces absolute paths to built artefacts (such as by invoking a compiler),
1379+
the user may request them to be sanitised in different types of artefacts.
1380+
Common paths requiring sanitisation include `OUT_DIR` and `CARGO_MANIFEST_DIR`,
1381+
plus any other introduced by the build script, such as include directories.
1382+
12921383
# Stabilized and removed features
12931384

12941385
## Compile progress

0 commit comments

Comments
 (0)