Skip to content

Commit

Permalink
feat: lazy env eval
Browse files Browse the repository at this point in the history
Fixes #1912
  • Loading branch information
jdx committed Dec 16, 2024
1 parent 3eb8fae commit 7f23e0d
Show file tree
Hide file tree
Showing 18 changed files with 404 additions and 212 deletions.
21 changes: 21 additions & 0 deletions docs/environments/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ NODE_ENV development mise.toml
$ mise unset NODE_ENV
```

## Lazy eval

Environment variables typically are resolved before tools—that way you can configure tool installation
with environment variables. However, sometimes you want to access environment variables produced by
tools. To do that, turn the value into a map with `tools = true`:

```toml
[env]
MY_VAR = { value = "tools path: {{env.PATH}}", tools = true }
_.path = { value = ["{{env.GEM_HOME}}/bin"], tools = true } # directives may also set tools = true
```

## `env._` directives

`env._.*` define special behavior for setting environment variables. (e.g.: reading env vars
Expand All @@ -57,6 +69,15 @@ not to mise since there is not much mise can do about the way that crate works.
Or set [`MISE_ENV_FILE=.env`](/configuration#mise-env-file) to automatically load dotenv files in any
directory.

You can also use json or yaml files:

```toml
[env]
_.file = '.env.json'
```

See [secrets](/encironments/secrets) for ways to read encrypted files with `env._.file`.

### `env._.path`

`PATH` is treated specially, it needs to be defined as a string/array in `mise.path`:
Expand Down
18 changes: 18 additions & 0 deletions e2e/config/test_config_post_tools
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

cat <<EOF >mise.toml
[[env]]
A_PATH = "foo: {{ env.PATH }}"
B_PATH = { value = "foo: {{ env.PATH }}", tools = true }
[[env]]
_.path = {value = "tiny-{{env.JDXCODE_TINY}}-tiny", tools = true}
[tools]
tiny = "1.0.0"
EOF

mise i
assert_not_contains "mise env | grep A_PATH" "tiny"
assert_contains "mise env | grep B_PATH" "tiny"

assert_contains "mise dr path" "tiny-1.0.0-tiny"
1 change: 1 addition & 0 deletions mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ cmd "doctor" help="Check mise installation for possible problems" {
[WARN] plugin node is not installed
"
cmd "path" help="Print the current PATH entries mise is providing" {
alias "paths" hide=true
after_long_help r"Examples:

Get the current PATH entries mise is providing
Expand Down
2 changes: 1 addition & 1 deletion src/cli/doctor/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::env;

/// Print the current PATH entries mise is providing
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
#[clap(alias="paths", verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct Path {
/// Print all entries including those not provided by mise
#[clap(long, short, verbatim_doc_comment)]
Expand Down
4 changes: 2 additions & 2 deletions src/cli/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Set {
if env_vars.len() == 1 && env_vars[0].value.is_none() {
let key = &env_vars[0].key;
match config.env_entries()?.into_iter().find_map(|ev| match ev {
EnvDirective::Val(k, v) if &k == key => Some(v),
EnvDirective::Val(k, v, _) if &k == key => Some(v),
_ => None,
}) {
Some(value) => miseprintln!("{value}"),
Expand Down Expand Up @@ -122,7 +122,7 @@ impl Set {
.env_entries()?
.into_iter()
.filter_map(|ed| match ed {
EnvDirective::Val(key, value) => Some(Row {
EnvDirective::Val(key, value, _) => Some(Row {
key,
value,
source: display_path(file),
Expand Down
Loading

0 comments on commit 7f23e0d

Please sign in to comment.