Skip to content

Commit 922caa9

Browse files
committed
support RTX_ADD_PATH
This is an env var that poetry/pipenv use to add VIRTUAL_ENV/bin to PATH while not also creating shims for everything in there. See #897
1 parent 7232723 commit 922caa9

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/cli/direnv/envrc.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ impl Envrc {
4141
writeln!(file, "watch_file {}", cf.to_string_lossy())?;
4242
}
4343
for (k, v) in ts.env(&config) {
44-
writeln!(
45-
file,
46-
"export {}={}",
47-
shell_escape::unix::escape(k.into()),
48-
shell_escape::unix::escape(v.into()),
49-
)?;
44+
if k == "PATH" {
45+
writeln!(file, "PATH_add {}", v)?;
46+
} else {
47+
writeln!(
48+
file,
49+
"export {}={}",
50+
shell_escape::unix::escape(k.into()),
51+
shell_escape::unix::escape(v.into()),
52+
)?;
53+
}
5054
}
5155
for path in ts.list_paths(&config).into_iter().rev() {
5256
writeln!(file, "PATH_add {}", path.to_string_lossy())?;

src/cli/hook_env.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::cmp::max;
2-
use std::env::join_paths;
2+
use std::env::{join_paths, split_paths};
33
use std::ops::Deref;
44
use std::path::PathBuf;
55

@@ -41,12 +41,16 @@ impl HookEnv {
4141
.build(&mut config)?;
4242
let shell = get_shell(self.shell).expect("no shell provided, use `--shell=zsh`");
4343
out.stdout.write(hook_env::clear_old_env(&*shell));
44-
let env = ts.env(&config);
44+
let mut env = ts.env(&config);
45+
let env_path = env.remove("PATH");
4546
let mut diff = EnvDiff::new(&env::PRISTINE_ENV, env);
4647
let mut patches = diff.to_patches();
4748

4849
let mut paths = config.path_dirs.clone();
4950
paths.extend(ts.list_paths(&config)); // load the active runtime paths
51+
if let Some(p) = env_path {
52+
paths.extend(split_paths(&p).collect_vec());
53+
}
5054
diff.path = paths.clone(); // update __RTX_DIFF with the new paths for the next run
5155

5256
patches.extend(self.build_path_operations(&paths, &__RTX_DIFF.path)?);

src/toolset/mod.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,15 @@ impl Toolset {
287287
}
288288
pub fn env_with_path(&self, config: &Config) -> BTreeMap<String, String> {
289289
let mut env = self.env(config);
290-
let path_env = self.path_env(config);
290+
let mut path_env = self.path_env(config);
291+
if let Some(path) = env.get("PATH") {
292+
path_env = format!("{}:{}", path, path_env);
293+
}
291294
env.insert("PATH".to_string(), path_env);
292295
env
293296
}
294297
pub fn env(&self, config: &Config) -> BTreeMap<String, String> {
295-
let mut entries: BTreeMap<String, String> = self
298+
let entries = self
296299
.list_current_installed_versions(config)
297300
.into_par_iter()
298301
.flat_map(|(p, tv)| match p.exec_env(config, self, &tv) {
@@ -302,12 +305,21 @@ impl Toolset {
302305
Vec::new()
303306
}
304307
})
305-
.collect::<Vec<(String, String)>>()
308+
.collect::<Vec<(String, String)>>();
309+
let add_paths = entries
310+
.iter()
311+
.filter(|(k, _)| k == "RTX_ADD_PATH")
312+
.map(|(_, v)| v)
313+
.join(":");
314+
let mut entries: BTreeMap<String, String> = entries
306315
.into_iter()
307316
.filter(|(k, _)| k != "RTX_ADD_PATH")
308317
.filter(|(k, _)| !k.starts_with("RTX_TOOL_OPTS__"))
309318
.rev()
310319
.collect();
320+
if !add_paths.is_empty() {
321+
entries.insert("PATH".to_string(), add_paths);
322+
}
311323
entries.extend(config.env.clone());
312324
entries
313325
}

0 commit comments

Comments
 (0)