Skip to content

Commit

Permalink
hook-env: keep prepended PATH entries in front (#1150)
Browse files Browse the repository at this point in the history
With this change, if you manually run PATH="/foo:$PATH" in
your shell after activating rtx, rtx will keep "/foo" in front of its own PATH entries.
Fixes #863
  • Loading branch information
jdx authored Dec 13, 2023
1 parent 784d153 commit 9160c4f
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 4 deletions.
7 changes: 6 additions & 1 deletion e2e/cd/test_bash
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ cd .. && _rtx_hook
test "$(node -v)" = "v20.0.0"
assert_path "/root:ROOT/e2e/cwd:INSTALLS/node/20.0.0/bin:INSTALLS/python/3.12.0/bin:INSTALLS/tiny/3.1.0/bin:INSTALLS/poetry/1.7.1/bin:INSTALLS/shellcheck/0.9.0/bin:INSTALLS/shfmt/3.6.0/bin"

export PATH="PRE:$PATH"
cd 18 && _rtx_hook
test "$(node -v)" = "v18.0.0"
assert_path "PRE:/root:ROOT/e2e/cwd:INSTALLS/node/18.0.0/bin:INSTALLS/python/3.12.0/bin:INSTALLS/tiny/3.1.0/bin:INSTALLS/poetry/1.7.1/bin:INSTALLS/shellcheck/0.9.0/bin:INSTALLS/shfmt/3.6.0/bin"

rtx shell [email protected] && _rtx_hook
test "$(node -v)" = "v18.0.0"

rtx deactivate
assert_path ""
assert_path "PRE"
16 changes: 13 additions & 3 deletions src/cli/hook_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,19 @@ impl HookEnv {
installs: &Vec<PathBuf>,
to_remove: &Vec<PathBuf>,
) -> Result<Vec<EnvDiffOperation>> {
let new_path = join_paths([installs.clone(), env::PATH.clone()].concat())?
.to_string_lossy()
.to_string();
let full = join_paths(&*env::PATH)?.to_string_lossy().to_string();
let (pre, post) = match &*env::__RTX_ORIG_PATH {
Some(orig_path) => match full.split_once(&format!(":{orig_path}")) {
Some((pre, post)) => (pre.to_string(), (orig_path.to_string() + post)),
None => (String::new(), full),
},
None => (String::new(), full),
};
let install_path = join_paths(installs)?.to_string_lossy().to_string();
let new_path = vec![pre, install_path, post]
.into_iter()
.filter(|p| !p.is_empty())
.join(":");
let mut ops = vec![EnvDiffOperation::Add("PATH".into(), new_path)];

if let Some(input) = env::DIRENV_DIFF.deref() {
Expand Down
1 change: 1 addition & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub static RTX_FETCH_REMOTE_VERSIONS_CACHE: Lazy<Option<Duration>> = Lazy::new(|
/// used to prevent infinite loops
pub static __RTX_SCRIPT: Lazy<bool> = Lazy::new(|| var_is_true("__RTX_SCRIPT"));
pub static __RTX_DIFF: Lazy<EnvDiff> = Lazy::new(get_env_diff);
pub static __RTX_ORIG_PATH: Lazy<Option<String>> = Lazy::new(|| var("__RTX_ORIG_PATH").ok());
pub static CI: Lazy<bool> = Lazy::new(|| var_is_true("CI"));
pub static PREFER_STALE: Lazy<bool> = Lazy::new(|| prefer_stale(&ARGS));
/// essentially, this is whether we show spinners or build output on runtime install
Expand Down
1 change: 1 addition & 0 deletions src/shell/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl Shell for Bash {
}
out.push_str(&formatdoc! {r#"
export RTX_SHELL=bash
export __RTX_ORIG_PATH="$PATH"
rtx() {{
local command
Expand Down
1 change: 1 addition & 0 deletions src/shell/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl Shell for Fish {
// https://github.com/direnv/direnv/blob/cb5222442cb9804b1574954999f6073cc636eff0/internal/cmd/shell_fish.go#L14-L36
out.push_str(&formatdoc! {r#"
set -gx RTX_SHELL fish
set -gx __RTX_ORIG_PATH $PATH
function rtx
if test (count $argv) -eq 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ expression: "bash.activate(exe, true)"
---
export PATH="/some/dir:$PATH"
export RTX_SHELL=bash
export __RTX_ORIG_PATH="$PATH"

rtx() {
local command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/shell/bash.rs
expression: "bash.activate(exe, true)"
---
export RTX_SHELL=bash
export __RTX_ORIG_PATH="$PATH"

rtx() {
local command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ expression: "fish.activate(exe, true)"
---
fish_add_path -g /some/dir
set -gx RTX_SHELL fish
set -gx __RTX_ORIG_PATH $PATH

function rtx
if test (count $argv) -eq 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/shell/fish.rs
expression: "fish.activate(exe, true)"
---
set -gx RTX_SHELL fish
set -gx __RTX_ORIG_PATH $PATH

function rtx
if test (count $argv) -eq 0
Expand Down
1 change: 1 addition & 0 deletions src/shell/snapshots/rtx__shell__zsh__tests__hook_init.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ expression: "zsh.activate(exe, true)"
---
export PATH="/some/dir:$PATH"
export RTX_SHELL=zsh
export __RTX_ORIG_PATH="$PATH"

rtx() {
local command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/shell/zsh.rs
expression: "zsh.activate(exe, true)"
---
export RTX_SHELL=zsh
export __RTX_ORIG_PATH="$PATH"

rtx() {
local command
Expand Down
1 change: 1 addition & 0 deletions src/shell/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl Shell for Zsh {
}
out.push_str(&formatdoc! {r#"
export RTX_SHELL=zsh
export __RTX_ORIG_PATH="$PATH"
rtx() {{
local command
Expand Down

0 comments on commit 9160c4f

Please sign in to comment.