add unstable -Zroot-path flag to configure the path from which rustc should be invoked

This commit is contained in:
Ralf Jung 2024-10-31 21:35:57 +01:00
parent 573ff00fe6
commit c2be327042
4 changed files with 45 additions and 17 deletions

View File

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

View File

@ -4,6 +4,7 @@ use crate::core::{Target, Workspace};
use crate::ops::CompileOptions;
use crate::util::CargoResult;
use anyhow::bail;
use cargo_util::paths::normalize_path;
use cargo_util::ProcessBuilder;
use std::fmt::Write;
use std::path::PathBuf;
@ -109,15 +110,20 @@ pub fn print_available_tests(ws: &Workspace<'_>, options: &CompileOptions) -> Ca
/// The first returned value here is the argument to pass to rustc, and the
/// second is the cwd that rustc should operate in.
pub fn path_args(ws: &Workspace<'_>, unit: &Unit) -> (PathBuf, PathBuf) {
let ws_root = ws.root();
let src = match unit.target.src_path() {
TargetSourcePath::Path(path) => path.to_path_buf(),
TargetSourcePath::Metabuild => unit.pkg.manifest().metabuild_path(ws.target_dir()),
};
assert!(src.is_absolute());
if unit.pkg.package_id().source_id().is_path() {
if let Ok(path) = src.strip_prefix(ws_root) {
return (path.to_path_buf(), ws_root.to_path_buf());
// Determine which path we make this relative to: usually it's the workspace root,
// but this can be overwritten with a `-Z` flag.
let root = match &ws.gctx().cli_unstable().root_dir {
None => ws.root().to_owned(),
Some(root_dir) => normalize_path(&ws.gctx().cwd().join(root_dir)),
};
if let Ok(path) = src.strip_prefix(&root) {
return (path.to_path_buf(), root);
}
}
(src, unit.pkg.root().to_path_buf())

View File

@ -1,4 +1,4 @@
<svg width="1230px" height="740px" xmlns="http://www.w3.org/2000/svg">
<svg width="1230px" height="758px" xmlns="http://www.w3.org/2000/svg">
<style>
.fg { fill: #AAAAAA }
.bg { background: #000000 }
@ -74,27 +74,29 @@
</tspan>
<tspan x="10px" y="532px"><tspan> -Z publish-timeout Enable the `publish.timeout` key in .cargo/config.toml file</tspan>
</tspan>
<tspan x="10px" y="550px"><tspan> -Z rustdoc-map Allow passing external documentation mappings to rustdoc</tspan>
<tspan x="10px" y="550px"><tspan> -Z root-dir Set the root directory relative to which paths are printed (defaults to workspace root)</tspan>
</tspan>
<tspan x="10px" y="568px"><tspan> -Z rustdoc-scrape-examples Allows Rustdoc to scrape code examples from reverse-dependencies</tspan>
<tspan x="10px" y="568px"><tspan> -Z rustdoc-map Allow passing external documentation mappings to rustdoc</tspan>
</tspan>
<tspan x="10px" y="586px"><tspan> -Z script Enable support for single-file, `.rs` packages</tspan>
<tspan x="10px" y="586px"><tspan> -Z rustdoc-scrape-examples Allows Rustdoc to scrape code examples from reverse-dependencies</tspan>
</tspan>
<tspan x="10px" y="604px"><tspan> -Z target-applies-to-host Enable the `target-applies-to-host` key in the .cargo/config.toml file</tspan>
<tspan x="10px" y="604px"><tspan> -Z script Enable support for single-file, `.rs` packages</tspan>
</tspan>
<tspan x="10px" y="622px"><tspan> -Z trim-paths Enable the `trim-paths` option in profiles</tspan>
<tspan x="10px" y="622px"><tspan> -Z target-applies-to-host Enable the `target-applies-to-host` key in the .cargo/config.toml file</tspan>
</tspan>
<tspan x="10px" y="640px"><tspan> -Z unstable-options Allow the usage of unstable options</tspan>
<tspan x="10px" y="640px"><tspan> -Z trim-paths Enable the `trim-paths` option in profiles</tspan>
</tspan>
<tspan x="10px" y="658px">
<tspan x="10px" y="658px"><tspan> -Z unstable-options Allow the usage of unstable options</tspan>
</tspan>
<tspan x="10px" y="676px"><tspan>Run with `cargo -Z [FLAG] [COMMAND]`</tspan>
<tspan x="10px" y="676px">
</tspan>
<tspan x="10px" y="694px">
<tspan x="10px" y="694px"><tspan>Run with `cargo -Z [FLAG] [COMMAND]`</tspan>
</tspan>
<tspan x="10px" y="712px"><tspan>See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information about these flags.</tspan>
<tspan x="10px" y="712px">
</tspan>
<tspan x="10px" y="730px">
<tspan x="10px" y="730px"><tspan>See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information about these flags.</tspan>
</tspan>
<tspan x="10px" y="748px">
</tspan>
</text>

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

@ -802,13 +802,17 @@ fn root_dir_diagnostics() {
.file("ws_root/src/lib.rs", "invalid;")
.build();
// Crucially, the rustc error message below says `ws_root/...`, i.e.
// it is relative to our fake home, not to the workspace root.
p.cargo("check")
.arg("-Zroot-dir=.")
.arg("--manifest-path=ws_root/Cargo.toml")
.masquerade_as_nightly_cargo(&["-Zroot-dir"])
.with_status(101)
.with_stderr_data(str![[r#"
[CHECKING] foo v0.1.0 ([ROOT]/ws_root)
[ERROR] [..]
--> src/lib.rs:1:8
--> ws_root/src/lib.rs:1:8
|
1 | invalid;
| [..]
@ -839,10 +843,23 @@ fn root_dir_file_macro() {
)
.build();
// Crucially, the path is relative to our fake home, not to the workspace root.
p.cargo("run")
.arg("-Zroot-dir=.")
.arg("--manifest-path=ws_root/Cargo.toml")
.masquerade_as_nightly_cargo(&["-Zroot-dir"])
.with_stdout_data(str![[r#"
src/main.rs
ws_root/src/main.rs
"#]])
.run();
// Try again with an absolute path for `root-dir`.
p.cargo("run")
.arg(format!("-Zroot-dir={}", p.root().display()))
.arg("--manifest-path=ws_root/Cargo.toml")
.masquerade_as_nightly_cargo(&["-Zroot-dir"])
.with_stdout_data(str![[r#"
ws_root/src/main.rs
"#]])
.run();