diff --git a/docs/templates.md b/docs/templates.md index 767b123391..450160082b 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -128,6 +128,7 @@ These variables offer key information about the current environment: - `config_root: PathBuf` – Locates the directory containing your `mise.toml` file, or in the case of something like `~/src/myproj/.config/mise.toml`, it will point to `~/src/myproj`. - `mise_bin: String` - Points to the path to the current mise executable - `mise_pid: String` - Points to the pid of the current mise process +- `mise_env: String` - The configuration environment as specified by `MISE_ENV`, `-E`, or `--env`. Will be undefined if the configuration environment is not set. - `xdg_cache_home: PathBuf` - Points to the directory of XDG cache home - `xdg_config_home: PathBuf` - Points to the directory of XDG config home - `xdg_data_home: PathBuf` - Points to the directory of XDG data home diff --git a/e2e/env/test_env_profiles b/e2e/env/test_env_profiles index 2bdcc94a6d..e88d137561 100644 --- a/e2e/env/test_env_profiles +++ b/e2e/env/test_env_profiles @@ -7,3 +7,13 @@ echo 'env.AAA = "override-2"' >mise.override2.toml assert "mise env --json | jq -r .AAA" "main" MISE_ENV=override1 assert "mise env --json | jq -r .AAA" "override-1" MISE_ENV=override1,override2 assert "mise env --json | jq -r .AAA" "override-2" + +cat <mise.toml +[tasks.print] +run = '{% if mise_env %}echo {{mise_env}}{% endif %}' +EOF +assert "mise run print" "" +MISE_ENV=env1 assert "mise run print" "[env1]" +MISE_ENV=env1,env2 assert "mise run print" "[env1, env2]" +assert "mise --env env3 run print" "[env3]" +assert "mise -E env3,env4 run print" "[env3, env4]" diff --git a/src/tera.rs b/src/tera.rs index a5154d2ed1..628ab7dfb4 100644 --- a/src/tera.rs +++ b/src/tera.rs @@ -22,6 +22,9 @@ pub static BASE_CONTEXT: Lazy = Lazy::new(|| { context.insert("env", &*env::PRISTINE_ENV); context.insert("mise_bin", &*env::MISE_BIN); context.insert("mise_pid", &*env::MISE_PID); + if !(*env::MISE_ENV).is_empty() { + context.insert("mise_env", &*env::MISE_ENV); + } if let Ok(dir) = env::current_dir() { context.insert("cwd", &dir); } @@ -381,6 +384,11 @@ mod tests { assert_eq!(render("{{config_root}}"), "/"); } + #[test] + fn test_mise_env() { + assert_eq!(render("{% if mise_env %}{{mise_env}}{% endif %}"), ""); + } + #[test] fn test_cwd() { assert_eq!(render("{{cwd}}"), "/");