From f4c17ec2d1327934cd927f85aefabb519b59521a Mon Sep 17 00:00:00 2001 From: Taku Kodama <79110363+risu729@users.noreply.github.com> Date: Sun, 19 Jul 2026 04:55:33 +1000 Subject: [PATCH 1/7] fix(task): deduplicate global tasks by source --- e2e/tasks/test_task_config_precedence | 67 --------------------------- e2e/tasks/test_task_ls_global | 17 +++++++ src/config/mod.rs | 27 ++++++++--- src/file.rs | 37 +++++++++++++++ 4 files changed, 75 insertions(+), 73 deletions(-) delete mode 100644 e2e/tasks/test_task_config_precedence diff --git a/e2e/tasks/test_task_config_precedence b/e2e/tasks/test_task_config_precedence deleted file mode 100644 index 7595067b27c..00000000000 --- a/e2e/tasks/test_task_config_precedence +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env bash - -mkdir -p .config/mise/conf.d .mise/tasks - -cat <<'EOF' >.config/mise.toml -[tasks.a] -description = ".config/a" -run = "echo .config/a" - -[tasks.b] -description = ".config/b" -alias = "low" -env = { LOWER_PRECEDENCE = "applied" } -EOF - -cat <<'EOF' >.mise/config.toml -[tasks.a] -description = ".mise/a" -run = "echo .mise/a" - -[tasks.b] -description = ".mise/b" -alias = "high" -env = { HIGHER_PRECEDENCE = "applied" } -EOF - -cat <<'EOF' >.mise/tasks/b -#!/usr/bin/env bash -echo "HIGHER_PRECEDENCE=${HIGHER_PRECEDENCE:-}" -echo "LOWER_PRECEDENCE=${LOWER_PRECEDENCE:-}" -EOF -chmod +x .mise/tasks/b - -assert_contains "mise tasks" "a .mise/a" -assert_not_contains "mise tasks" "a .config/a" -assert_contains "mise run a" ".mise/a" -assert_not_contains "mise run a" ".config/a" - -# A script receives only the highest-precedence inline definition. Additive -# fields such as aliases and env must not accumulate from lower configs. -assert_contains "mise tasks" "b .mise/b" -assert_contains "mise run high" "HIGHER_PRECEDENCE=applied" -assert_contains "mise run high" "LOWER_PRECEDENCE=" -assert_fail "mise run low" - -cat <<'EOF' >.config/mise/conf.d/01-base.toml -[tasks.c] -description = "conf.d/01/c" -env = { LOWER_PRECEDENCE = "applied" } -EOF - -cat <<'EOF' >.config/mise/conf.d/02-override.toml -[tasks.c] -description = "conf.d/02/c" -env = { HIGHER_PRECEDENCE = "applied" } -EOF - -cat <<'EOF' >.mise/tasks/c -#!/usr/bin/env bash -echo "HIGHER_PRECEDENCE=${HIGHER_PRECEDENCE:-}" -echo "LOWER_PRECEDENCE=${LOWER_PRECEDENCE:-}" -EOF -chmod +x .mise/tasks/c - -assert_contains "mise tasks" "c conf.d/02/c" -assert_contains "mise run c" "HIGHER_PRECEDENCE=applied" -assert_contains "mise run c" "LOWER_PRECEDENCE=" diff --git a/e2e/tasks/test_task_ls_global b/e2e/tasks/test_task_ls_global index 8961dacf6a7..f71b589ee50 100644 --- a/e2e/tasks/test_task_ls_global +++ b/e2e/tasks/test_task_ls_global @@ -43,3 +43,20 @@ myglobalfile systemglobalfile" assert "mise run customglobalfile" "PROJECT_ROOT=$PWD" assert "mise run systemglobalfile" "PROJECT_ROOT=$PWD" + +# A local ancestor config can explicitly include the global task directory even +# when the global config replaces that default include. Source deduplication +# must not discard a task that the independent global pass did not load. +cat >"$HOME/mise.toml" <<'TOML' +[task_config] +includes = [".config/mise/tasks"] +TOML +cat >"$HOME/.config/mise/config.toml" <<'TOML' +tasks.myglobal = { run = 'echo myglobal' } + +[task_config] +includes = ["dotfiles/tasks"] +TOML + +assert "mise run myglobalfile" "myglobalfile" +assert_contains "mise tasks --global" "myglobalfile" diff --git a/src/config/mod.rs b/src/config/mod.rs index 30d7a93cd65..1a11a029a86 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -696,8 +696,13 @@ impl Config { let templates = collect_task_templates(&config.config_files); - let local_tasks = load_local_tasks_with_context(&config, ctx, &templates).await?; + let mut local_tasks = load_local_tasks_with_context(&config, ctx, &templates).await?; let global_tasks = load_global_tasks(&config, &templates).await?; + local_tasks.retain(|local| { + !global_tasks + .iter() + .any(|global| tasks_have_same_source(local, global)) + }); let mut tasks: BTreeMap = local_tasks .into_iter() .chain(global_tasks) @@ -1709,6 +1714,21 @@ fn path_starts_with_resolved(path: &Path, prefix: &Path) -> bool { path.starts_with(prefix) || file::desymlink_path(path).starts_with(file::desymlink_path(prefix)) } +fn tasks_have_same_source(left: &Task, right: &Task) -> bool { + if left.name != right.name { + return false; + } + match (left.file.as_deref(), right.file.as_deref()) { + (Some(left), Some(right)) => paths_equal_resolved(left, right), + (None, None) => { + !left.config_source.as_os_str().is_empty() + && !right.config_source.as_os_str().is_empty() + && paths_equal_resolved(&left.config_source, &right.config_source) + } + _ => false, + } +} + /// Returns true if the path should be filtered out due to MISE_CONFIG_DIR override. /// When MISE_CONFIG_DIR is set to a non-default location, this filters out configs /// found under the default location (~/.config/mise) during traversal. @@ -2628,11 +2648,6 @@ async fn load_local_tasks_with_context( continue; } let mut dir_tasks = load_tasks_in_dir(config, &d, &local_config_files, templates).await?; - if paths_equal_resolved(&d, env::MISE_GLOBAL_CONFIG_ROOT.as_path()) { - // Walking ancestors can reach the global task directory under HOME. - // Global tasks are loaded with their config overlays in a separate pass. - dir_tasks.retain(|task| !task.global); - } if let Some(ref monorepo_root) = monorepo_root { prefix_monorepo_task_names(&mut dir_tasks, &d, monorepo_root); diff --git a/src/file.rs b/src/file.rs index 61f45010f51..76f84643f60 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1565,6 +1565,11 @@ pub fn desymlink_path(p: &Path) -> PathBuf { if p.is_symlink() && let Ok(target) = fs::read_link(p) { + let target = if target.is_absolute() { + target + } else { + p.parent().unwrap_or_else(|| Path::new("")).join(target) + }; return target .canonicalize() .unwrap_or_else(|_| target.to_path_buf()); @@ -1952,6 +1957,38 @@ mod tests { assert_eq!(run_blocking(|| 42), 42); } + #[cfg(unix)] + #[test] + fn test_desymlink_path_resolves_relative_target_from_link_parent() { + use std::os::unix::fs::symlink; + + let root = tempfile::tempdir().unwrap(); + let target_dir = root.path().join("target"); + let link_dir = root.path().join("links"); + fs::create_dir_all(&target_dir).unwrap(); + fs::create_dir_all(&link_dir).unwrap(); + let target = target_dir.join("file"); + fs::write(&target, "test").unwrap(); + let link = link_dir.join("file"); + symlink("../target/file", &link).unwrap(); + + assert_eq!(desymlink_path(&link), target.canonicalize().unwrap()); + } + + #[cfg(unix)] + #[test] + fn test_desymlink_path_preserves_absolute_target() { + use std::os::unix::fs::symlink; + + let root = tempfile::tempdir().unwrap(); + let target = root.path().join("target"); + fs::write(&target, "test").unwrap(); + let link = root.path().join("link"); + symlink(&target, &link).unwrap(); + + assert_eq!(desymlink_path(&link), target.canonicalize().unwrap()); + } + #[test] fn test_retry_remove_all_retries_directory_not_empty() { let mut attempts = 0; From 94f9ea2cde161003d0a790f512baf49afb046f1e Mon Sep 17 00:00:00 2001 From: Taku Kodama <79110363+risu729@users.noreply.github.com> Date: Thu, 23 Jul 2026 05:49:46 +1000 Subject: [PATCH 2/7] fix(task): compare task sources safely --- e2e/tasks/test_task_config_precedence | 67 +++++++++++++++ e2e/tasks/test_task_global_config_dedup | 30 +++++++ e2e/tasks/test_task_ls_global | 17 ---- e2e/tasks/test_task_monorepo_config_dedup | 23 ++++++ e2e/tasks/test_task_source_comparison_no_exec | 40 +++++++++ src/config/mod.rs | 82 ++++++++++++++----- src/file.rs | 47 ++++++++++- src/tera.rs | 11 +++ 8 files changed, 276 insertions(+), 41 deletions(-) create mode 100644 e2e/tasks/test_task_config_precedence create mode 100644 e2e/tasks/test_task_global_config_dedup create mode 100644 e2e/tasks/test_task_monorepo_config_dedup create mode 100644 e2e/tasks/test_task_source_comparison_no_exec diff --git a/e2e/tasks/test_task_config_precedence b/e2e/tasks/test_task_config_precedence new file mode 100644 index 00000000000..7595067b27c --- /dev/null +++ b/e2e/tasks/test_task_config_precedence @@ -0,0 +1,67 @@ +#!/usr/bin/env bash + +mkdir -p .config/mise/conf.d .mise/tasks + +cat <<'EOF' >.config/mise.toml +[tasks.a] +description = ".config/a" +run = "echo .config/a" + +[tasks.b] +description = ".config/b" +alias = "low" +env = { LOWER_PRECEDENCE = "applied" } +EOF + +cat <<'EOF' >.mise/config.toml +[tasks.a] +description = ".mise/a" +run = "echo .mise/a" + +[tasks.b] +description = ".mise/b" +alias = "high" +env = { HIGHER_PRECEDENCE = "applied" } +EOF + +cat <<'EOF' >.mise/tasks/b +#!/usr/bin/env bash +echo "HIGHER_PRECEDENCE=${HIGHER_PRECEDENCE:-}" +echo "LOWER_PRECEDENCE=${LOWER_PRECEDENCE:-}" +EOF +chmod +x .mise/tasks/b + +assert_contains "mise tasks" "a .mise/a" +assert_not_contains "mise tasks" "a .config/a" +assert_contains "mise run a" ".mise/a" +assert_not_contains "mise run a" ".config/a" + +# A script receives only the highest-precedence inline definition. Additive +# fields such as aliases and env must not accumulate from lower configs. +assert_contains "mise tasks" "b .mise/b" +assert_contains "mise run high" "HIGHER_PRECEDENCE=applied" +assert_contains "mise run high" "LOWER_PRECEDENCE=" +assert_fail "mise run low" + +cat <<'EOF' >.config/mise/conf.d/01-base.toml +[tasks.c] +description = "conf.d/01/c" +env = { LOWER_PRECEDENCE = "applied" } +EOF + +cat <<'EOF' >.config/mise/conf.d/02-override.toml +[tasks.c] +description = "conf.d/02/c" +env = { HIGHER_PRECEDENCE = "applied" } +EOF + +cat <<'EOF' >.mise/tasks/c +#!/usr/bin/env bash +echo "HIGHER_PRECEDENCE=${HIGHER_PRECEDENCE:-}" +echo "LOWER_PRECEDENCE=${LOWER_PRECEDENCE:-}" +EOF +chmod +x .mise/tasks/c + +assert_contains "mise tasks" "c conf.d/02/c" +assert_contains "mise run c" "HIGHER_PRECEDENCE=applied" +assert_contains "mise run c" "LOWER_PRECEDENCE=" diff --git a/e2e/tasks/test_task_global_config_dedup b/e2e/tasks/test_task_global_config_dedup new file mode 100644 index 00000000000..28ccef43241 --- /dev/null +++ b/e2e/tasks/test_task_global_config_dedup @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +mkdir -p "$HOME/global-config" + +marker="$MISE_TMP_DIR/global-config-render-count" +cat >"$HOME/global-config/config.toml" </dev/null +assert "wc -l < '$marker' | tr -d ' '" "1" + +linked_marker="$MISE_TMP_DIR/linked-global-config-render-count" +cat >"$HOME/global-config/linked-config.toml" </dev/null +assert "wc -l < '$linked_marker' | tr -d ' '" "1" diff --git a/e2e/tasks/test_task_ls_global b/e2e/tasks/test_task_ls_global index f71b589ee50..8961dacf6a7 100644 --- a/e2e/tasks/test_task_ls_global +++ b/e2e/tasks/test_task_ls_global @@ -43,20 +43,3 @@ myglobalfile systemglobalfile" assert "mise run customglobalfile" "PROJECT_ROOT=$PWD" assert "mise run systemglobalfile" "PROJECT_ROOT=$PWD" - -# A local ancestor config can explicitly include the global task directory even -# when the global config replaces that default include. Source deduplication -# must not discard a task that the independent global pass did not load. -cat >"$HOME/mise.toml" <<'TOML' -[task_config] -includes = [".config/mise/tasks"] -TOML -cat >"$HOME/.config/mise/config.toml" <<'TOML' -tasks.myglobal = { run = 'echo myglobal' } - -[task_config] -includes = ["dotfiles/tasks"] -TOML - -assert "mise run myglobalfile" "myglobalfile" -assert_contains "mise tasks --global" "myglobalfile" diff --git a/e2e/tasks/test_task_monorepo_config_dedup b/e2e/tasks/test_task_monorepo_config_dedup new file mode 100644 index 00000000000..db454816190 --- /dev/null +++ b/e2e/tasks/test_task_monorepo_config_dedup @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +export MISE_EXPERIMENTAL=1 + +marker="$MISE_TMP_DIR/monorepo-config-render-count" +mkdir -p apps/example + +cat <<'EOF' >mise.toml +monorepo_root = true + +[monorepo] +config_roots = ["apps/*"] +EOF + +cat >apps/example/mise.toml </dev/null +assert "wc -l < '$marker' | tr -d ' '" "1" diff --git a/e2e/tasks/test_task_source_comparison_no_exec b/e2e/tasks/test_task_source_comparison_no_exec new file mode 100644 index 00000000000..f629ee2bfa9 --- /dev/null +++ b/e2e/tasks/test_task_source_comparison_no_exec @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +mkdir -p "$HOME/.config/mise/conf.d" "$HOME/.config/mise/tasks" + +marker="$MISE_TMP_DIR/source-comparison-exec-marker" +script="$HOME/.config/mise/tasks/same" + +cat <<'EOF' >"$script" +#!/usr/bin/env bash +echo same-script +EOF +chmod +x "$script" +cat <<'EOF' >"$HOME/.config/mise/tasks/same-hash" +#!/usr/bin/env bash +echo same-hash-script +EOF +chmod +x "$HOME/.config/mise/tasks/same-hash" + +cat <<'EOF' >"$HOME/.config/mise/config.toml" +[task_config] +includes = [".config/mise/tasks"] +EOF + +cat >"$HOME/.config/mise/conf.d/lower.toml" </dev/null +if [[ -e $marker ]]; then + fail "task source comparison executed a deferred file template" +fi diff --git a/src/config/mod.rs b/src/config/mod.rs index 1a11a029a86..28c5365cb34 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -31,7 +31,9 @@ use crate::shorthands::{Shorthands, get_shorthands}; use crate::task::task_file_providers::TaskFileProvidersBuilder; use crate::task::task_sources::TaskOutputs; use crate::task::{Task, TaskCacheConfig, TaskTemplate, monorepo_scope, strip_extension}; -use crate::tera::{contains_template_syntax, render_str, take_tera_accessed_files}; +use crate::tera::{ + contains_template_syntax, get_tera_for_source_comparison, render_str, take_tera_accessed_files, +}; use crate::toolset::env_cache::{CachedNonToolEnv, compute_settings_hash, get_file_mtime}; use crate::toolset::{ ResolvedToolOptions, ToolOptionSource, ToolOptions, ToolRequestSet, ToolRequestSetBuilder, @@ -701,7 +703,7 @@ impl Config { local_tasks.retain(|local| { !global_tasks .iter() - .any(|global| tasks_have_same_source(local, global)) + .any(|global| tasks_have_same_source(&config, local, global)) }); let mut tasks: BTreeMap = local_tasks .into_iter() @@ -1706,29 +1708,66 @@ fn config_set_contains(set: &IndexSet, path: &Path) -> bool { set.iter().any(|p| file::desymlink_path(p) == target) } -fn paths_equal_resolved(left: &Path, right: &Path) -> bool { - left == right || file::desymlink_path(left) == file::desymlink_path(right) -} - fn path_starts_with_resolved(path: &Path, prefix: &Path) -> bool { path.starts_with(prefix) || file::desymlink_path(path).starts_with(file::desymlink_path(prefix)) } -fn tasks_have_same_source(left: &Task, right: &Task) -> bool { - if left.name != right.name { - return false; - } - match (left.file.as_deref(), right.file.as_deref()) { - (Some(left), Some(right)) => paths_equal_resolved(left, right), - (None, None) => { - !left.config_source.as_os_str().is_empty() - && !right.config_source.as_os_str().is_empty() - && paths_equal_resolved(&left.config_source, &right.config_source) +fn resolved_task_file(config: &Config, task: &Task) -> Option { + let file = task.file.as_ref()?; + let file_str = file.to_string_lossy().to_string(); + let rendered = if contains_template_syntax(&file_str) { + let mut tera = get_tera_for_source_comparison(); + // Use only context that was already resolved while loading the config. + // Building the task's full runtime context here would evaluate task + // env/vars directives and could execute templates merely to compare + // source identities. + let mut tera_ctx = config.tera_ctx.clone(); + tera_ctx.insert("config_root", &task.config_root.clone().unwrap_or_default()); + match render_str(&mut tera, &file_str, &tera_ctx) { + Ok(rendered) => rendered, + Err(err) => { + // Deferred file templates may intentionally use exec() or + // read_file(). Source comparison must never invoke those + // functions merely to list tasks; conservatively treat an + // unsupported dynamic path as a distinct source. + debug!( + "failed to resolve task file for source comparison ({}): {err:#}", + task.name + ); + return None; + } } - _ => false, + } else { + file_str + }; + let path = file::replace_path(&rendered); + let path = if path.is_absolute() { + path + } else if let Some(root) = &task.config_root { + root.join(path) + } else { + path + }; + Some(file::desymlink_path(&path)) +} + +fn resolved_task_source(config: &Config, task: &Task) -> Option { + if task.file.is_some() { + resolved_task_file(config, task) + } else if task.config_source.as_os_str().is_empty() { + None + } else { + Some(file::desymlink_path(&task.config_source)) } } +fn tasks_have_same_source(config: &Config, left: &Task, right: &Task) -> bool { + left.name == right.name + && resolved_task_source(config, left) + .zip(resolved_task_source(config, right)) + .is_some_and(|(left, right)| left == right) +} + /// Returns true if the path should be filtered out due to MISE_CONFIG_DIR override. /// When MISE_CONFIG_DIR is set to a non-default location, this filters out configs /// found under the default location (~/.config/mise) during traversal. @@ -3085,6 +3124,7 @@ async fn load_global_tasks( .map(|(_, cf)| cf) }) }) + .unique_by(|cf| file::desymlink_path(cf.get_path())) .collect::>(); // Global config files keep independent task include sets. Aggregate their @@ -3115,11 +3155,9 @@ async fn load_global_tasks( if let Some(inline_task) = inline_tasks.get(&task.name) { if seen_config_task_names.insert(task.name.clone()) { if let Some(existing) = tasks.get_mut(&task.name) { - let rediscovered_file = existing - .file - .as_deref() - .zip(task.file.as_deref()) - .is_some_and(|(left, right)| paths_equal_resolved(left, right)); + let rediscovered_file = resolved_task_file(config, existing) + .zip(resolved_task_file(config, &task)) + .is_some_and(|(left, right)| left == right); if rediscovered_file { existing.merge_toml_overlay(inline_task.clone()); } diff --git a/src/file.rs b/src/file.rs index 76f84643f60..d505dc27ccb 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1561,6 +1561,30 @@ pub fn same_file(a: &Path, b: &Path) -> bool { desymlink_path(a) == desymlink_path(b) } +fn normalize_path_components(path: &Path) -> PathBuf { + use std::path::Component; + + let mut normalized = PathBuf::new(); + for component in path.components() { + match component { + Component::CurDir => {} + Component::ParentDir => { + let can_pop = normalized + .components() + .next_back() + .is_some_and(|component| matches!(component, Component::Normal(_))); + if can_pop { + normalized.pop(); + } else if !normalized.has_root() { + normalized.push(component.as_os_str()); + } + } + _ => normalized.push(component.as_os_str()), + } + } + normalized +} + pub fn desymlink_path(p: &Path) -> PathBuf { if p.is_symlink() && let Ok(target) = fs::read_link(p) @@ -1572,9 +1596,10 @@ pub fn desymlink_path(p: &Path) -> PathBuf { }; return target .canonicalize() - .unwrap_or_else(|_| target.to_path_buf()); + .unwrap_or_else(|_| normalize_path_components(&target)); } - p.canonicalize().unwrap_or_else(|_| p.to_path_buf()) + p.canonicalize() + .unwrap_or_else(|_| normalize_path_components(p)) } pub fn clone_dir(from: &PathBuf, to: &PathBuf) -> Result<()> { @@ -1975,6 +2000,24 @@ mod tests { assert_eq!(desymlink_path(&link), target.canonicalize().unwrap()); } + #[cfg(unix)] + #[test] + fn test_desymlink_path_normalizes_broken_relative_target() { + use std::os::unix::fs::symlink; + + let root = tempfile::tempdir().unwrap(); + let link_dir = root.path().join("links"); + fs::create_dir_all(&link_dir).unwrap(); + let link = link_dir.join("missing"); + symlink("../nested/../missing", &link).unwrap(); + + assert_eq!(desymlink_path(&link), root.path().join("missing")); + assert_eq!( + desymlink_path(&link), + desymlink_path(&root.path().join("missing")) + ); + } + #[cfg(unix)] #[test] fn test_desymlink_path_preserves_absolute_target() { diff --git a/src/tera.rs b/src/tera.rs index 0128f913e0a..46eeb5f5c4e 100644 --- a/src/tera.rs +++ b/src/tera.rs @@ -1234,6 +1234,17 @@ pub fn get_miserc_tera() -> TeraEngine { TeraEngine::V2(Box::new(TERA.clone())) } +/// Returns a renderer for comparing deferred values without executing template +/// functions that can run commands or read arbitrary files. Unlike +/// [`get_miserc_tera`], this respects the configured Tera version. +pub fn get_tera_for_source_comparison() -> TeraEngine { + if use_tera_v1() { + TeraEngine::V1(Box::default()) + } else { + TeraEngine::V2(Box::default()) + } +} + pub fn get_tera(dir: Option<&Path>) -> TeraEngine { if use_tera_v1() { TeraEngine::V1(Box::new(get_tera_v1(dir))) From 2666e9004b4850be81659a97a91bc924a081994f Mon Sep 17 00:00:00 2001 From: Taku Kodama <79110363+risu729@users.noreply.github.com> Date: Thu, 23 Jul 2026 05:51:08 +1000 Subject: [PATCH 3/7] test(task): cover resolved global task sources --- e2e/tasks/test_task_global_config_dedup | 2 ++ e2e/tasks/test_task_ls_global | 33 +++++++++++++++++++++++-- src/tera.rs | 9 ++++--- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/e2e/tasks/test_task_global_config_dedup b/e2e/tasks/test_task_global_config_dedup index 28ccef43241..2cfcd7d667e 100644 --- a/e2e/tasks/test_task_global_config_dedup +++ b/e2e/tasks/test_task_global_config_dedup @@ -3,6 +3,7 @@ mkdir -p "$HOME/global-config" marker="$MISE_TMP_DIR/global-config-render-count" +: >"$marker" cat >"$HOME/global-config/config.toml" <"$linked_marker" cat >"$HOME/global-config/linked-config.toml" <~/.config/mise/tasks/myglobalfile echo myglobalfile EOF chmod +x ~/.config/mise/tasks/myglobalfile +mkdir -p ~/.config/mise/conf.d +cat <<'TOML' >~/.config/mise/conf.d/relative-overlay.toml +[task_config] +includes = ["empty-global-tasks"] + +[tasks.relative-meta] +file = ".config/mise/tasks/relative-meta" +env = { RELATIVE_OVERLAY = "applied" } + +[tasks.templated-meta] +file = "{{config_root}}/.config/mise/tasks/templated-meta" +env = { TEMPLATED_OVERLAY = "applied" } +TOML +cat <<'EOF' >~/.config/mise/tasks/relative-meta +#!/usr/bin/env bash +echo "RELATIVE_OVERLAY=${RELATIVE_OVERLAY:-}" +EOF +chmod +x ~/.config/mise/tasks/relative-meta +cat <<'EOF' >~/.config/mise/tasks/templated-meta +#!/usr/bin/env bash +echo "TEMPLATED_OVERLAY=${TEMPLATED_OVERLAY:-}" +EOF +chmod +x ~/.config/mise/tasks/templated-meta mkdir -p ~/dotfiles/tasks cat <<'EOF' >~/dotfiles/tasks/customglobalfile #!/usr/bin/env bash @@ -35,11 +58,17 @@ assert "mise tasks" "customglobalfile myglobal myglobalfile mylocal -systemglobalfile" +relative-meta +systemglobalfile +templated-meta" assert "mise tasks --local" "mylocal" assert "mise tasks --global" "customglobalfile myglobal myglobalfile -systemglobalfile" +relative-meta +systemglobalfile +templated-meta" assert "mise run customglobalfile" "PROJECT_ROOT=$PWD" assert "mise run systemglobalfile" "PROJECT_ROOT=$PWD" +assert "mise run relative-meta" "RELATIVE_OVERLAY=applied" +assert "mise run templated-meta" "TEMPLATED_OVERLAY=applied" diff --git a/src/tera.rs b/src/tera.rs index 46eeb5f5c4e..2ca3046412e 100644 --- a/src/tera.rs +++ b/src/tera.rs @@ -1234,9 +1234,12 @@ pub fn get_miserc_tera() -> TeraEngine { TeraEngine::V2(Box::new(TERA.clone())) } -/// Returns a renderer for comparing deferred values without executing template -/// functions that can run commands or read arbitrary files. Unlike -/// [`get_miserc_tera`], this respects the configured Tera version. +/// Returns an empty renderer for comparing deferred values without invoking +/// custom template functions or filters. This intentionally excludes inert +/// helpers such as `arch()` and `os()` as well as effectful helpers such as +/// `exec()` and `read_file()`; unsupported expressions are treated as distinct +/// sources. Unlike [`get_miserc_tera`], this respects the configured Tera +/// version. pub fn get_tera_for_source_comparison() -> TeraEngine { if use_tera_v1() { TeraEngine::V1(Box::default()) From 0c15e4c2604d4d67f84920511d6a672683520ce0 Mon Sep 17 00:00:00 2001 From: Taku Kodama <79110363+risu729@users.noreply.github.com> Date: Sun, 26 Jul 2026 18:53:14 +1000 Subject: [PATCH 4/7] fix(task): avoid duplicate source rendering --- e2e/tasks/test_task_global_config_dedup | 54 ++++++++ e2e/tasks/test_task_source_comparison_no_exec | 25 +++- src/config/mod.rs | 126 ++++++++++++++---- src/file.rs | 123 ++++++++++++++--- src/tera.rs | 10 +- 5 files changed, 284 insertions(+), 54 deletions(-) diff --git a/e2e/tasks/test_task_global_config_dedup b/e2e/tasks/test_task_global_config_dedup index 2cfcd7d667e..0b3e94eec58 100644 --- a/e2e/tasks/test_task_global_config_dedup +++ b/e2e/tasks/test_task_global_config_dedup @@ -30,3 +30,57 @@ MISE_GLOBAL_CONFIG_FILE="$HOME/global-config/linked-config.toml" \ MISE_SYSTEM_CONFIG_FILE="$HOME/global-config/config-link.toml" \ mise tasks >/dev/null assert "wc -l < '$linked_marker' | tr -d ' '" "1" + +shared_marker="$MISE_TMP_DIR/shared-global-task-render-count" +: >"$shared_marker" +shared_toml_marker="$MISE_TMP_DIR/shared-global-toml-render-count" +: >"$shared_toml_marker" +mkdir -p "$HOME/shared-global-tasks" +cat >"$HOME/shared-global-tasks/render-shared-once" <"$HOME/shared-global-tasks/shared.toml" <"$HOME/global-config/user.toml" <"$HOME/global-config/system.toml" </dev/null +assert "wc -l < '$shared_marker' | tr -d ' '" "1" +assert "wc -l < '$shared_toml_marker' | tr -d ' '" "1" + +mkdir -p "$HOME/include-order-tasks" +cat >"$HOME/include-order-tasks/include-winner" <<'EOF' +#!/usr/bin/env bash +echo "$MISE_TASK_FILE" +EOF +chmod +x "$HOME/include-order-tasks/include-winner" +ln -s "include-order-tasks" "$HOME/first-global-tasks" +ln -s "include-order-tasks" "$HOME/second-global-tasks" +cat >"$HOME/global-config/include-order.toml" <"$HOME/.config/mise/tasks/same-hash" echo same-hash-script EOF chmod +x "$HOME/.config/mise/tasks/same-hash" +cat <<'EOF' >"$HOME/.config/mise/tasks/same-vars" +#!/usr/bin/env bash +echo "LOWER_OVERLAY=${LOWER_OVERLAY:-}" +EOF +chmod +x "$HOME/.config/mise/tasks/same-vars" +cat <<'EOF' >"$HOME/other-vars-script" +#!/usr/bin/env bash +echo other-vars-script +EOF +chmod +x "$HOME/other-vars-script" -cat <<'EOF' >"$HOME/.config/mise/config.toml" +cat >"$HOME/.config/mise/config.toml" <"$HOME/.config/mise/conf.d/lower.toml" </dev/null if [[ -e $marker ]]; then fail "task source comparison executed a deferred file template" fi + +# The active config's vars must not resolve a different task's deferred file +# path. The task-local value points elsewhere, so treating this as the same +# source would incorrectly apply the lower overlay to the discovered script. +assert "mise run same-vars" "LOWER_OVERLAY=" diff --git a/src/config/mod.rs b/src/config/mod.rs index 28c5365cb34..5b51c9bfd71 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -31,9 +31,7 @@ use crate::shorthands::{Shorthands, get_shorthands}; use crate::task::task_file_providers::TaskFileProvidersBuilder; use crate::task::task_sources::TaskOutputs; use crate::task::{Task, TaskCacheConfig, TaskTemplate, monorepo_scope, strip_extension}; -use crate::tera::{ - contains_template_syntax, get_tera_for_source_comparison, render_str, take_tera_accessed_files, -}; +use crate::tera::{contains_template_syntax, get_empty_tera, render_str, take_tera_accessed_files}; use crate::toolset::env_cache::{CachedNonToolEnv, compute_settings_hash, get_file_mtime}; use crate::toolset::{ ResolvedToolOptions, ToolOptionSource, ToolOptions, ToolRequestSet, ToolRequestSetBuilder, @@ -703,7 +701,7 @@ impl Config { local_tasks.retain(|local| { !global_tasks .iter() - .any(|global| tasks_have_same_source(&config, local, global)) + .any(|global| tasks_have_same_source(local, global)) }); let mut tasks: BTreeMap = local_tasks .into_iter() @@ -1712,22 +1710,27 @@ fn path_starts_with_resolved(path: &Path, prefix: &Path) -> bool { path.starts_with(prefix) || file::desymlink_path(path).starts_with(file::desymlink_path(prefix)) } -fn resolved_task_file(config: &Config, task: &Task) -> Option { +fn paths_equal_resolved(left: &Path, right: &Path) -> bool { + left == right || file::desymlink_path(left) == file::desymlink_path(right) +} + +fn resolved_task_file(task: &Task) -> Option { let file = task.file.as_ref()?; let file_str = file.to_string_lossy().to_string(); let rendered = if contains_template_syntax(&file_str) { - let mut tera = get_tera_for_source_comparison(); - // Use only context that was already resolved while loading the config. - // Building the task's full runtime context here would evaluate task - // env/vars directives and could execute templates merely to compare - // source identities. - let mut tera_ctx = config.tera_ctx.clone(); + let mut tera = get_empty_tera(); + // Only config_root has source-stable semantics here. Task env/vars may + // themselves be effectful templates, while the active Config context + // may belong to a different config hierarchy and produce a false + // source match. + let mut tera_ctx = ::tera::Context::new(); tera_ctx.insert("config_root", &task.config_root.clone().unwrap_or_default()); match render_str(&mut tera, &file_str, &tera_ctx) { Ok(rendered) => rendered, Err(err) => { // Deferred file templates may intentionally use exec() or - // read_file(). Source comparison must never invoke those + // read_file(), which are evaluated later when the task's file + // path is needed. Source comparison must never invoke those // functions merely to list tasks; conservatively treat an // unsupported dynamic path as a distinct source. debug!( @@ -1751,9 +1754,9 @@ fn resolved_task_file(config: &Config, task: &Task) -> Option { Some(file::desymlink_path(&path)) } -fn resolved_task_source(config: &Config, task: &Task) -> Option { +fn resolved_task_source(task: &Task) -> Option { if task.file.is_some() { - resolved_task_file(config, task) + resolved_task_file(task) } else if task.config_source.as_os_str().is_empty() { None } else { @@ -1761,11 +1764,11 @@ fn resolved_task_source(config: &Config, task: &Task) -> Option { } } -fn tasks_have_same_source(config: &Config, left: &Task, right: &Task) -> bool { +fn tasks_have_same_source(left: &Task, right: &Task) -> bool { left.name == right.name - && resolved_task_source(config, left) - .zip(resolved_task_source(config, right)) - .is_some_and(|(left, right)| left == right) + && resolved_task_source(left) + .zip(resolved_task_source(right)) + .is_some_and(|(left, right)| paths_equal_resolved(&left, &right)) } /// Returns true if the path should be filtered out due to MISE_CONFIG_DIR override. @@ -3134,6 +3137,7 @@ async fn load_global_tasks( // preserved. let mut tasks: IndexMap = IndexMap::new(); let mut seen_config_task_names = BTreeSet::new(); + let mut rendered_file_tasks = RenderedTaskCache::default(); for cf in configs { let sources = load_task_sources_from_configs( config, @@ -3142,8 +3146,10 @@ async fn load_global_tasks( templates, false, None, + Some(&mut rendered_file_tasks), ) .await?; + rendered_file_tasks.finish_config(); let mut inline_tasks = IndexMap::new(); for task in &sources.config_tasks { inline_tasks @@ -3155,9 +3161,9 @@ async fn load_global_tasks( if let Some(inline_task) = inline_tasks.get(&task.name) { if seen_config_task_names.insert(task.name.clone()) { if let Some(existing) = tasks.get_mut(&task.name) { - let rediscovered_file = resolved_task_file(config, existing) - .zip(resolved_task_file(config, &task)) - .is_some_and(|(left, right)| left == right); + let rediscovered_file = resolved_task_file(existing) + .zip(resolved_task_file(&task)) + .is_some_and(|(left, right)| paths_equal_resolved(&left, &right)); if rediscovered_file { existing.merge_toml_overlay(inline_task.clone()); } @@ -3345,15 +3351,25 @@ async fn load_config_tasks( Ok(tasks) } +struct LoadTaskIncludesOptions<'a> { + monorepo_cf: Option<&'a Arc>, + require_trust: bool, + rendered_file_tasks: Option<&'a mut RenderedTaskCache>, +} + async fn load_tasks_includes( config: &Arc, root: &Path, config_root: &Path, task_config: &ResolvedTaskConfig, templates: &IndexMap, - monorepo_cf: Option<&Arc>, - require_trust: bool, + options: LoadTaskIncludesOptions<'_>, ) -> Result> { + let LoadTaskIncludesOptions { + monorepo_cf, + require_trust, + mut rendered_file_tasks, + } = options; if root.is_file() && root.extension().map(|e| e == "toml").unwrap_or(false) { trust_check_task_include(root, require_trust)?; load_task_file( @@ -3364,6 +3380,7 @@ async fn load_tasks_includes( &task_config.shell, templates, monorepo_cf, + rendered_file_tasks, ) .await } else if root.is_dir() { @@ -3404,6 +3421,7 @@ async fn load_tasks_includes( &task_config.shell, templates, monorepo_cf, + rendered_file_tasks.as_deref_mut(), ) .await?, ); @@ -3421,6 +3439,14 @@ async fn load_tasks_includes( &config_root, monorepo_cf.cloned(), )?; + let cache_key = rendered_task_cache_key(&task); + if let Some(cached) = rendered_file_tasks + .as_deref() + .and_then(|cache| cache.get(&cache_key)) + { + tasks.push(cached.clone()); + continue; + } if task.shell.is_none() { task.shell = task_config.shell.clone(); } @@ -3447,6 +3473,9 @@ async fn load_tasks_includes( dir.clone() }); } + if let Some(cache) = rendered_file_tasks.as_deref_mut() { + cache.insert(cache_key, task.clone()); + } tasks.push(task); } Ok(tasks) @@ -3715,6 +3744,32 @@ fn cascaded_task_config_for_dir( Ok(cascaded) } +#[derive(Default)] +struct RenderedTaskCache { + previous_configs: HashMap<(PathBuf, String), Task>, + current_config: HashMap<(PathBuf, String), Task>, +} + +fn rendered_task_cache_key(task: &Task) -> (PathBuf, String) { + (file::desymlink_path(&task.config_source), task.name.clone()) +} + +impl RenderedTaskCache { + fn get(&self, key: &(PathBuf, String)) -> Option<&Task> { + self.previous_configs.get(key) + } + + fn insert(&mut self, key: (PathBuf, String), task: Task) { + self.current_config.insert(key, task); + } + + fn finish_config(&mut self) { + for (key, task) in self.current_config.drain() { + self.previous_configs.entry(key).or_insert(task); + } + } +} + impl TaskSources { fn into_tasks(self) -> Vec { let mut tasks = merge_file_and_config_tasks(self.file_tasks, self.config_tasks) @@ -3753,6 +3808,7 @@ async fn load_tasks_from_configs( templates, monorepo_context, cascaded_task_config, + None, ) .await? .into_tasks()) @@ -3770,6 +3826,7 @@ async fn load_task_sources_from_configs( templates: &IndexMap, monorepo_context: bool, cascaded_task_config: Option<&CascadedTaskConfig>, + mut rendered_file_tasks: Option<&mut RenderedTaskCache>, ) -> Result { let cascaded_task_config = if configs.iter().find_map(|cf| cf.task_config().cascade) == Some(false) { @@ -3857,8 +3914,11 @@ async fn load_task_sources_from_configs( dir, &task_config, templates, - monorepo_cf, - require_task_include_trust, + LoadTaskIncludesOptions { + monorepo_cf, + require_trust: require_task_include_trust, + rendered_file_tasks: rendered_file_tasks.as_deref_mut(), + }, ) .await?; for task in &mut loaded { @@ -3912,6 +3972,7 @@ async fn load_task_file( task_config_shell: &Option, templates: &IndexMap, monorepo_cf: Option<&Arc>, + mut rendered_file_tasks: Option<&mut RenderedTaskCache>, ) -> Result> { let raw = file::read_to_string_async(path).await?; let mut tasks = toml::from_str::(&raw) @@ -3929,6 +3990,14 @@ async fn load_task_file( for (_, mut task) in tasks { let config_root = config_root.to_path_buf(); resolve_task_template(&mut task, templates)?; + let cache_key = rendered_task_cache_key(&task); + if let Some(cached) = rendered_file_tasks + .as_deref() + .and_then(|cache| cache.get(&cache_key)) + { + out.push(cached.clone()); + continue; + } if task.dir.is_none() { task.dir = task_config_dir.clone(); } @@ -3937,6 +4006,9 @@ async fn load_task_file( } match task.render(config, &config_root).await { Ok(()) => { + if let Some(cache) = rendered_file_tasks.as_deref_mut() { + cache.insert(cache_key, task.clone()); + } out.push(task); } Err(err) => { @@ -3948,6 +4020,9 @@ async fn load_task_file( ); } else { warn!("rendering task: {err:?}"); + if let Some(cache) = rendered_file_tasks.as_deref_mut() { + cache.insert(cache_key, task.clone()); + } out.push(task); } } @@ -5030,6 +5105,7 @@ vars = { target = "linux" } &None, &IndexMap::new(), None, + None, ) .await?; assert_eq!(tasks.len(), 1); diff --git a/src/file.rs b/src/file.rs index d505dc27ccb..04e9afa90a7 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1561,28 +1561,57 @@ pub fn same_file(a: &Path, b: &Path) -> bool { desymlink_path(a) == desymlink_path(b) } -fn normalize_path_components(path: &Path) -> PathBuf { - use std::path::Component; - - let mut normalized = PathBuf::new(); - for component in path.components() { - match component { - Component::CurDir => {} - Component::ParentDir => { - let can_pop = normalized - .components() - .next_back() - .is_some_and(|component| matches!(component, Component::Normal(_))); - if can_pop { - normalized.pop(); - } else if !normalized.has_root() { - normalized.push(component.as_os_str()); +fn resolve_path_with_existing_prefix(path: &Path) -> PathBuf { + let mut resolved = if path.is_relative() { + env::current_dir().unwrap_or_default() + } else { + PathBuf::new() + }; + let mut components = path.components(); + while let Some(component) = components.next() { + if matches!(component, std::path::Component::CurDir) { + continue; + } + #[cfg(windows)] + if matches!( + component, + std::path::Component::Prefix(_) | std::path::Component::RootDir + ) { + // A drive prefix such as `C:` is drive-relative until its root is + // present, so do not canonicalize the two components separately. + resolved.push(component.as_os_str()); + continue; + } + let candidate = resolved.join(component.as_os_str()); + match candidate.canonicalize() { + Ok(candidate) => resolved = candidate, + Err(_) => { + #[cfg(windows)] + { + // PathBuf::push lexically resolves `..` after a verbatim + // (`\\?\`) prefix, but the unresolved suffix must remain + // opaque so two potentially different files stay distinct. + let mut raw = resolved.into_os_string(); + if Path::new(&raw).file_name().is_some() { + raw.push("\\"); + } + raw.push(component.as_os_str()); + for component in components { + raw.push("\\"); + raw.push(component.as_os_str()); + } + resolved = raw.into(); + } + #[cfg(not(windows))] + { + resolved.push(component.as_os_str()); + resolved.extend(components.map(|component| component.as_os_str())); } + break; } - _ => normalized.push(component.as_os_str()), } } - normalized + resolved } pub fn desymlink_path(p: &Path) -> PathBuf { @@ -1596,10 +1625,10 @@ pub fn desymlink_path(p: &Path) -> PathBuf { }; return target .canonicalize() - .unwrap_or_else(|_| normalize_path_components(&target)); + .unwrap_or_else(|_| resolve_path_with_existing_prefix(&target)); } p.canonicalize() - .unwrap_or_else(|_| normalize_path_components(p)) + .unwrap_or_else(|_| resolve_path_with_existing_prefix(p)) } pub fn clone_dir(from: &PathBuf, to: &PathBuf) -> Result<()> { @@ -2007,17 +2036,69 @@ mod tests { let root = tempfile::tempdir().unwrap(); let link_dir = root.path().join("links"); + let nested_dir = root.path().join("nested"); fs::create_dir_all(&link_dir).unwrap(); + fs::create_dir_all(&nested_dir).unwrap(); let link = link_dir.join("missing"); symlink("../nested/../missing", &link).unwrap(); + let expected = root.path().canonicalize().unwrap().join("missing"); - assert_eq!(desymlink_path(&link), root.path().join("missing")); + assert_eq!(desymlink_path(&link), expected); assert_eq!( desymlink_path(&link), desymlink_path(&root.path().join("missing")) ); } + #[cfg(unix)] + #[test] + fn test_desymlink_path_resolves_existing_symlink_prefix_before_parent() { + use std::os::unix::fs::symlink; + + let root = tempfile::tempdir().unwrap(); + let outside = tempfile::tempdir().unwrap(); + let outside_nested = outside.path().join("nested"); + fs::create_dir_all(&outside_nested).unwrap(); + let link = root.path().join("link"); + symlink(&outside_nested, &link).unwrap(); + + let through_link = link.join("../.config/mise/tasks/same"); + let expected = outside + .path() + .canonicalize() + .unwrap() + .join(".config/mise/tasks/same"); + let false_match = root + .path() + .canonicalize() + .unwrap() + .join(".config/mise/tasks/same"); + + assert_eq!(desymlink_path(&through_link), expected); + assert_ne!(desymlink_path(&through_link), desymlink_path(&false_match)); + } + + #[test] + fn test_desymlink_path_preserves_parent_after_missing_component() { + let root = tempfile::tempdir().unwrap(); + let unresolved = root.path().join("missing/../target"); + let canonical_root = root.path().canonicalize().unwrap(); + #[cfg(windows)] + let expected = { + let mut expected = canonical_root.as_os_str().to_os_string(); + expected.push("\\missing\\..\\target"); + PathBuf::from(expected) + }; + #[cfg(not(windows))] + let expected = canonical_root.join("missing/../target"); + + assert_eq!(desymlink_path(&unresolved), expected); + assert_ne!( + desymlink_path(&unresolved), + desymlink_path(&canonical_root.join("target")) + ); + } + #[cfg(unix)] #[test] fn test_desymlink_path_preserves_absolute_target() { diff --git a/src/tera.rs b/src/tera.rs index 2ca3046412e..7993777ccd6 100644 --- a/src/tera.rs +++ b/src/tera.rs @@ -1234,13 +1234,9 @@ pub fn get_miserc_tera() -> TeraEngine { TeraEngine::V2(Box::new(TERA.clone())) } -/// Returns an empty renderer for comparing deferred values without invoking -/// custom template functions or filters. This intentionally excludes inert -/// helpers such as `arch()` and `os()` as well as effectful helpers such as -/// `exec()` and `read_file()`; unsupported expressions are treated as distinct -/// sources. Unlike [`get_miserc_tera`], this respects the configured Tera -/// version. -pub fn get_tera_for_source_comparison() -> TeraEngine { +/// Returns a renderer without mise's custom template functions or filters. +/// Unlike [`get_miserc_tera`], this respects the configured Tera version. +pub fn get_empty_tera() -> TeraEngine { if use_tera_v1() { TeraEngine::V1(Box::default()) } else { From 421d7cd5441f3ed2306cc3de4d1b32d650536942 Mon Sep 17 00:00:00 2001 From: Taku Kodama <79110363+risu729@users.noreply.github.com> Date: Tue, 28 Jul 2026 06:03:26 +1000 Subject: [PATCH 5/7] refactor(file): centralize resolved path comparisons --- src/config/mod.rs | 40 +++++++++++++++++++--------------------- src/file.rs | 8 +++++++- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 5b51c9bfd71..e6f14c3a1a9 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1706,14 +1706,6 @@ fn config_set_contains(set: &IndexSet, path: &Path) -> bool { set.iter().any(|p| file::desymlink_path(p) == target) } -fn path_starts_with_resolved(path: &Path, prefix: &Path) -> bool { - path.starts_with(prefix) || file::desymlink_path(path).starts_with(file::desymlink_path(prefix)) -} - -fn paths_equal_resolved(left: &Path, right: &Path) -> bool { - left == right || file::desymlink_path(left) == file::desymlink_path(right) -} - fn resolved_task_file(task: &Task) -> Option { let file = task.file.as_ref()?; let file_str = file.to_string_lossy().to_string(); @@ -1768,7 +1760,7 @@ fn tasks_have_same_source(left: &Task, right: &Task) -> bool { left.name == right.name && resolved_task_source(left) .zip(resolved_task_source(right)) - .is_some_and(|(left, right)| paths_equal_resolved(&left, &right)) + .is_some_and(|(left, right)| file::same_file(&left, &right)) } /// Returns true if the path should be filtered out due to MISE_CONFIG_DIR override. @@ -2549,7 +2541,7 @@ fn is_global_task_include_path(path: &Path) -> bool { dirs::SYSTEM_CONFIG.join("tasks"), ] .iter() - .any(|prefix| path_starts_with_resolved(path, prefix)) + .any(|prefix| file::path_starts_with_resolved(path, prefix)) } #[async_backtrace::framed] @@ -2619,8 +2611,8 @@ fn enclosing_monorepo_roots(config_files: &ConfigMap, selected_root: &Path) -> V .filter(|cf| cf.monorepo_root() == Some(true)) .filter_map(|cf| cf.project_root()) .filter(|root| { - !paths_equal_resolved(root, selected_root) - && path_starts_with_resolved(selected_root, root) + !file::same_file(root, selected_root) + && file::path_starts_with_resolved(selected_root, root) }) .collect() } @@ -2634,7 +2626,7 @@ fn enclosing_monorepo_roots(config_files: &ConfigMap, selected_root: &Path) -> V /// Directories above the enclosing root (`$HOME`, global config) are unaffected and /// keep inheriting normally. /// -/// Prefix checks go through [`path_starts_with_resolved`] because `dir` comes from the +/// Prefix checks go through [`file::path_starts_with_resolved`] because `dir` comes from the /// cwd walk while the roots come from a config's `project_root`, and those can be /// different symlink spellings of the same path (e.g. Fedora Atomic's `/home` -> /// `/var/home`) when a task's config hierarchy was loaded from another directory. @@ -2650,8 +2642,8 @@ fn dir_is_in_enclosing_monorepo( } enclosing_roots .iter() - .any(|enclosing| path_starts_with_resolved(dir, enclosing)) - && !path_starts_with_resolved(dir, selected_root) + .any(|enclosing| file::path_starts_with_resolved(dir, enclosing)) + && !file::path_starts_with_resolved(dir, selected_root) } async fn load_local_tasks_with_context( @@ -2730,25 +2722,31 @@ async fn load_local_tasks_with_context( async move { let exact_config_paths = config_paths_in_dir(&subdir); let found_config = !exact_config_paths.is_empty(); + let mut seen_config_paths = std::collections::HashSet::new(); let mut hierarchy_configs = config .config_files .iter() .filter(|(_, cf)| { let root = cf.config_root(); - path_starts_with_resolved(&root, &monorepo_root) - && path_starts_with_resolved(&subdir, &root) + file::path_starts_with_resolved(&root, &monorepo_root) + && file::path_starts_with_resolved(&subdir, &root) + }) + .filter(|(path, _)| { + seen_config_paths.insert(file::desymlink_path(path)) }) .map(|(path, cf)| (path.clone(), cf.clone())) .collect::(); let mut hierarchy_dirs = subdir .ancestors() - .take_while(|dir| path_starts_with_resolved(dir, &monorepo_root)) + .take_while(|dir| { + file::path_starts_with_resolved(dir, &monorepo_root) + }) .map(Path::to_path_buf) .collect::>(); hierarchy_dirs.reverse(); for hierarchy_dir in hierarchy_dirs { for config_path in config_paths_in_dir(&hierarchy_dir) { - if hierarchy_configs.contains_key(&config_path) { + if !seen_config_paths.insert(file::desymlink_path(&config_path)) { continue; } match config_file::parse(&config_path).await { @@ -3163,7 +3161,7 @@ async fn load_global_tasks( if let Some(existing) = tasks.get_mut(&task.name) { let rediscovered_file = resolved_task_file(existing) .zip(resolved_task_file(&task)) - .is_some_and(|(left, right)| paths_equal_resolved(&left, &right)); + .is_some_and(|(left, right)| file::same_file(&left, &right)); if rediscovered_file { existing.merge_toml_overlay(inline_task.clone()); } @@ -3716,7 +3714,7 @@ fn cascaded_task_config_for_dir( .values() .filter(|cf| !is_global_config(cf.get_path())) .map(|cf| cf.config_root()) - .filter(|root| !paths_equal_resolved(root, dir) && path_starts_with_resolved(dir, root)) + .filter(|root| !file::same_file(root, dir) && file::path_starts_with_resolved(dir, root)) .unique() .collect::>(); roots.sort_by_key(|root| root.components().count()); diff --git a/src/file.rs b/src/file.rs index 04e9afa90a7..202c74495dc 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1558,7 +1558,13 @@ pub fn split_file_name(path: &Path) -> (String, String) { } pub fn same_file(a: &Path, b: &Path) -> bool { - desymlink_path(a) == desymlink_path(b) + a == b || desymlink_path(a) == desymlink_path(b) +} + +/// Returns whether `path` starts with `prefix` either lexically or after +/// resolving symlinks and the existing path prefix. +pub fn path_starts_with_resolved(path: &Path, prefix: &Path) -> bool { + path.starts_with(prefix) || desymlink_path(path).starts_with(desymlink_path(prefix)) } fn resolve_path_with_existing_prefix(path: &Path) -> PathBuf { From b98a084155d9a1d57332d9d91ea3abd16f609a40 Mon Sep 17 00:00:00 2001 From: Taku Kodama <79110363+risu729@users.noreply.github.com> Date: Tue, 28 Jul 2026 06:32:20 +1000 Subject: [PATCH 6/7] refactor(task): reduce task loader arguments --- src/config/mod.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index e6f14c3a1a9..111c0a2b831 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -3374,8 +3374,7 @@ async fn load_tasks_includes( config, root, config_root, - &task_config.dir, - &task_config.shell, + task_config, templates, monorepo_cf, rendered_file_tasks, @@ -3415,8 +3414,7 @@ async fn load_tasks_includes( config, &path, config_root, - &task_config.dir, - &task_config.shell, + task_config, templates, monorepo_cf, rendered_file_tasks.as_deref_mut(), @@ -3966,8 +3964,7 @@ async fn load_task_file( config: &Arc, path: &Path, config_root: &Path, - task_config_dir: &Option, - task_config_shell: &Option, + task_config: &ResolvedTaskConfig, templates: &IndexMap, monorepo_cf: Option<&Arc>, mut rendered_file_tasks: Option<&mut RenderedTaskCache>, @@ -3997,10 +3994,10 @@ async fn load_task_file( continue; } if task.dir.is_none() { - task.dir = task_config_dir.clone(); + task.dir = task_config.dir.clone(); } if task.shell.is_none() { - task.shell = task_config_shell.clone(); + task.shell = task_config.shell.clone(); } match task.render(config, &config_root).await { Ok(()) => { @@ -5099,8 +5096,7 @@ vars = { target = "linux" } &config, &tasks_toml, temp_dir.path(), - &None, - &None, + &ResolvedTaskConfig::default(), &IndexMap::new(), None, None, From ea750abfd0a6826972cfe59cdef35db35fa13db6 Mon Sep 17 00:00:00 2001 From: Taku Kodama <79110363+risu729@users.noreply.github.com> Date: Tue, 28 Jul 2026 11:14:48 +1000 Subject: [PATCH 7/7] test(task): fail when source listing errors --- e2e/tasks/test_task_source_comparison_no_exec | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/e2e/tasks/test_task_source_comparison_no_exec b/e2e/tasks/test_task_source_comparison_no_exec index a07fbae3c6f..aa589f152f5 100644 --- a/e2e/tasks/test_task_source_comparison_no_exec +++ b/e2e/tasks/test_task_source_comparison_no_exec @@ -52,7 +52,9 @@ vars = { comparison_path = "$HOME/other-vars-script" } env = { LOWER_OVERLAY = "must-not-apply" } EOF -mise tasks >/dev/null +if ! mise tasks >/dev/null; then + fail "task listing failed during source-comparison test" +fi if [[ -e $marker ]]; then fail "task source comparison executed a deferred file template" fi