Skip to content

Commit decd020

Browse files
committed
doc(trim-paths): unstable doc
1 parent d1830f6 commit decd020

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

src/doc/src/reference/unstable.md

Lines changed: 84 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.
@@ -1286,6 +1287,89 @@ edition that may break your build.
12861287

12871288
[edition]: ../../edition-guide/index.html
12881289

1290+
## Profile `trim-paths` option
1291+
1292+
* Tracking Issue: [rust-lang/cargo#12137](https://github.com/rust-lang/cargo/issues/12137)
1293+
* Tracking Rustc Issue: [rust-lang/rust#111540](https://github.com/rust-lang/rust/issues/111540)
1294+
1295+
This adds a new profile setting to control how paths are sanitised in the resulting binary.
1296+
This can be enabled like so:
1297+
1298+
```toml
1299+
cargo-features = ["trim-paths"]
1300+
1301+
[package]
1302+
# ...
1303+
1304+
[profile.release]
1305+
trim-paths = ["diagnostics", "object"]
1306+
```
1307+
1308+
To set this in a profile in Cargo configuration,
1309+
you need to use either `-Z trim-paths` or `[unstable]` table to enable it.
1310+
For example,
1311+
1312+
```toml
1313+
# .cargo/config.toml
1314+
[unstable]
1315+
trim-paths = true
1316+
1317+
[profile.release]
1318+
trim-paths = ["diagnostics", "object"]
1319+
```
1320+
1321+
### Documentation updates
1322+
1323+
#### trim-paths
1324+
1325+
*as a new ["Profiles settings" entry](./profiles.html#profile-settings)*
1326+
1327+
`trim-paths` is a profile setting which enables and controls the sanitization of file paths in build outputs.
1328+
It takes the following values:
1329+
1330+
- `"none"` and `false` --- disable path sanitization
1331+
- `"macro"` --- sanitize paths in the expansion of `std::file!()` macro.
1332+
This is where paths in embedded panic messages come from
1333+
- `"diagnostics"` --- sanitize paths in printed compiler diagnostics
1334+
- `"object"` --- sanitize paths in compiled executables or libraries
1335+
- `"all"` and `true` --- sanitize paths in all possible locations
1336+
1337+
It also takes an array with the combinations of `"macro"`, `"diagnostics"`, and `"object"`.
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`) sanitizes 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 sanitized.
1355+
1356+
If `trim-paths` is not `none` or `false`, then the following paths are sanitized 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, relatively to the current workspace root, 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 sanitization,
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 sanitization).
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+
12891373
# Stabilized and removed features
12901374

12911375
## Compile progress

0 commit comments

Comments
 (0)