Skip to content

Commit c2be327

Browse files
committed
add unstable -Zroot-path flag to configure the path from which rustc should be invoked
1 parent 573ff00 commit c2be327

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

src/cargo/core/features.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
use std::collections::BTreeSet;
122122
use std::env;
123123
use std::fmt::{self, Write};
124+
use std::path::PathBuf;
124125
use std::str::FromStr;
125126

126127
use anyhow::{bail, Error};
@@ -783,6 +784,7 @@ unstable_cli_options!(
783784
profile_rustflags: bool = ("Enable the `rustflags` option in profiles in .cargo/config.toml file"),
784785
public_dependency: bool = ("Respect a dependency's `public` field in Cargo.toml to control public/private dependencies"),
785786
publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"),
787+
root_dir: Option<PathBuf> = ("Set the root directory relative to which paths are printed (defaults to workspace root)"),
786788
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
787789
rustdoc_scrape_examples: bool = ("Allows Rustdoc to scrape code examples from reverse-dependencies"),
788790
script: bool = ("Enable support for single-file, `.rs` packages"),
@@ -1287,6 +1289,7 @@ impl CliUnstable {
12871289
"profile-rustflags" => self.profile_rustflags = parse_empty(k, v)?,
12881290
"trim-paths" => self.trim_paths = parse_empty(k, v)?,
12891291
"publish-timeout" => self.publish_timeout = parse_empty(k, v)?,
1292+
"root-dir" => self.root_dir = v.map(|v| v.into()),
12901293
"rustdoc-map" => self.rustdoc_map = parse_empty(k, v)?,
12911294
"rustdoc-scrape-examples" => self.rustdoc_scrape_examples = parse_empty(k, v)?,
12921295
"separate-nightlies" => self.separate_nightlies = parse_empty(k, v)?,

src/cargo/util/workspace.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::core::{Target, Workspace};
44
use crate::ops::CompileOptions;
55
use crate::util::CargoResult;
66
use anyhow::bail;
7+
use cargo_util::paths::normalize_path;
78
use cargo_util::ProcessBuilder;
89
use std::fmt::Write;
910
use std::path::PathBuf;
@@ -109,15 +110,20 @@ pub fn print_available_tests(ws: &Workspace<'_>, options: &CompileOptions) -> Ca
109110
/// The first returned value here is the argument to pass to rustc, and the
110111
/// second is the cwd that rustc should operate in.
111112
pub fn path_args(ws: &Workspace<'_>, unit: &Unit) -> (PathBuf, PathBuf) {
112-
let ws_root = ws.root();
113113
let src = match unit.target.src_path() {
114114
TargetSourcePath::Path(path) => path.to_path_buf(),
115115
TargetSourcePath::Metabuild => unit.pkg.manifest().metabuild_path(ws.target_dir()),
116116
};
117117
assert!(src.is_absolute());
118118
if unit.pkg.package_id().source_id().is_path() {
119-
if let Ok(path) = src.strip_prefix(ws_root) {
120-
return (path.to_path_buf(), ws_root.to_path_buf());
119+
// Determine which path we make this relative to: usually it's the workspace root,
120+
// but this can be overwritten with a `-Z` flag.
121+
let root = match &ws.gctx().cli_unstable().root_dir {
122+
None => ws.root().to_owned(),
123+
Some(root_dir) => normalize_path(&ws.gctx().cwd().join(root_dir)),
124+
};
125+
if let Ok(path) = src.strip_prefix(&root) {
126+
return (path.to_path_buf(), root);
121127
}
122128
}
123129
(src, unit.pkg.root().to_path_buf())

tests/testsuite/cargo/z_help/stdout.term.svg

Lines changed: 14 additions & 12 deletions
Loading

tests/testsuite/directory.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,13 +802,17 @@ fn root_dir_diagnostics() {
802802
.file("ws_root/src/lib.rs", "invalid;")
803803
.build();
804804

805+
// Crucially, the rustc error message below says `ws_root/...`, i.e.
806+
// it is relative to our fake home, not to the workspace root.
805807
p.cargo("check")
808+
.arg("-Zroot-dir=.")
806809
.arg("--manifest-path=ws_root/Cargo.toml")
810+
.masquerade_as_nightly_cargo(&["-Zroot-dir"])
807811
.with_status(101)
808812
.with_stderr_data(str![[r#"
809813
[CHECKING] foo v0.1.0 ([ROOT]/ws_root)
810814
[ERROR] [..]
811-
--> src/lib.rs:1:8
815+
--> ws_root/src/lib.rs:1:8
812816
|
813817
1 | invalid;
814818
| [..]
@@ -839,10 +843,23 @@ fn root_dir_file_macro() {
839843
)
840844
.build();
841845

846+
// Crucially, the path is relative to our fake home, not to the workspace root.
842847
p.cargo("run")
848+
.arg("-Zroot-dir=.")
843849
.arg("--manifest-path=ws_root/Cargo.toml")
850+
.masquerade_as_nightly_cargo(&["-Zroot-dir"])
844851
.with_stdout_data(str![[r#"
845-
src/main.rs
852+
ws_root/src/main.rs
853+
854+
"#]])
855+
.run();
856+
// Try again with an absolute path for `root-dir`.
857+
p.cargo("run")
858+
.arg(format!("-Zroot-dir={}", p.root().display()))
859+
.arg("--manifest-path=ws_root/Cargo.toml")
860+
.masquerade_as_nightly_cargo(&["-Zroot-dir"])
861+
.with_stdout_data(str![[r#"
862+
ws_root/src/main.rs
846863
847864
"#]])
848865
.run();

0 commit comments

Comments
 (0)