mise icon indicating copy to clipboard operation
mise copied to clipboard

Lazily evaluated env template variables in path entries

Open josb opened this issue 1 year ago • 8 comments

Setup:

  • HOME=/Users/john
  • A global config.tomlfile with:
    • a _.path template entry with value "{{env.GEM_HOME}}/bin"
    • an env entry: GEM_HOME = "{{env.HOME}}/.gem"
  • A .mise.toml file in some directory under the home directory with:
    • an env entry: GEM_HOME = "{{env.HOME}}/.mise-gemsets/foo"

Today, env entries in path templates are expanded using the environment variables defined in the same mise config file, either global or local.

The idea behind a lazily expanded reference in path entries is to only expand the path env reference after both the global and local mise configs have been read, and use whatever value the env variable has at that point.

In the above example, this would mean that in the specific directory a path entry valued /Users/john/.mise-gemsets/foo/bin would be added, whereas elsewhere the entry /Users/john/.gem/bin would be added instead.

Today this doesn't work because the path entry has to be set in the global config where it is always added with the global value, which is not what we want here.

If there's another way to achieve this with existing mise facilities I'd love to learn about it. Thanks!

josb avatar Apr 17 '24 03:04 josb

@jdx here's a demo of my current branch, which allows lazy path entries to be specified using { value = "lazy_path_entry" } in addition to the existing "normal_path_entry". The effect is that the same positional PATH entry is either controlled by the global mise config, or by a local mise config in some directory, as can be seen below:

~/.config/mise/config.toml:

[env]
GEM_HOME = "{{env.HOME}}/.gem"
_.path = [
  { value = "{{env.GEM_HOME}}/bin" },
]

~/github/josb/.mise.toml:

[env]
GEM_HOME = "{{env.HOME}}/.mise-gemsets/foo"
~% mise env
export GEM_HOME=/Users/josb/.gem
export PATH='/Users/josb/.gem/bin:/opt/homebrew/opt/ruby/bin:...
~% cd ~/github/josb
~/github/josb% mise env
export GEM_HOME=/Users/josb/.mise-gemsets/foo
export PATH='/Users/josb/.mise-gemsets/foo/bin:/opt/homebrew/opt/ruby/bin:...
~/github/josb% 

Normal _.path entries also still work, of course.

Feedback welcome.

josb avatar Jun 18 '24 02:06 josb

there's another ticket (didn't have time to search for it) where we've talked about being able to configure some env vars to be parsed after tools are loaded that I think would be a prerequisite for doing this

jdx avatar Jun 19 '24 13:06 jdx

Do you mean https://github.com/jdx/mise/issues/1982? That seems to not concern env var evaluation in env.path entries?

josb avatar Jun 19 '24 17:06 josb

Oh sorry, I thought you wanted it to run after the tools are loaded, this makes sense

jdx avatar Jun 19 '24 20:06 jdx

I seem to not be able to view your branch, not sure if that's because I'm on my phone

jdx avatar Jun 19 '24 20:06 jdx

Hm. I'll open a PR in your repo later today so you take a look.

josb avatar Jun 19 '24 21:06 josb

oh it works on my computer, go ahead and submit a PR—first glance it looks solid 👍

jdx avatar Jun 19 '24 21:06 jdx

@jdx FYI https://github.com/jdx/mise/pull/2310

josb avatar Jun 27 '24 21:06 josb