From 573ff00fe6265420bfeb6959515efb092c4a6250 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 31 Oct 2024 21:34:48 +0100 Subject: [PATCH 1/3] add tests in preparation for root-dir flag that changes their behavior --- tests/testsuite/directory.rs | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tests/testsuite/directory.rs b/tests/testsuite/directory.rs index a9a8324b8b0..6dbbceb80c6 100644 --- a/tests/testsuite/directory.rs +++ b/tests/testsuite/directory.rs @@ -784,3 +784,66 @@ Caused by: .with_status(101) .run(); } + +#[cargo_test] +fn root_dir_diagnostics() { + 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(); + + p.cargo("check") + .arg("--manifest-path=ws_root/Cargo.toml") + .with_status(101) + .with_stderr_data(str![[r#" +[CHECKING] foo v0.1.0 ([ROOT]/ws_root) +[ERROR] [..] + --> src/lib.rs:1:8 + | +1 | invalid; + | [..] + +[ERROR] could not compile `foo` (lib) due to 1 previous error + +"#]]) + .run(); +} + +#[cargo_test] +fn root_dir_file_macro() { + 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/main.rs", + r#"fn main() { println!("{}", file!()); }"#, + ) + .build(); + + p.cargo("run") + .arg("--manifest-path=ws_root/Cargo.toml") + .with_stdout_data(str![[r#" +src/main.rs + +"#]]) + .run(); +} From c2be32704208f46d6134d264ebe7285ebbf67e9b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 31 Oct 2024 21:35:57 +0100 Subject: [PATCH 2/3] add unstable -Zroot-path flag to configure the path from which rustc should be invoked --- src/cargo/core/features.rs | 3 +++ src/cargo/util/workspace.rs | 12 ++++++--- tests/testsuite/cargo/z_help/stdout.term.svg | 26 +++++++++++--------- tests/testsuite/directory.rs | 21 ++++++++++++++-- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index 2638ff95234..51e26054c53 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -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 = ("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)?, diff --git a/src/cargo/util/workspace.rs b/src/cargo/util/workspace.rs index a2e0fff5068..754775596fa 100644 --- a/src/cargo/util/workspace.rs +++ b/src/cargo/util/workspace.rs @@ -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()) diff --git a/tests/testsuite/cargo/z_help/stdout.term.svg b/tests/testsuite/cargo/z_help/stdout.term.svg index a429e92d58b..993a456f1e5 100644 --- a/tests/testsuite/cargo/z_help/stdout.term.svg +++ b/tests/testsuite/cargo/z_help/stdout.term.svg @@ -1,4 +1,4 @@ - +