Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/dev-tools/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ It's also compatible
with asdf `.tool-versions` files as well as [idiomatic version files](/configuration#idiomatic-version-files) like `.node-version` and
`.ruby-version`. See [configuration](/configuration) for more details.

When specifying tool versions, you can also refer to environment variables defined in the same file, but note
that environment variables from referenced files are not resolved here.

::: info
mise is inspired by [asdf](https://asdf-vm.com) and can leverage asdf's
vast [plugin ecosystem](https://github.com/mise-plugins/registry)
Expand Down
4 changes: 4 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ Here is an example of a `mise.toml` file that uses templates:
```toml
[env]
PROJECT_NAME = "{{ cwd | basename }}"
TERRAFORM_VERSION = "1.0.0"

[tools]
# refers to env variable defined in this file
terraform = "{{ env.TERRAFORM_VERSION }}"
# refers to external env variable
node = "{{ get_env(name='NODE_VERSION', default='20') }}"
```

Expand Down
51 changes: 48 additions & 3 deletions src/config/config_file/mise_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::config::config_file::{config_root, toml::deserialize_arr};
use crate::config::env_directive::{AgeFormat, EnvDirective, EnvDirectiveOptions, RequiredValue};
use crate::config::settings::SettingsPartial;
use crate::config::{Alias, AliasMap, Config};
use crate::file;
use crate::env_diff::EnvMap;
use crate::file::{create_dir_all, display_path};
use crate::hooks::{Hook, Hooks};
use crate::redactions::Redactions;
Expand All @@ -31,6 +31,7 @@ use crate::task::Task;
use crate::tera::{BASE_CONTEXT, get_tera};
use crate::toolset::{ToolRequest, ToolRequestSet, ToolSource, ToolVersionOptions};
use crate::watch_files::WatchFile;
use crate::{env, file};

use super::{ConfigFileType, min_version::MinVersionSpec};

Expand Down Expand Up @@ -137,11 +138,13 @@ impl MiseToml {
"config_root",
config_root::config_root(path).to_str().unwrap(),
);
Self {
let mut rf = Self {
path: path.to_path_buf(),
context,
..Default::default()
}
};
rf.update_context_env(env::PRISTINE_ENV.clone());
rf
}

pub fn from_file(path: &Path) -> eyre::Result<Self> {
Expand All @@ -166,6 +169,7 @@ impl MiseToml {
rf.context = BASE_CONTEXT.clone();
rf.context
.insert("config_root", path.parent().unwrap().to_str().unwrap());
rf.update_context_env(env::PRISTINE_ENV.clone());
rf.path = path.to_path_buf();
let project_root = rf.project_root().map(|p| p.to_path_buf());
for task in rf.tasks.0.values_mut() {
Expand Down Expand Up @@ -356,6 +360,23 @@ impl MiseToml {
Ok(())
}

// Merge base OS env vars with env sections from this file,
// so they are available for templating.
// Note this only merges regular key-value variables; referenced files are not resolved.
fn update_context_env(&mut self, mut base_env: EnvMap) {
let env_vars = self
.env
.0
.iter()
.filter_map(|e| match e {
EnvDirective::Val(key, value, _) => Some((key.clone(), value.clone())),
_ => None,
})
.collect::<IndexMap<_, _>>();
base_env.extend(env_vars);
self.context.insert("env", &base_env);
}

fn parse_template(&self, input: &str) -> eyre::Result<String> {
self.parse_template_with_context(&self.context, input)
}
Expand Down Expand Up @@ -1731,6 +1752,30 @@ mod tests {
});
}

#[tokio::test]
async fn test_env_var_in_tool() {
let _config = Config::get().await.unwrap();
let p = CWD.as_ref().unwrap().join(".test.mise.toml");
file::write(
&p,
r#"
[env]
TERRAFORM_VERSION = '1.0.0'
JQ_PREFIX = '1.6'

[tools]
terraform = "{{env.TERRAFORM_VERSION}}"
jq = { prefix = "{{ env.JQ_PREFIX }}" }
"#,
)
.unwrap();
let cf = MiseToml::from_file(&p).unwrap();
assert_snapshot!(replace_path(&format!(
"{:#?}",
cf.to_tool_request_set().unwrap().tools
)));
}

#[tokio::test]
async fn test_env_array_valid() {
let _config = Config::get().await.unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
source: src/config/config_file/mise_toml.rs
expression: "replace_path(&format!(\"{:#?}\", cf.to_tool_request_set().unwrap().tools))"
---
{
BackendArg(terraform): [
Version {
backend: BackendArg(terraform),
version: "1.0.0",
options: ToolVersionOptions {
os: None,
install_env: {},
opts: {},
},
source: MiseToml(
"~/cwd/.test.mise.toml",
),
},
],
BackendArg(jq): [
Prefix {
backend: BackendArg(jq),
prefix: "1.6",
options: ToolVersionOptions {
os: None,
install_env: {},
opts: {},
},
source: MiseToml(
"~/cwd/.test.mise.toml",
),
},
],
}
Loading