Skip to content

fix(task): resolve env vars in usage tera templates when flags are provided#8957

Merged
jdx merged 2 commits intomainfrom
fix/task-usage-env-rendering
Apr 7, 2026
Merged

fix(task): resolve env vars in usage tera templates when flags are provided#8957
jdx merged 2 commits intomainfrom
fix/task-usage-env-rendering

Conversation

@jdx
Copy link
Copy Markdown
Owner

@jdx jdx commented Apr 7, 2026

Summary

  • parse_usage_values_from_task was creating an empty EnvMap and passing it to parse_run_scripts, which overwrote the real env context (already populated by task.tera_ctx()toolset.tera_ctx()full_env()) with empty data
  • This caused {{ env.VAR }} references in task usage field defaults to fail with "Variable not found in context" whenever any flags were passed to the task
  • Fix: compute the real env from the toolset instead of using an empty map

Fixes #8954

Test plan

  • Added e2e test test_task_usage_env_default_tera covering:
    • Task with {{ env.DEFAULT_FILE }} in usage default runs without flags
    • Task with {{ env.DEFAULT_FILE }} in usage default runs with explicit flag override
    • Task --help renders without error
  • All existing usage env tests pass (test_task_usage_env, test_task_usage_env_tera, test_task_monorepo_usage_env, test_task_dep_args)

🤖 Generated with Claude Code


Note

Medium Risk
Moderate risk because it changes how task CLI args/flags are parsed by wiring in the full computed environment, which could affect usage default evaluation across tasks; scope is small and covered by a targeted e2e regression test.

Overview
Fixes a regression where task usage defaults using Tera {{ env.* }} templates could fail when flags were provided by ensuring parse_usage_values_from_task uses the toolset’s full environment rather than an empty EnvMap.

Adds an e2e regression test (test_task_usage_env_default_tera) that verifies env-based usage defaults work with no flags, with explicit flag overrides, with --help, and with system env vars like HOME.

Reviewed by Cursor Bugbot for commit 2f5821b. Bugbot is set up for automated code reviews on this repo. Configure here.

…ovided

parse_usage_values_from_task was creating an empty EnvMap which
overwrote the real env context in the tera renderer, causing
`{{ env.VAR }}` in usage field defaults to fail when any flags
were passed to the task.

Fixes #8954

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 7, 2026

Greptile Summary

This PR fixes a bug where parse_usage_values_from_task used an empty EnvMap when parsing usage specs containing Tera env-variable references (e.g., default="{{ env.VAR }}"). The empty map caused a "Variable not found in context" error whenever any flags were passed to the task. The fix replaces the empty map with a proper environment derived from the toolset via ts.full_env(config).

Confidence Score: 5/5

Safe to merge — the fix is minimal and well-targeted, with a direct regression test covering the reported failure.

The change is two lines, replaces an obviously wrong empty map with the correct toolset-derived environment, is consistent with how env is used everywhere else in the codebase, and is guarded by a new e2e test that exercises all three scenarios (no flags, explicit flag, --help). No P0/P1 findings.

No files require special attention.

Vulnerabilities

No security concerns identified. The change passes environment variables that are already available to the task into the Tera rendering context, consistent with how they are used elsewhere in the codebase.

Important Files Changed

Filename Overview
src/task/mod.rs Two-line fix replacing Default::default() (empty EnvMap) with ts.full_env(config).await? so Tera templates referencing env.* resolve correctly during usage spec parsing.
e2e/tasks/test_task_usage_env_default_tera New e2e regression test covering: env-based usage defaults without flags, with an explicit flag override, --help rendering, and system env vars (HOME) as defaults.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[mise run task --flag value] --> B[parse_usage_values_from_task]
    B --> C{Before fix}
    B --> D{After fix}
    C --> E[EnvMap = empty default]
    D --> F[config.get_toolset]
    F --> G[ts.full_env - real env vars]
    E --> H[parse_usage_spec_with_vars]
    G --> H
    H --> I{Tera template with env.VAR?}
    I -- EnvMap empty --> J[ERROR: Variable not found]
    I -- EnvMap populated --> K[Template renders correctly]
    K --> L[usage::Parser resolves flag defaults]
    L --> M[Return IndexMap of flag values]
Loading

Reviews (2): Last reviewed commit: "fix(task): use full_env to include syste..." | Re-trigger Greptile

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a regression test to ensure that Tera environment templates work correctly within task usage fields and updates the parse_usage_values_from_task function to retrieve the environment from the toolset. Feedback suggests using full_env instead of env_with_path to maintain consistency with existing patterns in the module and to align with the pull request's stated intent.

Comment thread src/task/mod.rs Outdated
) -> Result<IndexMap<String, String>> {
let env: EnvMap = Default::default();
let ts = config.get_toolset().await?;
let env = ts.env_with_path(config).await?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with other parts of src/task/mod.rs (e.g., lines 1010 and 1181) and to align with the PR description which mentions full_env(), consider using ts.full_env(config) instead of ts.env_with_path(config). Both likely provide the necessary environment, but full_env is the established pattern in this module for obtaining the environment to be used in Tera contexts, ensuring that {{ env.VAR }} references behave identically across all task templates.

Suggested change
let env = ts.env_with_path(config).await?;
let env = ts.full_env(config).await?;

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 1f563f5. Configure here.

Comment thread src/task/mod.rs Outdated
env_with_path only includes mise-managed env vars, but full_env also
includes system variables (PRISTINE_ENV). Without this, references
like {{ env.HOME }} in usage defaults would fail during dependency
resolution while working fine during actual task execution.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 7, 2026

Hyperfine Performance

mise x -- echo

Command Mean [ms] Min [ms] Max [ms] Relative
mise-2026.4.5 x -- echo 26.7 ± 0.5 24.6 28.0 1.01 ± 0.03
mise x -- echo 26.6 ± 0.7 24.7 30.9 1.00

mise env

Command Mean [ms] Min [ms] Max [ms] Relative
mise-2026.4.5 env 25.4 ± 0.7 23.7 30.3 1.00
mise env 25.9 ± 1.1 24.2 38.3 1.02 ± 0.05

mise hook-env

Command Mean [ms] Min [ms] Max [ms] Relative
mise-2026.4.5 hook-env 26.4 ± 0.5 25.0 28.7 1.00
mise hook-env 27.4 ± 0.7 25.7 37.1 1.04 ± 0.03

mise ls

Command Mean [ms] Min [ms] Max [ms] Relative
mise-2026.4.5 ls 24.0 ± 1.3 22.8 39.8 1.00
mise ls 24.4 ± 1.1 22.4 40.0 1.01 ± 0.07

xtasks/test/perf

Command mise-2026.4.5 mise Variance
install (cached) 160ms 158ms +1%
ls (cached) 84ms 85ms -1%
bin-paths (cached) 89ms 90ms -1%
task-ls (cached) 809ms 804ms +0%

@jdx jdx merged commit 2e378bf into main Apr 7, 2026
36 checks passed
@jdx jdx deleted the fix/task-usage-env-rendering branch April 7, 2026 22:32
mise-en-dev added a commit that referenced this pull request Apr 7, 2026
### 🚀 Features

- **(config)** Add Tera template support to miserc.toml by
@richardthe3rd in [#8867](#8867)

### 🐛 Bug Fixes

- **(env)** include tools-only redactions in `mise env --redacted` by
@jakedgy in [#8956](#8956)
- **(env)** pass dependency env to vfox backend plugin hooks by
@cprecioso in [#8952](#8952)
- **(shim)** fix race condition when removing in make_shim, when
multiple plugins provide the same shim by @brander-john in
[#8947](#8947)
- **(spm)** derive API URL from host for self-hosted instances by
@ThomasDutartre in [#8955](#8955)
- **(task)** resolve env vars in usage tera templates when flags are
provided by @jdx in [#8957](#8957)

### 📚 Documentation

- **(python)** clarify attestation settings must be under [settings] in
mise.toml by @fru1tworld in
[#8939](#8939)

### 📦 Registry

- added sing-box by @tony-sol in
[#8944](#8944)

### Chore

- **(ci)** remove auto-draft PR workflow by @jdx in
[#8945](#8945)

### New Contributors

- @ThomasDutartre made their first contribution in
[#8955](#8955)
- @jakedgy made their first contribution in
[#8956](#8956)
- @brander-john made their first contribution in
[#8947](#8947)
- @fru1tworld made their first contribution in
[#8939](#8939)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant