Skip to content

Commit

Permalink
anchored config discovery for cargo-install and cargo-uninstall at th…
Browse files Browse the repository at this point in the history
…e global configuration file
  • Loading branch information
jofas committed Feb 28, 2023
1 parent 6e757c7 commit b73b642
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
8 changes: 4 additions & 4 deletions crates/cargo-util/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>("root").map(String::as_str);
Expand Down
4 changes: 1 addition & 3 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: Into<PathBuf>>(&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
Expand Down
15 changes: 9 additions & 6 deletions tests/testsuite/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
));
}

Expand Down
26 changes: 18 additions & 8 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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]
Expand Down

0 comments on commit b73b642

Please sign in to comment.