From 97fee3645d343931965a0a43fd47fc4bf515c200 Mon Sep 17 00:00:00 2001 From: Hugues Verlin Date: Tue, 14 Jan 2025 16:48:37 +0100 Subject: [PATCH] fix: use vars in [env] templates (#4100) --- e2e/env/test_env_template | 27 ++++++++++++++++++++++++--- src/config/env_directive/mod.rs | 19 +++++++++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/e2e/env/test_env_template b/e2e/env/test_env_template index 43107b9ad9..b38ddad415 100644 --- a/e2e/env/test_env_template +++ b/e2e/env/test_env_template @@ -4,20 +4,20 @@ cat <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 <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 <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 <mise.toml [env] @@ -61,3 +61,24 @@ cat <env.sh export BAR="\$PATH" EOF assert_contains "mise env -s bash | grep BAR" "export BAR='/foo:" + +cat <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 <mise.local.toml +[vars] +MY_VAR = "mise.local.toml" +EOF + +assert_json_partial_object "mise env --json" "FOO" '{ + "FOO": "mise.local.toml" +}' diff --git a/src/config/env_directive/mod.rs b/src/config/env_directive/mod.rs index 3d8f50b8f8..421736e393 100644 --- a/src/config/env_directive/mod.rs +++ b/src/config/env_directive/mod.rs @@ -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}; @@ -200,13 +201,19 @@ impl EnvResults { .map(|(k, (v, _))| (k.clone(), v.clone())) .collect::(); 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::(), - ); + .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 {