Skip to content

Commit

Permalink
add unstable -Zroot-path flag to configure the path from which rustc …
Browse files Browse the repository at this point in the history
…should be invoked
  • Loading branch information
RalfJung committed Oct 30, 2024
1 parent 06e0ef4 commit 146cc8a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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_path: Option<PathBuf> = ("Invoke rustc from the given root rather than the workspace root; affects diagnostics"),
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"),
Expand Down Expand Up @@ -1287,6 +1289,11 @@ 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-path" => self.root_path = v.map(|v| {
// Make the path absolute, if we can.
let v = PathBuf::from(v);
v.canonicalize().unwrap_or(v)
}),
"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)?,
Expand Down
13 changes: 10 additions & 3 deletions src/cargo/util/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,22 @@ 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 = ws
.gctx()
.cli_unstable()
.root_path
.as_deref()
.unwrap_or(ws.root());
if let Ok(path) = src.strip_prefix(root) {
return (path.to_path_buf(), root.to_path_buf());
}
}
(src, unit.pkg.root().to_path_buf())
Expand Down
38 changes: 38 additions & 0 deletions tests/testsuite/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,3 +784,41 @@ Caused by:
.with_status(101)
.run();
}

#[cargo_test]
fn custom_root_path() {
let p = ProjectBuilder::new(paths::root())
.no_manifest() // we are placing it in a different dir
.file(
"ws_root/Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
authors = []
"#,
)
.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-path=.")
.arg("--manifest-path=ws_root/Cargo.toml")
.masquerade_as_nightly_cargo(&["-Zroot-path"])
.with_status(101)
.with_stderr_data(str![[r#"
[CHECKING] foo v0.1.0 ([ROOT]/ws_root)
[ERROR] expected one of `!` or `::`, found `;`
--> ws_root/src/lib.rs:1:8
|
1 | invalid;
| ^ expected one of `!` or `::`
[ERROR] could not compile `foo` (lib) due to 1 previous error
"#]])
.run();
}

0 comments on commit 146cc8a

Please sign in to comment.