Skip to content

Commit

Permalink
Do not add home bin path to PATH if it's already there
Browse files Browse the repository at this point in the history
This is to allow users to control the order via PATH if they so desire.

Tested by preparing two different `cargo-foo` scripts in `$HOME/.cargo/bin` and `$HOME/bin`:

```
> env PATH="/usr/bin/:$HOME/bin:$HOME/.cargo/bin" ./target/debug/cargo foo
Inside ~/bin/
> env PATH="$HOME/.cargo/bin:/usr/bin/:$HOME/bin" ./target/debug/cargo foo
Inside ~/.cargo/bin/
> env PATH="/usr/bin/:$HOME/bin" ./target/debug/cargo foo
Inside ~/.cargo/bin/
```

and trailing slash:

```
> env PATH="$HOME/.cargo/bin/:/usr/bin/:$HOME/bin" ./target/debug/cargo foo
Inside ~/.cargo/bin/
> env PATH="/usr/bin/:$HOME/bin:$HOME/.cargo/bin/" ./target/debug/cargo foo
Inside ~/bin/
```

Fix rust-lang#11020
  • Loading branch information
dpc committed Aug 31, 2022
1 parent 4f81a49 commit 691877c
Showing 1 changed file with 57 additions and 35 deletions.
92 changes: 57 additions & 35 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
use std::env;
use std::fs;
use std::io::{Read, Write};
use std::os::unix::fs::PermissionsExt;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::str;

use cargo_test_support::basic_manifest;
use cargo_test_support::paths::CargoPathExt;
use cargo_test_support::registry::Package;
use cargo_test_support::tools::echo_subcommand;
use cargo_test_support::{
Expand Down Expand Up @@ -346,35 +347,56 @@ fn cargo_subcommand_env() {
///
/// Return paths to both places
fn set_up_cargo_foo() -> (PathBuf, PathBuf) {
let cargo_home_dir = paths::home().join(".cargo");
let bin_dir = cargo_home_dir.join("bin");
fs::create_dir_all(&bin_dir).unwrap();
fs::File::create(&bin_dir.join("cargo-foo"))
.unwrap()
.write_all(
br#"!/usr/bin/env bash
echo INSIDE
"#,
let p = project()
.at("cargo-foo")
.file("Cargo.toml", &basic_manifest("cargo-foo", "1.0.0"))
.file(
"src/bin/cargo-foo.rs",
r#"fn main() { println!("INSIDE"); }"#,
)
.unwrap();

fs::File::create(&cargo_home_dir.join("cargo-foo"))
.unwrap()
.write_all(
br#"!/usr/bin/env bash
echo OUTSIDE
"#,
.file(
"src/bin/cargo-foo2.rs",
r#"fn main() { println!("OUTSIDE"); }"#,
)
.unwrap();

fs::set_permissions(
cargo_home_dir.join("cargo-foo"),
fs::Permissions::from_mode(0o555),
)
.unwrap();
fs::set_permissions(bin_dir.join("cargo-foo"), fs::Permissions::from_mode(0o555)).unwrap();

(cargo_home_dir, bin_dir)
.build();
p.cargo("build").run();
let cargo_bin_dir = paths::home().join(".cargo/bin");
cargo_bin_dir.mkdir_p();
let root_bin_dir = paths::root().join("bin");
root_bin_dir.mkdir_p();
let exe_name = format!("cargo-foo{}", env::consts::EXE_SUFFIX);
fs::rename(p.bin("cargo-foo"), cargo_bin_dir.join(&exe_name)).unwrap();
fs::rename(p.bin("cargo-foo2"), root_bin_dir.join(&exe_name)).unwrap();

// let cargo_home_dir = paths::home().join(".cargo");
// let bin_dir = cargo_home_dir.join("bin");
// fs::create_dir_all(&bin_dir).unwrap();
// fs::File::create(&bin_dir.join("cargo-foo"))
// .unwrap()
// .write_all(
// br#"!/usr/bin/env bash
// echo INSIDE
// "#,
// )
// .unwrap();

// fs::File::create(&cargo_home_dir.join("cargo-foo"))
// .unwrap()
// .write_all(
// br#"!/usr/bin/env bash
// echo OUTSIDE
// "#,
// )
// .unwrap();

// fs::set_permissions(
// cargo_home_dir.join("cargo-foo"),
// fs::Permissions::from_mode(0o555),
// )
// .unwrap();
// fs::set_permissions(bin_dir.join("cargo-foo"), fs::Permissions::from_mode(0o555)).unwrap();

(root_bin_dir, cargo_bin_dir)
}

// If `$CARGO_HOME/bin` is not in a path, prefer it over anything in `$PATH`.
Expand All @@ -383,7 +405,7 @@ fn set_up_cargo_foo() -> (PathBuf, PathBuf) {
#[cfg(unix)]
#[cargo_test]
fn cargo_cmd_bin_prefer_over_path_if_not_included() {
let (_cargo_home_dir, _bin_dir) = set_up_cargo_foo();
let (_outside_dir, _inside_dir) = set_up_cargo_foo();

cargo_process("foo").with_stdout_contains("INSIDE").run();
}
Expand All @@ -393,12 +415,12 @@ fn cargo_cmd_bin_prefer_over_path_if_not_included() {
#[cfg(unix)]
#[cargo_test]
fn cargo_cmd_bin_respect_path_when_included() {
let (cargo_home_dir, bin_dir) = set_up_cargo_foo();
let (outside_dir, inside_dir) = set_up_cargo_foo();

cargo_process("foo")
.env(
"PATH",
format!("{}:{}", bin_dir.display(), cargo_home_dir.display()),
format!("{}:{}", inside_dir.display(), outside_dir.display()),
)
.with_stdout_contains("INSIDE")
.run();
Expand All @@ -407,15 +429,15 @@ fn cargo_cmd_bin_respect_path_when_included() {
// Note: trailing slash
.env(
"PATH",
format!("{}:{}/", bin_dir.display(), cargo_home_dir.display()),
format!("{}:{}/", inside_dir.display(), outside_dir.display()),
)
.with_stdout_contains("INSIDE")
.run();

cargo_process("foo")
.env(
"PATH",
format!("{}:{}", cargo_home_dir.display(), bin_dir.display()),
format!("{}:{}", outside_dir.display(), inside_dir.display()),
)
.with_stdout_contains("OUTSIDE")
.run();
Expand All @@ -424,7 +446,7 @@ fn cargo_cmd_bin_respect_path_when_included() {
// Note: trailing slash
.env(
"PATH",
format!("{}/:{}", cargo_home_dir.display(), bin_dir.display()),
format!("{}/:{}", outside_dir.display(), inside_dir.display()),
)
.with_stdout_contains("OUTSIDE")
.run();
Expand Down

0 comments on commit 691877c

Please sign in to comment.