Skip to content

feat(requires): support variable references in enum constraints#2678

Merged
vmaerten merged 9 commits intomainfrom
feat/enum-ref-support
Mar 21, 2026
Merged

feat(requires): support variable references in enum constraints#2678
vmaerten merged 9 commits intomainfrom
feat/enum-ref-support

Conversation

@vmaerten
Copy link
Copy Markdown
Member

@vmaerten vmaerten commented Feb 4, 2026

Summary

Add support for variable references in enum constraints via a new ref key, allowing enum values to be defined once and reused or computed dynamically instead of being hardcoded.

fixes #2647

Add support for referencing variables in enum validation:

```yaml
vars:
  ALLOWED_ENVS: ["dev", "staging", "prod"]

tasks:
  deploy:
    requires:
      vars:
        - name: ENV
          enum:
            ref: .ALLOWED_ENVS
```

This allows enum values to be defined once and reused, or computed
dynamically using template expressions like `| fromYaml`.

Changes:
- Add Enum type with Ref/Value fields in taskfile/ast/requires.go
- Add resolveEnumRefs() to resolve refs at task compilation time
- Add getEnumValues() helper in requires.go
- Only resolve enum refs when shell variables are evaluated
@vmaerten vmaerten marked this pull request as ready for review February 17, 2026 20:53
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds support for referencing variables in requires.vars[].enum via a new ref key, allowing enum constraints to be reused or computed dynamically (including via templating/shell-derived vars), and updates docs/tests accordingly.

Changes:

  • Introduce ast.Enum (with YAML unmarshalling) to support either a static enum list or a ref expression.
  • Resolve enum.ref during task compilation (when dynamic vars are evaluated) and validate required vars against the resolved values.
  • Add docs/examples and golden tests covering enum refs and error cases.

Reviewed changes

Copilot reviewed 11 out of 13 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
taskfile/ast/requires.go Adds Enum type and YAML unmarshalling for enum: [...] vs enum: {ref: ...}.
variables.go Resolves enum.ref during compilation to populate enum values for validation.
requires.go Adapts prompting/validation plumbing to use the new Enum structure.
internal/summary/summary.go Updates summary printing to account for enum values being nested under Enum.Value.
executor_test.go Adds executor tests for enum refs (pass/fail/non-list).
testdata/requires/Taskfile.yml Adds variables/tasks used by new enum-ref requires tests.
testdata/requires/testdata/*.golden Golden outputs for the new enum-ref test cases.
website/src/docs/guide.md Documents enum.ref usage, including templating/shell-derived examples.
website/src/docs/reference/schema.md Adds schema reference docs example for enum.ref.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Check cache.Err() after ResolveRef to surface template parse errors
- Resolve enum refs in all compilation paths (not just evaluateShVars)
- Validate non-empty ref in Enum.UnmarshalYAML to reject empty mappings
- Display enum ref expression in task summary when values are unresolved
- Fix Go template pipeline syntax in docs (use parentheses)
- Update JSON schema to accept enum as object with ref key
- Add test for enum ref to nonexistent variable
Enum refs that use template pipelines like fromJson depend on
shell-derived variables which are only available after sh: evaluation.
Restoring the guard prevents resolution failures in FastCompiledTask.
@vmaerten vmaerten merged commit 8b6aca5 into main Mar 21, 2026
14 checks passed
@vmaerten vmaerten deleted the feat/enum-ref-support branch March 21, 2026 10:32
vmaerten added a commit that referenced this pull request Mar 21, 2026
Copilot AI pushed a commit to libor-m/task that referenced this pull request Mar 27, 2026
…ask#2678)

Co-authored-by: libor-m <1497769+libor-m@users.noreply.github.com>
Copilot AI pushed a commit to libor-m/task that referenced this pull request Mar 27, 2026
Co-authored-by: libor-m <1497769+libor-m@users.noreply.github.com>
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.

Allow Interactive Prompt Variables to be Fed From Shell

2 participants