jdx / mise

dev tools, env vars, task runner
https://mise.jdx.dev
MIT License
10.39k stars 297 forks source link

env "over-evaluated" times leading to incorrect results #2611

Open adrianomitre opened 2 months ago

adrianomitre commented 2 months ago

Describe the bug

To Reproduce

[env] in .mise.toml "over-evaluated" times leading to ever increasing, incorrect results.

Given ~/.mise.toml:

[env]
LD_LIBRARY_PATH = "{{ get_env(name='LD_LIBRARY_PATH', default='') }}:{{ env.BREW }}/lib"
$ cd ~
$ echo $LD_LIBRARY_PATH 
:/home/linuxbrew/.linuxbrew/lib

$ cd ~/some-folder # does not contain .mise.toml
$ echo $LD_LIBRARY_PATH 
:/home/linuxbrew/.linuxbrew/lib:/home/linuxbrew/.linuxbrew/lib

$ cd -
$ echo $LD_LIBRARY_PATH 
:/home/linuxbrew/.linuxbrew/lib:/home/linuxbrew/.linuxbrew/lib:/home/linuxbrew/.linuxbrew/lib

Expected behavior

The value of LD_LIBRARY_PATH should be :/home/linuxbrew/.linuxbrew/lib in all the cases above.

mise doctor output

version: 2024.9.6 linux-x64 (1c0bc95 2024-09-18)
activated: yes
shims_on_path: no

build_info: 
  Target: x86_64-unknown-linux-gnu
  Features: DEFAULT, NATIVE_TLS, OPENSSL
  Built: Wed, 18 Sep 2024 21:37:42 +0000
  Rust Version: rustc 1.81.0 (eeb90cda1 2024-09-04)
  Profile: release

shell: 
  /bin/bash
  GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
  Copyright (C) 2022 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

  This is free software; you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.

dirs: 
  data: ~/.local/share/mise
  config: ~/.config/mise
  cache: ~/.cache/mise
  state: ~/.local/state/mise
  shims: ~/.local/share/mise/shims

config_files: 
  ~/.tool-versions
  ~/.mise.toml
  ~/Projects/Guideline/app/.nvmrc
  ~/Projects/Guideline/app/.ruby-version
  ~/Projects/Guideline/app/Gemfile
  ~/Projects/Guideline/app/.tool-versions

backends: 
  cargo
  core
  go
  npm
  pipx
  spm
  ubi
  vfox

plugins: 
  actionlint  https://github.com/crazy-matt/asdf-actionlint.git#f8df3cf
  earthly     https://github.com/YR-ZR0/asdf-earthly#58c78cb
  github-cli  https://github.com/bartlomiejdanek/asdf-github-cli.git#e0605b7
  jq          https://github.com/mise-plugins/asdf-jq.git#6d86d19
  shellcheck  https://github.com/luizm/asdf-shellcheck.git#66200ff
  shfmt       https://github.com/luizm/asdf-shfmt.git#a42c5ff
  terraform   https://github.com/asdf-community/asdf-hashicorp.git#f1602b6

toolset: 
  ruby@3.2.5         
  node@20.17.0       
  python@3.12.5      
  terraform@1.9.5    
  earthly@0.8.15     
  shellcheck@0.9.0   
  github-cli@2.55.0  
  shfmt@3.7.0        
  actionlint@1.6.26  
  jq@1.7             

env_vars: 
  MISE_SHELL=bash

settings: 
  activate_aggressive = false
  all_compile = false
  always_keep_download = false
  always_keep_install = false
  asdf = true
  asdf_compat = false
  cache_prune_age = "30d"
  cargo_binstall = true
  color = true
  disable_default_shorthands = false
  disable_hints = []
  disable_tools = []
  experimental = false
  go_default_packages_file = "~/.default-go-packages"
  go_download_mirror = "https://dl.google.com/go"
  go_repo = "https://github.com/golang/go"
  go_set_gopath = false
  go_set_goroot = true
  go_skip_checksum = false
  http_timeout = 30
  jobs = 4
  legacy_version_file = true
  legacy_version_file_disable_tools = []
  libgit2 = true
  node_compile = false
  not_found_auto_install = true
  paranoid = false
  pipx_uvx = false
  plugin_autoupdate_last_check_duration = "7d"
  python_default_packages_file = "~/.default-python-packages"
  python_pyenv_repo = "https://github.com/pyenv/pyenv.git"
  raw = false
  trusted_config_paths = []
  quiet = false
  use_versions_host = true
  verbose = false
  vfox = false
  yes = false
  ci = false
  debug = false
  trace = false
  log_level = "info"
  python_venv_auto_create = false

  [ruby]
  default_packages_file = "~/.default-gems"
  ruby_build_repo = "https://github.com/rbenv/ruby-build.git"
  ruby_install = false
  ruby_install_repo = "https://github.com/postmodern/ruby-install.git"

  [status]
  missing_tools = "if_other_versions_installed"
  show_env = false
  show_tools = false

No warnings found
No problems found
jdx commented 2 days ago

this would be a challenging this to fix I'm afraid. We have some logic for this specific to PATH but it's quite complex and I don't know if it could be reused or how easy that might be since I think it's split across the codebase.

I would just do export ORIGINAL_LD_LIBRARY_PATH=$LD_LIBRARY_PATH in your rc file then use that with mise so it is reading from an env var it's not modifying

josb commented 2 days ago

One reason the delayed PATH entries logic is complicated is because it operates on a list variable instead of a scalar. Plus a good amount of my PR was about parsing the delayed evaluation entries in the TOML configs.

jdx commented 2 days ago

I'm not sure if the tera templates are using PRISTINE_ENV (which is the current env vars minus mise modifications) or not but they probably should. I suppose if mise was using PRISTINE_ENV and it was working correctly this shouldn't be a problem

jdx commented 2 days ago

oh interesting, looks like we set env to PRISTINE_ENV as I thought: https://github.com/jdx/mise/blob/65694784bc1a407b34f51df6b6376e5ae23db85c/src/tera.rs#L19

however you're using get_env() which is a tera built-in function which doesn't use PRISTINE_ENV—hence why this is happening. I would try just switching to env. If that's the case we should update the docs. Not really to admonish using get_env() because sometimes you may actually want that behavior.