diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index c15c73a215e..7d72c712cc1 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -119,17 +119,15 @@ pub fn install( // able to run these commands. let dst = root.join("bin").into_path_unlocked(); let path = env::var_os("PATH").unwrap_or_default(); - for path in env::split_paths(&path) { - if path == dst { - return Ok(()); - } - } + let dst_in_path = env::split_paths(&path).any(|path| path == dst); - config.shell().warn(&format!( - "be sure to add `{}` to your PATH to be \ + if !dst_in_path { + config.shell().warn(&format!( + "be sure to add `{}` to your PATH to be \ able to run the installed binaries", - dst.display() - ))?; + dst.display() + ))?; + } } if scheduled_error { diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 7a38607cc35..6e45c3d19fa 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -14,6 +14,8 @@ use cargo_test_support::install::{ assert_has_installed_exe, assert_has_not_installed_exe, cargo_home, }; use cargo_test_support::paths; +use std::env; +use std::path::PathBuf; fn pkg(name: &str, vers: &str) { Package::new(name, vers) @@ -129,6 +131,65 @@ fn multiple_pkgs() { assert_has_not_installed_exe(cargo_home(), "bar"); } +fn path() -> Vec { + env::split_paths(&env::var_os("PATH").unwrap_or_default()).collect() +} + +#[cargo_test] +fn multiple_pkgs_path_set() { + // confirm partial failure results in 101 status code and does not have the + // '[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries' + // even if CARGO_HOME/bin is in the PATH + pkg("foo", "0.0.1"); + pkg("bar", "0.0.2"); + + // add CARGO_HOME/bin to path + let mut path = path(); + path.push(cargo_home().join("bin")); + let new_path = env::join_paths(path).unwrap(); + cargo_process("install foo bar baz") + .env("PATH", new_path) + .with_status(101) + .with_stderr( + "\ +[UPDATING] `[..]` index +[DOWNLOADING] crates ... +[DOWNLOADED] foo v0.0.1 (registry `[CWD]/registry`) +[INSTALLING] foo v0.0.1 +[COMPILING] foo v0.0.1 +[FINISHED] release [optimized] target(s) in [..] +[INSTALLING] [CWD]/home/.cargo/bin/foo[EXE] +[INSTALLED] package `foo v0.0.1` (executable `foo[EXE]`) +[DOWNLOADING] crates ... +[DOWNLOADED] bar v0.0.2 (registry `[CWD]/registry`) +[INSTALLING] bar v0.0.2 +[COMPILING] bar v0.0.2 +[FINISHED] release [optimized] target(s) in [..] +[INSTALLING] [CWD]/home/.cargo/bin/bar[EXE] +[INSTALLED] package `bar v0.0.2` (executable `bar[EXE]`) +[ERROR] could not find `baz` in registry `[..]` with version `*` +[SUMMARY] Successfully installed foo, bar! Failed to install baz (see error(s) above). +[ERROR] some crates failed to install +", + ) + .run(); + assert_has_installed_exe(cargo_home(), "foo"); + assert_has_installed_exe(cargo_home(), "bar"); + + cargo_process("uninstall foo bar") + .with_stderr( + "\ +[REMOVING] [CWD]/home/.cargo/bin/foo[EXE] +[REMOVING] [CWD]/home/.cargo/bin/bar[EXE] +[SUMMARY] Successfully uninstalled foo, bar! +", + ) + .run(); + + assert_has_not_installed_exe(cargo_home(), "foo"); + assert_has_not_installed_exe(cargo_home(), "bar"); +} + #[cargo_test] fn pick_max_version() { pkg("foo", "0.1.0");