-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement RFC 3127 -Ztrim-paths
#12625
Changes from all commits
decd020
28b169b
4d29af1
08c5e35
f4a1a03
fd893b2
557fc4f
63cef2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,7 @@ use crate::util::errors::{CargoResult, VerboseError}; | |
use crate::util::interning::InternedString; | ||
use crate::util::machine_message::{self, Message}; | ||
use crate::util::toml::TomlDebugInfo; | ||
use crate::util::toml::TomlTrimPaths; | ||
use crate::util::{add_path_args, internal, iter_join_onto, profile}; | ||
use cargo_util::{paths, ProcessBuilder, ProcessError}; | ||
use rustfix::diagnostics::Applicability; | ||
|
@@ -950,6 +951,7 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit) | |
incremental, | ||
strip, | ||
rustflags: profile_rustflags, | ||
trim_paths, | ||
.. | ||
} = unit.profile.clone(); | ||
let test = unit.mode.is_any_test(); | ||
|
@@ -1028,6 +1030,10 @@ fn build_base_args(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder, unit: &Unit) | |
} | ||
} | ||
|
||
if let Some(trim_paths) = trim_paths { | ||
trim_paths_args(cmd, cx, unit, &trim_paths)?; | ||
} | ||
|
||
cmd.args(unit.pkg.manifest().lint_rustflags()); | ||
cmd.args(&profile_rustflags); | ||
if let Some(args) = cx.bcx.extra_args_for(unit) { | ||
|
@@ -1162,6 +1168,74 @@ fn features_args(unit: &Unit) -> Vec<OsString> { | |
args | ||
} | ||
|
||
/// Generates the `--remap-path-scope` and `--remap-path-prefix` for [RFC 3127]. | ||
/// See also unstable feature [`-Ztrim-paths`]. | ||
/// | ||
/// [RFC 3127]: https://rust-lang.github.io/rfcs/3127-trim-paths.html | ||
/// [`-Ztrim-paths`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#profile-trim-paths-option | ||
fn trim_paths_args( | ||
cmd: &mut ProcessBuilder, | ||
cx: &Context<'_, '_>, | ||
unit: &Unit, | ||
trim_paths: &TomlTrimPaths, | ||
) -> CargoResult<()> { | ||
if trim_paths.is_none() { | ||
return Ok(()); | ||
} | ||
|
||
// feature gate was checked during mainfest/config parsing. | ||
cmd.arg("-Zunstable-options"); | ||
cmd.arg(format!("-Zremap-path-scope={trim_paths}")); | ||
|
||
let sysroot_remap = { | ||
let sysroot = &cx.bcx.target_data.info(unit.kind).sysroot; | ||
let mut remap = OsString::from("--remap-path-prefix="); | ||
remap.push(sysroot); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the correct remap prefix for sysroot, and how to setup a test case for it? I've checked rustup, Debian, and Fedora. All of them put the rust-src under @Urgau do you have any insight? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Well you could have a Rust There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the tip! Unfortunately I've tried
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hum, the mapping is done by Or maybe instead of disabling it you could do the inverse and demap in debug? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The desired behavior is mentioned in the RFC
To follow the RFC, yes, we might want to disable it in bootstrap. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Me and Urgau had a chat on this. Urgau pointed out changing the bootstrap this may require |
||
remap.push("/lib/rustlib/src/rust"); // See also `detect_sysroot_src_path()`. | ||
remap.push("="); | ||
remap.push("/rustc/"); | ||
// This remap logic aligns with rustc: | ||
// <https://github.com/rust-lang/rust/blob/c2ef3516/src/bootstrap/src/lib.rs#L1113-L1116> | ||
if let Some(commit_hash) = cx.bcx.rustc().commit_hash.as_ref() { | ||
remap.push(commit_hash); | ||
} else { | ||
remap.push(cx.bcx.rustc().version.to_string()); | ||
} | ||
remap | ||
}; | ||
cmd.arg(sysroot_remap); | ||
|
||
let package_remap = { | ||
let pkg_root = unit.pkg.root(); | ||
let ws_root = cx.bcx.ws.root(); | ||
let is_local = unit.pkg.package_id().source_id().is_path(); | ||
let mut remap = OsString::from("--remap-path-prefix="); | ||
// Remapped to path relative to workspace root: | ||
// | ||
// * path dependencies under workspace root directory | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The RFC only mentions about mapping the current package, which doesn't take workspaces into account:
Here we expand the RFC a bit: always remap from the workspace root to empty string. When dealing with compilations, Cargo always handle the workspace as a whole instead of a single member. We might not want an exception for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me, the main question of whether to be relative to workspace or package root is if there'd be ambiguity. Since we have name+version, I'm guessing not. |
||
// | ||
// Remapped to `<pkg>-<version>` | ||
// | ||
// * registry dependencies | ||
// * git dependencies | ||
// * path dependencies outside workspace root directory | ||
if is_local && pkg_root.strip_prefix(ws_root).is_ok() { | ||
remap.push(ws_root); | ||
remap.push("="); // empty to remap to relative paths. | ||
} else { | ||
remap.push(pkg_root); | ||
remap.push("="); | ||
remap.push(unit.pkg.name()); | ||
remap.push("-"); | ||
remap.push(unit.pkg.version().to_string()); | ||
} | ||
remap | ||
}; | ||
cmd.arg(package_remap); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Generates the `--check-cfg` arguments for the `unit`. | ||
/// See unstable feature [`check-cfg`]. | ||
/// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also be included in rustdoc to handle the diagnostics stripping (and same with doctests)?
(Though I still don't 100% understand the purpose of diagnostic remapping.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there is such an option for
rustdoc
. Did I miss something?Maybe this could be put in "unresolved questions" in RFC 3127 tracking issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, leaving that as an open issue sounds good. The rustdoc argument parser would just need to be updated.
Though, before that I would like to better understand why the diagnostic option exists and when someone would want to turn it on. I can't really follow the discussion from rust-lang/rust#87745 and how that relates to cargo. If we end up not stabilizing "diagnostic" as an option, then I don't have any motivation to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the
diagnostics
option I think it's related to build-systems that create VFS (Virtual File System) or temporary location when building, throwing the all diagnostics path to the wrong location.