Skip to content

Commit

Permalink
feat: allow inserting PATH in env._.source
Browse files Browse the repository at this point in the history
Fixes #3646
  • Loading branch information
jdx committed Dec 18, 2024
1 parent 577ff7d commit 3244a43
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 76 deletions.
90 changes: 45 additions & 45 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions e2e/env/test_env_source
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ EOF

cat >"$MISE_CONFIG_DIR/source.sh" <<EOF
export MISE_TEST_SOURCE=1234
export PATH="$HOME/newbin:$PATH"
EOF

assert "mise env -s bash | grep MISE_TEST_SOURCE" "export MISE_TEST_SOURCE=1234"
assert_contains "mise env -s bash | grep PATH" "export PATH='$HOME/newbin:"
8 changes: 0 additions & 8 deletions e2e/tasks/test_task_help

This file was deleted.

2 changes: 1 addition & 1 deletion mise.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ version = "0.2.3"
backend = "cargo:toml-cli"

[tools."cargo:usage-cli"]
version = "1.7.1"
version = "1.6.0"
backend = "cargo:usage-cli"

[tools.cosign]
Expand Down
2 changes: 1 addition & 1 deletion src/backend/asdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl AsdfBackend {
}
let script = sm.get_script_path(&ExecEnv);
let dir = dirs::CWD.clone().unwrap_or_default();
let ed = EnvDiff::from_bash_script(&script, &dir, &sm.env)?;
let ed = EnvDiff::from_bash_script(&script, &dir, &sm.env, Default::default())?;
let env = ed
.to_patches()
.into_iter()
Expand Down
1 change: 1 addition & 0 deletions src/config/env_directive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ impl EnvResults {
&mut ctx,
&mut tera,
&mut env,
&mut paths,
&mut r,
normalize_path,
&source,
Expand Down
22 changes: 18 additions & 4 deletions src/config/env_directive/source.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::config::env_directive::EnvResults;
use crate::env_diff::{EnvDiff, EnvDiffOperation, EnvMap};
use crate::env_diff::{EnvDiff, EnvDiffOperation, EnvDiffOptions, EnvMap};
use indexmap::IndexMap;
use std::path::{Path, PathBuf};
use crate::env;

impl EnvResults {
#[allow(clippy::too_many_arguments)]
pub fn source(
ctx: &mut tera::Context,
tera: &mut tera::Tera,
env: &mut IndexMap<String, (String, Option<PathBuf>)>,
paths: &mut Vec<(PathBuf, PathBuf)>,
r: &mut EnvResults,
normalize_path: fn(&Path, PathBuf) -> PathBuf,
source: &Path,
Expand All @@ -19,13 +21,25 @@ impl EnvResults {
if let Ok(s) = r.parse_template(ctx, tera, source, &input) {
for p in xx::file::glob(normalize_path(config_root, s.into())).unwrap_or_default() {
r.env_scripts.push(p.clone());
let env_diff = EnvDiff::from_bash_script(&p, config_root, env_vars.clone())
let orig_path = env_vars.get(&*env::PATH_KEY).cloned().unwrap_or_default();
let mut env_diff_opts = EnvDiffOptions::default();
env_diff_opts.ignore_keys.shift_remove(&*env::PATH_KEY); // allow modifying PATH
let env_diff = EnvDiff::from_bash_script(&p, config_root, env_vars.clone(), env_diff_opts)
.unwrap_or_default();
for p in env_diff.to_patches() {
match p {
EnvDiffOperation::Add(k, v) | EnvDiffOperation::Change(k, v) => {
r.env_remove.remove(&k);
env.insert(k.clone(), (v.clone(), Some(source.to_path_buf())));
if k == *env::PATH_KEY {
// TODO: perhaps deal with path removals as well
if let Some(new_path) = v.strip_suffix(&orig_path) {
for p in env::split_paths(new_path) {
paths.push((p, source.to_path_buf()));
}
}
} else {
r.env_remove.remove(&k);
env.insert(k.clone(), (v.clone(), Some(source.to_path_buf())));
}
}
EnvDiffOperation::Remove(k) => {
env.shift_remove(&k);
Expand Down
Loading

0 comments on commit 3244a43

Please sign in to comment.