Skip to content

Commit b73b642

Browse files
committed
anchored config discovery for cargo-install and cargo-uninstall at the global configuration file
1 parent 6e757c7 commit b73b642

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

crates/cargo-util/src/paths.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,10 @@ pub struct PathAncestors<'a> {
368368

369369
impl<'a> PathAncestors<'a> {
370370
fn new(path: &'a Path, stop_root_at: Option<&Path>) -> PathAncestors<'a> {
371-
let stop_at = env::var("__CARGO_TEST_ROOT")
372-
.ok()
373-
.map(PathBuf::from)
374-
.or_else(|| stop_root_at.map(|p| p.to_path_buf()));
371+
let stop_at = stop_root_at
372+
.map(|p| p.to_path_buf())
373+
.or_else(|| env::var("__CARGO_TEST_ROOT").ok().map(PathBuf::from));
374+
375375
PathAncestors {
376376
current: Some(path),
377377
//HACK: avoid reading `~/.cargo/config` when testing Cargo itself.

src/bin/cargo/commands/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
9090
if let Some(path) = &path {
9191
config.reload_rooted_at(path)?;
9292
} else {
93-
// TODO: Consider calling set_search_stop_path(home).
93+
config.set_search_stop_path(config.home().clone().into_path_unlocked());
9494
config.reload_rooted_at(config.home().clone().into_path_unlocked())?;
9595
}
9696

src/bin/cargo/commands/uninstall.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub fn cli() -> Command {
1515

1616
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
1717
// Ignore local configuration, same as `cargo install` does
18+
config.set_search_stop_path(config.home().clone().into_path_unlocked());
1819
config.reload_rooted_at(config.home().clone().into_path_unlocked())?;
1920

2021
let root = args.get_one::<String>("root").map(String::as_str);

src/cargo/util/config/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,7 @@ impl Config {
532532
/// Sets the path where ancestor config file searching will stop. The
533533
/// given path is included, but its ancestors are not.
534534
pub fn set_search_stop_path<P: Into<PathBuf>>(&mut self, path: P) {
535-
let path = path.into();
536-
debug_assert!(self.cwd.starts_with(&path));
537-
self.search_stop_path = Some(path);
535+
self.search_stop_path = Some(path.into());
538536
}
539537

540538
/// Reloads on-disk configuration values, starting at the given path and

tests/testsuite/directory.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@ use serde::Serialize;
88

99
use cargo_test_support::cargo_process;
1010
use cargo_test_support::git;
11+
use cargo_test_support::install::cargo_home;
1112
use cargo_test_support::paths;
1213
use cargo_test_support::registry::{cksum, Package};
1314
use cargo_test_support::{basic_manifest, project, t, ProjectBuilder};
1415

1516
fn setup() {
16-
let root = paths::root();
17-
t!(fs::create_dir(&root.join(".cargo")));
17+
t!(fs::create_dir_all(cargo_home()));
1818
t!(fs::write(
19-
root.join(".cargo/config"),
20-
r#"
19+
cargo_home().join("config"),
20+
&format!(
21+
"
2122
[source.crates-io]
2223
replace-with = 'my-awesome-local-registry'
2324
2425
[source.my-awesome-local-registry]
25-
directory = 'index'
26-
"#
26+
directory = '{}/index'
27+
",
28+
paths::root().display(),
29+
)
2730
));
2831
}
2932

tests/testsuite/install.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ fn install_location_precedence() {
407407
assert_has_installed_exe(&t1, "foo");
408408
assert_has_not_installed_exe(&t2, "foo");
409409

410+
cargo_process("uninstall foo --root")
411+
.arg(&t1)
412+
.env("CARGO_INSTALL_ROOT", &t2)
413+
.run();
414+
assert_has_not_installed_exe(&t1, "foo");
415+
410416
println!("install CARGO_INSTALL_ROOT");
411417

412418
cargo_process("install foo")
@@ -415,18 +421,22 @@ fn install_location_precedence() {
415421
assert_has_installed_exe(&t2, "foo");
416422
assert_has_not_installed_exe(&t3, "foo");
417423

418-
println!("install install.root");
419-
420-
cargo_process("install foo").run();
421-
assert_has_installed_exe(&t3, "foo");
422-
assert_has_not_installed_exe(&t4, "foo");
423-
424-
fs::remove_file(root.join(".cargo/config")).unwrap();
424+
cargo_process("uninstall foo")
425+
.env("CARGO_INSTALL_ROOT", &t2)
426+
.run();
427+
assert_has_not_installed_exe(&t2, "foo");
425428

426-
println!("install cargo home");
429+
// The `install.root` config value in `root/.cargo/config` is ignored by
430+
// `cargo install` and `cargo uninstall`, because they only consider the
431+
// global configuration file `$CARGO_HOME/config`.
432+
println!("install install.root");
427433

428434
cargo_process("install foo").run();
429435
assert_has_installed_exe(&t4, "foo");
436+
assert_has_not_installed_exe(&t3, "foo");
437+
438+
cargo_process("uninstall foo").run();
439+
assert_has_not_installed_exe(&t4, "foo");
430440
}
431441

432442
#[cargo_test]

0 commit comments

Comments
 (0)