diff --git a/crates/cargo-util/src/paths.rs b/crates/cargo-util/src/paths.rs index 69df7a2096c7..2724412dfdb3 100644 --- a/crates/cargo-util/src/paths.rs +++ b/crates/cargo-util/src/paths.rs @@ -368,10 +368,10 @@ pub struct PathAncestors<'a> { impl<'a> PathAncestors<'a> { fn new(path: &'a Path, stop_root_at: Option<&Path>) -> PathAncestors<'a> { - let stop_at = env::var("__CARGO_TEST_ROOT") - .ok() - .map(PathBuf::from) - .or_else(|| stop_root_at.map(|p| p.to_path_buf())); + let stop_at = stop_root_at + .map(|p| p.to_path_buf()) + .or_else(|| env::var("__CARGO_TEST_ROOT").ok().map(PathBuf::from)); + PathAncestors { current: Some(path), //HACK: avoid reading `~/.cargo/config` when testing Cargo itself. diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 790bfd2d7bce..354e6d8caf7e 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -90,7 +90,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { if let Some(path) = &path { config.reload_rooted_at(path)?; } else { - // TODO: Consider calling set_search_stop_path(home). + config.set_search_stop_path(config.home().clone().into_path_unlocked()); config.reload_rooted_at(config.home().clone().into_path_unlocked())?; } diff --git a/src/bin/cargo/commands/uninstall.rs b/src/bin/cargo/commands/uninstall.rs index e58634933834..95505bde5929 100644 --- a/src/bin/cargo/commands/uninstall.rs +++ b/src/bin/cargo/commands/uninstall.rs @@ -15,6 +15,7 @@ pub fn cli() -> Command { pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { // Ignore local configuration, same as `cargo install` does + config.set_search_stop_path(config.home().clone().into_path_unlocked()); config.reload_rooted_at(config.home().clone().into_path_unlocked())?; let root = args.get_one::("root").map(String::as_str); diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index b5c781efddd0..dd64f6b4110f 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -532,9 +532,7 @@ impl Config { /// Sets the path where ancestor config file searching will stop. The /// given path is included, but its ancestors are not. pub fn set_search_stop_path>(&mut self, path: P) { - let path = path.into(); - debug_assert!(self.cwd.starts_with(&path)); - self.search_stop_path = Some(path); + self.search_stop_path = Some(path.into()); } /// Reloads on-disk configuration values, starting at the given path and diff --git a/tests/testsuite/directory.rs b/tests/testsuite/directory.rs index 0e28de03942b..97c35cd6084b 100644 --- a/tests/testsuite/directory.rs +++ b/tests/testsuite/directory.rs @@ -8,22 +8,25 @@ use serde::Serialize; use cargo_test_support::cargo_process; use cargo_test_support::git; +use cargo_test_support::install::cargo_home; use cargo_test_support::paths; use cargo_test_support::registry::{cksum, Package}; use cargo_test_support::{basic_manifest, project, t, ProjectBuilder}; fn setup() { - let root = paths::root(); - t!(fs::create_dir(&root.join(".cargo"))); + t!(fs::create_dir_all(cargo_home())); t!(fs::write( - root.join(".cargo/config"), - r#" + cargo_home().join("config"), + &format!( + " [source.crates-io] replace-with = 'my-awesome-local-registry' [source.my-awesome-local-registry] - directory = 'index' - "# + directory = '{}/index' + ", + paths::root().display(), + ) )); } diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index d3713a481616..51b528077e2c 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -407,6 +407,12 @@ fn install_location_precedence() { assert_has_installed_exe(&t1, "foo"); assert_has_not_installed_exe(&t2, "foo"); + cargo_process("uninstall foo --root") + .arg(&t1) + .env("CARGO_INSTALL_ROOT", &t2) + .run(); + assert_has_not_installed_exe(&t1, "foo"); + println!("install CARGO_INSTALL_ROOT"); cargo_process("install foo") @@ -415,18 +421,22 @@ fn install_location_precedence() { assert_has_installed_exe(&t2, "foo"); assert_has_not_installed_exe(&t3, "foo"); - println!("install install.root"); - - cargo_process("install foo").run(); - assert_has_installed_exe(&t3, "foo"); - assert_has_not_installed_exe(&t4, "foo"); - - fs::remove_file(root.join(".cargo/config")).unwrap(); + cargo_process("uninstall foo") + .env("CARGO_INSTALL_ROOT", &t2) + .run(); + assert_has_not_installed_exe(&t2, "foo"); - println!("install cargo home"); + // The `install.root` config value in `root/.cargo/config` is ignored by + // `cargo install` and `cargo uninstall`, because they only consider the + // global configuration file `$CARGO_HOME/config`. + println!("install install.root"); cargo_process("install foo").run(); assert_has_installed_exe(&t4, "foo"); + assert_has_not_installed_exe(&t3, "foo"); + + cargo_process("uninstall foo").run(); + assert_has_not_installed_exe(&t4, "foo"); } #[cargo_test]