Skip to content

Add global option --absolute-paths #11297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>("unstable-features") {
unstable_flags.extend(values.cloned());
Expand All @@ -351,6 +352,7 @@ fn config_configure(
arg_target_dir,
&unstable_flags,
&config_args,
absolute_paths,
)?;
Ok(())
}
Expand Down Expand Up @@ -470,6 +472,7 @@ See 'cargo help <command>' 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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure. Should this be global? Some commands might not need it. Or maybe it controls not only "rustc invocations" but also all output related to filesystem paths.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I feel like this should be a -Z flag (unstable flag) first.

.arg(multi_opt("config", "KEY=VALUE", "Override a configuration value").global(true))
.arg(
Arg::new("unstable-features")
Expand Down
9 changes: 9 additions & 0 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ pub struct Config {
doc_extern_map: LazyCell<RustdocExternMap>,
progress_config: ProgressConfig,
env_config: LazyCell<EnvConfig>,
/// 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`
Expand Down Expand Up @@ -274,6 +276,7 @@ impl Config {
frozen: false,
locked: false,
offline: false,
absolute_paths: false,
jobserver: unsafe {
if GLOBAL_JOBSERVER.is_null() {
None
Expand Down Expand Up @@ -896,6 +899,7 @@ impl Config {
target_dir: &Option<PathBuf>,
unstable_flags: &[String],
cli_config: &[String],
absolute_paths: bool,
) -> CargoResult<()> {
for warning in self
.unstable_flags
Expand Down Expand Up @@ -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()?;

Expand Down Expand Up @@ -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<HashMap<String, ConfigValue>> {
self.load_values_from(&self.cwd)
Expand Down
8 changes: 6 additions & 2 deletions src/cargo/util/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl ConfigBuilder {
&None,
&self.unstable,
&self.config_args,
false,
)?;
Ok(config)
}
Expand Down
23 changes: 23 additions & 0 deletions tests/testsuite/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}