Skip to content

Commit

Permalink
fix: use vars in [env] templates (#4100)
Browse files Browse the repository at this point in the history
  • Loading branch information
hverlin authored Jan 14, 2025
1 parent b5a5573 commit 97fee36
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
27 changes: 24 additions & 3 deletions e2e/env/test_env_template
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ cat <<EOF >mise.toml
[env]
ENV_TMPL_EXEC = "{{ exec(command='echo foo') }}"
EOF
assert "mise env -s zsh | grep ENV_TMPL_EXEC" "export ENV_TMPL_EXEC=foo"
assert_contains "mise env -s zsh | grep ENV_TMPL_EXEC" "export ENV_TMPL_EXEC=foo"

cat <<EOF >mise.toml
[env]
B = "{{ env.A }}"
EOF
A=a assert "mise env -s bash | grep B" "export B=a"
A=a assert_contains "mise env -s bash | grep B" "export B=a"

cat <<EOF >mise.toml
[env]
A = "a"
B = "{{ env.A }}"
EOF
assert "mise env -s bash | grep B" "export B=a"
assert_contains "mise env -s bash | grep B" "export B=a"

cat <<EOF >mise.toml
[env]
Expand Down Expand Up @@ -61,3 +61,24 @@ cat <<EOF >env.sh
export BAR="\$PATH"
EOF
assert_contains "mise env -s bash | grep BAR" "export BAR='/foo:"

cat <<EOF >mise.toml
[vars]
MY_VAR = "my_var"
[env]
FOO = "{{vars.MY_VAR}}"
EOF

assert_json_partial_object "mise env --json" "FOO" '{
"FOO": "my_var"
}'

cat <<EOF >mise.local.toml
[vars]
MY_VAR = "mise.local.toml"
EOF

assert_json_partial_object "mise env --json" "FOO" '{
"FOO": "mise.local.toml"
}'
19 changes: 13 additions & 6 deletions src/config/env_directive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::path_env::PathEnv;
use crate::tera::{get_tera, tera_exec};
use eyre::{eyre, Context};
use indexmap::IndexMap;
use serde_json::Value;
use std::cmp::PartialEq;
use std::collections::{BTreeSet, HashMap};
use std::fmt::{Debug, Display, Formatter};
Expand Down Expand Up @@ -200,13 +201,19 @@ impl EnvResults {
.map(|(k, (v, _))| (k.clone(), v.clone()))
.collect::<EnvMap>();
ctx.insert("env", &env_vars);
ctx.insert(
"vars",
&r.vars

let mut vars: EnvMap = if let Some(Value::Object(existing_vars)) = ctx.get("vars") {
existing_vars
.iter()
.map(|(k, (v, _))| (k.clone(), v.clone()))
.collect::<EnvMap>(),
);
.filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string())))
.collect()
} else {
EnvMap::new()
};

vars.extend(r.vars.iter().map(|(k, (v, _))| (k.clone(), v.clone())));

ctx.insert("vars", &vars);
let redact = directive.options().redact;
// trace!("resolve: ctx.get('env'): {:#?}", &ctx.get("env"));
match directive {
Expand Down

0 comments on commit 97fee36

Please sign in to comment.