From 637d6f6d37c64cc8c21b1d3a30a96e2a4c43342f Mon Sep 17 00:00:00 2001 From: jdx <216188+jdx@users.noreply.github.com> Date: Mon, 15 Dec 2025 06:14:18 -0600 Subject: [PATCH] fix(config): use correct config_root in tera context for hooks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a config file is in a .mise/ subdirectory (e.g., ~/src/foo/.mise/config.toml), the config_root template variable in hooks incorrectly resolved to the .mise/ directory instead of the project root. This was because MiseToml::from_str() was using path.parent() to set config_root in the tera context, which returns ~/src/foo/.mise/ for a config at ~/src/foo/.mise/config.toml. The fix uses config_root::config_root() which correctly calculates the project root by detecting .mise/ directories and returning the parent. Fixes: https://github.com/jdx/mise/discussions/6531 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- e2e/config/test_hooks_config_root | 26 ++++++++++++++++++++++++++ src/config/config_file/mise_toml.rs | 6 ++++-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 e2e/config/test_hooks_config_root diff --git a/e2e/config/test_hooks_config_root b/e2e/config/test_hooks_config_root new file mode 100644 index 0000000000..80eba323da --- /dev/null +++ b/e2e/config/test_hooks_config_root @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +# Test that config_root in hooks correctly points to the project root +# even when the config file is in a .mise/ subdirectory +# See: https://github.com/jdx/mise/discussions/6531 + +# Create project structure with .mise/config.toml +mkdir -p .mise +cat <.mise/config.toml +[hooks] +enter = 'echo CONFIG_ROOT={{config_root}}' +EOF + +# Initialize mise and go to home first +eval "$(mise hook-env)" + +# Leave the workdir (go home) +cd ~ || exit 1 +eval "$(mise hook-env)" + +# Now re-enter the workdir to trigger the enter hook +cd ~/workdir || exit 1 + +# The config_root should be ~/workdir, not ~/workdir/.mise +assert_contains "mise hook-env 2>&1" "CONFIG_ROOT=$HOME/workdir" +assert_not_contains "mise hook-env 2>&1" "CONFIG_ROOT=$HOME/workdir/.mise" diff --git a/src/config/config_file/mise_toml.rs b/src/config/config_file/mise_toml.rs index e6c522b752..e062752b5b 100644 --- a/src/config/config_file/mise_toml.rs +++ b/src/config/config_file/mise_toml.rs @@ -167,8 +167,10 @@ impl MiseToml { } }; rf.context = BASE_CONTEXT.clone(); - rf.context - .insert("config_root", path.parent().unwrap().to_str().unwrap()); + rf.context.insert( + "config_root", + config_root::config_root(path).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());