Skip to content

Commit fdd2b60

Browse files
committed
fix(config)!: store override paths as given
`rustup override set` canonicalized the directory before storing it, so `settings.toml` could end up with a path the user never typed: `--path` on a symlink recorded its target, and a relative `--path` to a directory that did not exist yet was recorded verbatim and could never match. The current directory is now stored as rustup sees it, and `--path` is only made absolute, relative to that directory. Since overrides are matched by the directory a stored path leads to, entries written by earlier versions keep working, and one set on a symlink applies to its target as well.
1 parent 2a04511 commit fdd2b60

3 files changed

Lines changed: 22 additions & 34 deletions

File tree

‎src/cli/rustup_mode.rs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
env::consts::EXE_SUFFIX,
44
fmt,
55
io::{self, Write},
6-
path::{Path, PathBuf},
6+
path::{Path, PathBuf, absolute},
77
process::ExitStatus,
88
str::FromStr,
99
sync::Arc,
@@ -1750,7 +1750,12 @@ async fn override_add(
17501750
Err(e) => Err(e)?,
17511751
}
17521752

1753-
cfg.make_override(path.unwrap_or(&cfg.current_dir), &toolchain)?;
1753+
// Relative to the directory rustup runs in, which `absolute` alone would
1754+
// take from the OS rather than from `cfg`.
1755+
let path = path
1756+
.map(|path| absolute(cfg.current_dir.join(path)))
1757+
.transpose()?;
1758+
cfg.make_override(path.as_deref().unwrap_or(&cfg.current_dir), &toolchain)?;
17541759
Ok(ExitCode::SUCCESS)
17551760
}
17561761

@@ -1774,7 +1779,7 @@ fn override_remove(
17741779
}
17751780
list
17761781
} else if let Some(path) = path {
1777-
vec![path.to_owned()]
1782+
vec![absolute(cfg.current_dir.join(path))?]
17781783
} else {
17791784
vec![cfg.current_dir.clone()]
17801785
};

‎src/settings.rs‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,8 @@ pub struct Settings {
105105
}
106106

107107
impl Settings {
108-
fn path_to_key(path: &Path) -> String {
109-
if path.exists() {
110-
utils::canonicalize_path(path).display().to_string()
111-
} else {
112-
path.display().to_string()
113-
}
114-
}
115-
116108
pub(crate) fn remove_override(&mut self, path: &Path) -> bool {
117-
let key = Self::path_to_key(path);
109+
let key = path.display().to_string();
118110
if self.overrides.remove(&key).is_some() {
119111
return true;
120112
}
@@ -128,7 +120,7 @@ impl Settings {
128120
}
129121

130122
pub(crate) fn add_override(&mut self, path: &Path, toolchain: String) {
131-
let key = Self::path_to_key(path);
123+
let key = path.display().to_string();
132124
// Keep a single override per directory, whichever path it was stored under.
133125
if let Ok(dir) = Handle::from_path(path) {
134126
self.overrides.retain(|stored, _| !leads_to(stored, &dir));

‎tests/suite/cli_exact.rs‎

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -513,21 +513,14 @@ info: override toolchain for '[PATH]' removed
513513
#[tokio::test]
514514
async fn list_overrides() {
515515
let cx = CliTestContext::new(Scenario::SimpleV2).await;
516-
let cwd = fs::canonicalize(cx.config.current_dir()).unwrap();
517-
let mut cwd_formatted = format!("{}", cwd.display());
518-
519-
if cfg!(windows) {
520-
cwd_formatted.drain(..4);
521-
}
522-
523516
cx.config
524517
.expect(["rustup", "override", "add", "nightly"])
525518
.await
526519
.is_ok();
527520
cx.config
528521
.expect(["rustup", "override", "list"])
529522
.await
530-
.extend_redactions([("[CWD]", cwd_formatted)])
523+
.extend_redactions([("[CWD]", cx.config.current_dir())])
531524
.is_ok()
532525
.with_stdout(snapbox::str![[r#"
533526
[CWD] nightly
@@ -550,24 +543,19 @@ async fn list_overrides_with_nonexistent() {
550543
.expect(["rustup", "override", "add", "nightly"])
551544
.await
552545
.is_ok();
553-
fs::canonicalize(dir.path()).unwrap()
546+
dir.path().to_owned()
554547
};
555548
// FIXME TempDir seems to succumb to difficulties removing dirs on windows
556549
let _ = raw::remove_dir(&nonexistent_path);
557550
assert!(!nonexistent_path.exists());
558-
let mut path_formatted = format!("{}", nonexistent_path.display());
559-
560-
if cfg!(windows) {
561-
path_formatted.drain(..4);
562-
}
563551

564552
cx.config
565553
.expect(["rustup", "override", "list"])
566554
.await
567-
.extend_redactions([("[PATH]", path_formatted + " (not a directory)")])
555+
.extend_redactions([("[PATH]", nonexistent_path)])
568556
.is_ok()
569557
.with_stdout(snapbox::str![[r#"
570-
[PATH] nightly
558+
[PATH] (not a directory) nightly
571559
572560
573561
"#]])
@@ -765,14 +753,15 @@ async fn override_set_with_path_symlink() {
765753
.await
766754
.is_ok();
767755

768-
// The symlink is resolved before the override is stored.
756+
// The override is stored under the path as given, and applies to the
757+
// directory however it is reached.
769758
cx.config
770759
.expect(["rustup", "override", "list"])
771760
.await
772761
.extend_redactions([("[LINK]", link.clone()), ("[TARGET]", target.clone())])
773762
.is_ok()
774763
.with_stdout(snapbox::str![[r#"
775-
[TARGET] nightly
764+
[LINK] nightly
776765
777766
"#]])
778767
.with_stderr(snapbox::str![[""]]);
@@ -801,13 +790,15 @@ async fn override_set_with_path_relative_nonexistent() {
801790
.await
802791
.is_ok();
803792

804-
// The relative path is stored as given, so it can never match a directory.
793+
// The relative path is made absolute, so it matches the directory once
794+
// that exists.
805795
cx.config
806796
.expect(["rustup", "override", "list"])
807797
.await
798+
.extend_redactions([("[CWD]", cx.config.current_dir())])
808799
.is_ok()
809800
.with_stdout(snapbox::str![[r#"
810-
later (not a directory) nightly
801+
[CWD]/later (not a directory) nightly
811802
812803
813804
"#]])
@@ -824,7 +815,7 @@ info: you may remove overrides for non-existent directories with
824815
.await
825816
.is_ok()
826817
.with_stdout(snapbox::str![[r#"
827-
1.1.0 (hash-stable-1.1.0)
818+
1.3.0 (hash-nightly-2)
828819
829820
"#]]);
830821
}

0 commit comments

Comments
 (0)