From ddc60864726a3fe97f0f989128e2fbfa3ba1bf3e Mon Sep 17 00:00:00 2001 From: Daniel Poelzleithner Date: Wed, 26 Oct 2022 12:43:12 +0200 Subject: [PATCH] Add global option --absolute-paths Using this option will call rustc with absolute file paths and rustc will report absolute paths in error cases etc. This will cause the build cache to invalidate when the folder is moved, but will make external integrations easier. --- src/bin/cargo/cli.rs | 3 +++ src/cargo/util/config/mod.rs | 9 +++++++++ src/cargo/util/workspace.rs | 8 ++++++-- tests/testsuite/config.rs | 1 + tests/testsuite/paths.rs | 23 +++++++++++++++++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index c6b57910b03..cd8e66ae0f1 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -333,6 +333,7 @@ fn config_configure( let frozen = args.flag("frozen") || global_args.frozen; let locked = args.flag("locked") || global_args.locked; let offline = args.flag("offline") || global_args.offline; + let absolute_paths = args.flag("absolute-paths") || global_args.offline; let mut unstable_flags = global_args.unstable_flags; if let Some(values) = args.get_many::("unstable-features") { unstable_flags.extend(values.cloned()); @@ -351,6 +352,7 @@ fn config_configure( arg_target_dir, &unstable_flags, &config_args, + absolute_paths, )?; Ok(()) } @@ -470,6 +472,7 @@ See 'cargo help ' for more information on a specific command.\n", .arg(flag("frozen", "Require Cargo.lock and cache are up to date").global(true)) .arg(flag("locked", "Require Cargo.lock is up to date").global(true)) .arg(flag("offline", "Run without accessing the network").global(true)) + .arg(flag("absolute-paths", "Use absolute paths when calling rust").global(true)) .arg(multi_opt("config", "KEY=VALUE", "Override a configuration value").global(true)) .arg( Arg::new("unstable-features") diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 765c93db704..fa170c125e9 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -200,6 +200,8 @@ pub struct Config { doc_extern_map: LazyCell, progress_config: ProgressConfig, env_config: LazyCell, + /// Use absolute paths when invoking rustc + absolute_paths: bool, /// This should be false if: /// - this is an artifact of the rustc distribution process for "stable" or for "beta" /// - this is an `#[test]` that does not opt in with `enable_nightly_features` @@ -274,6 +276,7 @@ impl Config { frozen: false, locked: false, offline: false, + absolute_paths: false, jobserver: unsafe { if GLOBAL_JOBSERVER.is_null() { None @@ -896,6 +899,7 @@ impl Config { target_dir: &Option, unstable_flags: &[String], cli_config: &[String], + absolute_paths: bool, ) -> CargoResult<()> { for warning in self .unstable_flags @@ -961,6 +965,7 @@ impl Config { .and_then(|n| n.offline) .unwrap_or(false); self.target_dir = cli_target_dir; + self.absolute_paths = absolute_paths; self.load_unstable_flags_from_config()?; @@ -1014,6 +1019,10 @@ impl Config { !self.frozen && !self.locked } + pub fn absolute_paths(&self) -> bool { + self.absolute_paths + } + /// Loads configuration from the filesystem. pub fn load_values(&self) -> CargoResult> { self.load_values_from(&self.cwd) diff --git a/src/cargo/util/workspace.rs b/src/cargo/util/workspace.rs index e8317f101e4..5d9e92dc3b1 100644 --- a/src/cargo/util/workspace.rs +++ b/src/cargo/util/workspace.rs @@ -116,8 +116,12 @@ pub fn path_args(ws: &Workspace<'_>, unit: &Unit) -> (PathBuf, PathBuf) { }; 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()); + if ws.config().absolute_paths() { + return (src, ws_root.to_path_buf()); + } else { + if let Ok(path) = src.strip_prefix(ws_root) { + return (path.to_path_buf(), ws_root.to_path_buf()); + } } } (src, unit.pkg.root().to_path_buf()) diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index b1d07bb4050..f8c9c894a5a 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -90,6 +90,7 @@ impl ConfigBuilder { &None, &self.unstable, &self.config_args, + false, )?; Ok(config) } diff --git a/tests/testsuite/paths.rs b/tests/testsuite/paths.rs index c5b77586545..8ec513c3875 100644 --- a/tests/testsuite/paths.rs +++ b/tests/testsuite/paths.rs @@ -224,3 +224,26 @@ dependencies; the dependency on `baz` was either added or\ ) .run(); } + +#[cargo_test] +fn absolute_path_tests() { + let foo = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#, + ) + .file("src/main.rs", "fn main() { failure }") + .build(); + + println!("{}", foo.root().to_str().unwrap()); + foo.cargo("--absolute-paths build") + .with_stderr_contains("[..]--> [ROOT]/foo/src/main.rs:1:13[..]") + .with_stderr_contains("[COMPILING] foo v0.0.1 ([ROOT]/foo)") + .with_status(101) + .run(); +}