mise icon indicating copy to clipboard operation
mise copied to clipboard

Use mise tools in env template

Open abatilo opened this issue 1 year ago • 10 comments

I'd love to be able to use mise installed tools to execute env exec templates.

If I have the following example .mise.toml

[tools]
go = "latest"

[env]
EXAMPLE = "{{exec(command='go version')}}"

I'd hope that EXAMPLE would get populated with the output of go version.

Instead I see the following error:

bash: go: command not found
mise failed to parse template: '{{exec(command='go version')}}'
mise Failed to render '__tera_one_off'
mise Function call 'exec' failed
mise Io error while writing rendered value to output: Other
mise command ["bash", "-c", "go version"] exited with code 127
mise Run with --verbose or MISE_VERBOSE=1 for more information

I tried also using mise exec to execute the command but that didn't work either.

abatilo avatar Apr 28 '24 21:04 abatilo

the trouble is people also need some env vars to be loaded before tools. I wonder if maybe we could use [env.post] as a way to mark env vars that should be processed after tools are loaded or something.

jdx avatar Apr 28 '24 21:04 jdx

another idea:

[env]
EXAMPLE = {value="{{exec(command='go version')}}", when="after_tools"}

jdx avatar Apr 28 '24 21:04 jdx

Oh, that's an interesting idea. I hadn't considered the idea of putting hooks in with the env execution also but in hindsight it makes a lot of sense!

abatilo avatar Apr 28 '24 22:04 abatilo

An example of where this would be helpful at my work:

We're using mise to manage our local development environments. We're using FoundationDB with some go bindings that are use cgo to wrap the FoundationDB client libraries. We have foundationdb in the tools config section, and we're defining CGO_CPPFLAGS and other CGO_xxx environment variables in the env config section. These are all dependent on the install path for foundationdb, e.g.: ~/.local/share/mise/installs/foundationdb/x.y.z.

Right now we're using something like this:

[env]
# Needed for FoundationDB.
FDB_LOCATION = "{{exec(command='mise where foundationdb 2>/dev/null || true')}}"
CGO_CPPFLAGS = '-I{{ env.FDB_LOCATION }}/usr/local/include'

But it would be better if we could use something like

FDB_LOCATION = "{{tools(name='foundationdb').install_path}}"

or something like that.

Side note: Use of "{{exec(command='mise where ...')}}" seems to cause a fork-bomb with 2024.6.0 (mise calling mise calling mise etc.), which didn't happen with 2024.5.2.

richdawe-cio avatar Jun 03 '24 14:06 richdawe-cio

So the doc exemple:

[alias.node]
current = "{{exec(command='node --version')}}"

is a bit misleading in the sense that node must not be a mise installed version. I tried it and my terminal just freezes.

Should there exists a note in the docs?

fxsalazar avatar Sep 14 '24 10:09 fxsalazar

I like this feature.

Mise supports env._.source (#1447) and env._.run (#1448), so this improvement should consider them.

I prefer [env.post] for structure. Roughly:

[env]
SETUP_A_VARIABLE = 1
_.source = "./script-to-import-env.sh"
# or run a program to import env
_.run = "./import-env"

[env.post]
# Tools and environment variables are available here
ADDITIONAL_VARIABLE = "30"
ANOTHER_VARIABLE = "{{exec(command='node --version')}}"

# Import additional environment variables or execute commands
_.source = "./run-script-with-more-env.sh"
# or run a program
_.run = "./run-program"

erickguan avatar Oct 25 '24 09:10 erickguan