From 0a752c61e7ed127559157ad1fc8f8d6502365071 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Tue, 30 Jul 2024 20:49:49 -0400 Subject: [PATCH 1/3] refactor: extract remap rules into functions --- src/cargo/core/compiler/mod.rs | 125 +++++++++++++++++---------------- 1 file changed, 64 insertions(+), 61 deletions(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index fe07c59945d..1122fcddcb1 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -1242,73 +1242,76 @@ fn trim_paths_args( cmd.arg("-Zunstable-options"); cmd.arg(format!("-Zremap-path-scope={trim_paths}")); - let sysroot_remap = { - let sysroot = &build_runner.bcx.target_data.info(unit.kind).sysroot; - let mut remap = OsString::from("--remap-path-prefix="); - remap.push(sysroot); - remap.push("/lib/rustlib/src/rust"); // See also `detect_sysroot_src_path()`. - remap.push("="); - remap.push("/rustc/"); - // This remap logic aligns with rustc: - // - if let Some(commit_hash) = build_runner.bcx.rustc().commit_hash.as_ref() { - remap.push(commit_hash); - } else { - remap.push(build_runner.bcx.rustc().version.to_string()); - } - remap - }; - let package_remap = { - let pkg_root = unit.pkg.root(); - let ws_root = build_runner.bcx.ws.root(); - let mut remap = OsString::from("--remap-path-prefix="); - // Remap rules for dependencies - // - // * Git dependencies: remove ~/.cargo/git/checkouts prefix. - // * Registry dependencies: remove ~/.cargo/registry/src prefix. - // * Others (e.g. path dependencies): - // * relative paths to workspace root if inside the workspace directory. - // * otherwise remapped to `-`. - let source_id = unit.pkg.package_id().source_id(); - if source_id.is_git() { - remap.push( - build_runner - .bcx - .gctx - .git_checkouts_path() - .as_path_unlocked(), - ); - remap.push("="); - } else if source_id.is_registry() { - remap.push( - build_runner - .bcx - .gctx - .registry_source_path() - .as_path_unlocked(), - ); - remap.push("="); - } else if pkg_root.strip_prefix(ws_root).is_ok() { - remap.push(ws_root); - remap.push("=."); // remap to relative rustc work dir explicitly - } else { - remap.push(pkg_root); - remap.push("="); - remap.push(unit.pkg.name()); - remap.push("-"); - remap.push(unit.pkg.version().to_string()); - } - remap - }; - // Order of `--remap-path-prefix` flags is important for `-Zbuild-std`. // We want to show `/rustc//library/std` instead of `std-0.0.0`. - cmd.arg(package_remap); - cmd.arg(sysroot_remap); + cmd.arg(package_remap(build_runner, unit)); + cmd.arg(sysroot_remap(build_runner, unit)); Ok(()) } +/// Path prefix remap rules for sysroot. +/// +/// This remap logic aligns with rustc: +/// +fn sysroot_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString { + let sysroot = &build_runner.bcx.target_data.info(unit.kind).sysroot; + let mut remap = OsString::from("--remap-path-prefix="); + remap.push(sysroot); + remap.push("/lib/rustlib/src/rust"); // See also `detect_sysroot_src_path()`. + remap.push("="); + remap.push("/rustc/"); + if let Some(commit_hash) = build_runner.bcx.rustc().commit_hash.as_ref() { + remap.push(commit_hash); + } else { + remap.push(build_runner.bcx.rustc().version.to_string()); + } + remap +} + +/// Path prefix remap rules for dependencies. +/// +/// * Git dependencies: remove `~/.cargo/git/checkouts` prefix. +/// * Registry dependencies: remove `~/.cargo/registry/src` prefix. +/// * Others (e.g. path dependencies): +/// * relative paths to workspace root if inside the workspace directory. +/// * otherwise remapped to `-`. +fn package_remap(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> OsString { + let pkg_root = unit.pkg.root(); + let ws_root = build_runner.bcx.ws.root(); + let mut remap = OsString::from("--remap-path-prefix="); + let source_id = unit.pkg.package_id().source_id(); + if source_id.is_git() { + remap.push( + build_runner + .bcx + .gctx + .git_checkouts_path() + .as_path_unlocked(), + ); + remap.push("="); + } else if source_id.is_registry() { + remap.push( + build_runner + .bcx + .gctx + .registry_source_path() + .as_path_unlocked(), + ); + remap.push("="); + } else if pkg_root.strip_prefix(ws_root).is_ok() { + remap.push(ws_root); + remap.push("=."); // remap to relative rustc work dir explicitly + } else { + remap.push(pkg_root); + remap.push("="); + remap.push(unit.pkg.name()); + remap.push("-"); + remap.push(unit.pkg.version().to_string()); + } + remap +} + /// Generates the `--check-cfg` arguments for the `unit`. fn check_cfg_args(unit: &Unit) -> CargoResult> { // The routine below generates the --check-cfg arguments. Our goals here are to From 33a800caca19a7e382701247cd12a579c4cdcc85 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 12 Aug 2024 13:33:57 -0400 Subject: [PATCH 2/3] test: show `trim-paths=diagnostics` doesnt works for rustdoc It should be working with the next commit. --- tests/testsuite/profile_trim_paths.rs | 85 +++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/tests/testsuite/profile_trim_paths.rs b/tests/testsuite/profile_trim_paths.rs index 83740076ffe..720dd371802 100644 --- a/tests/testsuite/profile_trim_paths.rs +++ b/tests/testsuite/profile_trim_paths.rs @@ -752,3 +752,88 @@ Hello, Ferris! "#]], ); } + +#[cargo_test(nightly, reason = "rustdoc --remap-path-prefix is unstable")] +fn rustdoc_without_diagnostics_scope() { + Package::new("bar", "0.0.1") + .file("Cargo.toml", &basic_manifest("bar", "0.0.1")) + .file( + "src/lib.rs", + r#" + /// + pub struct Bar; + "#, + ) + .publish(); + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + + [dependencies] + bar = "0.0.1" + + [profile.dev] + trim-paths = "object" + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("doc -vv -Ztrim-paths") + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) + .with_stderr_data(str![[r#" +... +[WARNING] unopened HTML tag `script` + --> [ROOT]/home/.cargo/registry/src/-[HASH]/bar-0.0.1/src/lib.rs:2:17 +... +"#]]) + .run(); +} + +#[cargo_test(nightly, reason = "rustdoc --remap-path-prefix is unstable")] +fn rustdoc_diagnostics_works() { + // This is expected to work after rust-lang/rust#128736 + Package::new("bar", "0.0.1") + .file("Cargo.toml", &basic_manifest("bar", "0.0.1")) + .file( + "src/lib.rs", + r#" + /// + pub struct Bar; + "#, + ) + .publish(); + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + + [dependencies] + bar = "0.0.1" + + [profile.dev] + trim-paths = "diagnostics" + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("doc -vv -Ztrim-paths") + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) + .with_stderr_data(str![[r#" +... +[WARNING] unopened HTML tag `script` + --> [ROOT]/home/.cargo/registry/src/-[HASH]/bar-0.0.1/src/lib.rs:2:17 +... +"#]]) + .run(); +} From b8a3dbebd5191dcbdc7cb1827b6555b230e84473 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 9 Aug 2024 08:52:43 -0400 Subject: [PATCH 3/3] feat(trim-paths): rustdoc supports trim-paths for diagnostics This enables rustdoc invocation from `cargo doc` to trim paths in diagnostics. Doc tests are out-of-scope, as they are handled in a different mechanism, which needs more cares. --- src/cargo/core/compiler/mod.rs | 31 +++++++++++++++++++++++++++ tests/testsuite/profile_trim_paths.rs | 4 +++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 1122fcddcb1..7c9a8901e28 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -97,6 +97,7 @@ use crate::util::{add_path_args, internal}; use cargo_util::{paths, ProcessBuilder, ProcessError}; use cargo_util_schemas::manifest::TomlDebugInfo; use cargo_util_schemas::manifest::TomlTrimPaths; +use cargo_util_schemas::manifest::TomlTrimPathsValue; use rustfix::diagnostics::Applicability; const RUSTDOC_CRATE_VERSION_FLAG: &str = "--crate-version"; @@ -737,6 +738,10 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu add_error_format_and_color(build_runner, &mut rustdoc); add_allow_features(build_runner, &mut rustdoc); + if let Some(trim_paths) = unit.profile.trim_paths.as_ref() { + trim_paths_args_rustdoc(&mut rustdoc, build_runner, unit, trim_paths)?; + } + rustdoc.args(unit.pkg.manifest().lint_rustflags()); if let Some(args) = build_runner.bcx.extra_args_for(unit) { rustdoc.args(args); @@ -1223,6 +1228,32 @@ fn features_args(unit: &Unit) -> Vec { args } +/// Like [`trim_paths_args`] but for rustdoc invocations. +fn trim_paths_args_rustdoc( + cmd: &mut ProcessBuilder, + build_runner: &BuildRunner<'_, '_>, + unit: &Unit, + trim_paths: &TomlTrimPaths, +) -> CargoResult<()> { + match trim_paths { + // rustdoc supports diagnostics trimming only. + TomlTrimPaths::Values(values) if !values.contains(&TomlTrimPathsValue::Diagnostics) => { + return Ok(()) + } + _ => {} + } + + // feature gate was checked during manifest/config parsing. + cmd.arg("-Zunstable-options"); + + // Order of `--remap-path-prefix` flags is important for `-Zbuild-std`. + // We want to show `/rustc//library/std` instead of `std-0.0.0`. + cmd.arg(package_remap(build_runner, unit)); + cmd.arg(sysroot_remap(build_runner, unit)); + + Ok(()) +} + /// Generates the `--remap-path-scope` and `--remap-path-prefix` for [RFC 3127]. /// See also unstable feature [`-Ztrim-paths`]. /// diff --git a/tests/testsuite/profile_trim_paths.rs b/tests/testsuite/profile_trim_paths.rs index 720dd371802..821119a0961 100644 --- a/tests/testsuite/profile_trim_paths.rs +++ b/tests/testsuite/profile_trim_paths.rs @@ -831,8 +831,10 @@ fn rustdoc_diagnostics_works() { .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) .with_stderr_data(str![[r#" ... +[RUNNING] `[..]rustc [..]-Zremap-path-scope=diagnostics --remap-path-prefix=[ROOT]/home/.cargo/registry/src= --remap-path-prefix=[..]/lib/rustlib/src/rust=/rustc/[..]` +... [WARNING] unopened HTML tag `script` - --> [ROOT]/home/.cargo/registry/src/-[HASH]/bar-0.0.1/src/lib.rs:2:17 + --> -[..]/bar-0.0.1/src/lib.rs:2:17 ... "#]]) .run();