Skip to content

fix: validate include 'taskfile' field, suggest 'vars:' if missing#2860

Open
RichardHopperProGrammar wants to merge 1 commit into
go-task:mainfrom
RichardHopperProGrammar:fix/missing-taskfile-include-validation
Open

fix: validate include 'taskfile' field, suggest 'vars:' if missing#2860
RichardHopperProGrammar wants to merge 1 commit into
go-task:mainfrom
RichardHopperProGrammar:fix/missing-taskfile-include-validation

Conversation

@RichardHopperProGrammar
Copy link
Copy Markdown

Problem

When a user accidentally writes includes: with variable-like content instead of vars: — a common typo like this:

# Taskfile.yml
version: '3'

includes:
  GOBIN:
    sh: echo $(go env GOPATH)/bin

tasks:
  test:
    cmds:
      - "{{.GOBIN}}/richgo test ./..."

...the current behavior is to silently accept the malformed include entry (since any YAML mapping passes the yaml.MappingNode check in Includes.UnmarshalYAML), and later report a misleading "include cycle detected between A <--> A" error, because the empty Taskfile path resolves to the same file.

This is confusing and unhelpful — the real problem isn't a cycle, it's that the user put variables under includes: instead of vars:.

Solution

Added validation in taskfile/ast/include.go to check that each include entry has a non-empty Taskfile field after decoding. If it doesn't:

  1. Returns a clear error: include "GOBIN" is missing the required "taskfile" field; did you mean to use "vars:" instead?
  2. Lists unexpected keys found (e.g., sh) so the user understands what went wrong
  3. Provides actionable guidance (use vars: instead)

Changes

  • taskfile/ast/include.go: Added validation in Includes.UnmarshalYAML after decoding each include value, checking for empty Taskfile and producing a helpful error message
  • testdata/includes_missing_taskfile/: Test fixtures reproducing the exact scenario from issue Incorrect/confusing include cycle detected error message after a silly mistake #1881
  • task_test.go: New test TestIncludesMissingTaskfile verifying the new error message is shown instead of the misleading cycle error

Testing

  • All existing include-related tests pass (TestIncludes, TestIncludeCycle, TestIncludesIncorrect, etc.)
  • New test specifically validates:
    • Error contains "missing the required "taskfile" field"
    • Error does NOT contain "include cycle detected"

When a user accidentally uses 'includes:' with variable-like content
(e.g., 'GOBIN: {sh: ...}') instead of 'vars:', the previous behavior
was to silently accept it and later report a misleading 'include cycle
detected' error, since the empty Taskfile path resolved to the same file.

This fix validates that each include entry has a non-empty 'taskfile'
field during YAML parsing and returns a clear, actionable error message
suggesting the user may have meant to use 'vars:' instead.

Closes go-task#1881
@trulede
Copy link
Copy Markdown
Contributor

trulede commented May 27, 2026

See #1902

@RichardHopperProGrammar
Copy link
Copy Markdown
Author

Thanks @trulede — great call. #1902 already covers this and has been through review, so I'll close this PR to avoid duplication. Good to know about the existing work!

@trulede
Copy link
Copy Markdown
Contributor

trulede commented May 28, 2026

@RichardHopperProGrammar I think the other PR is dormant .... so you might prefer to keep yours open, and incorporate the feedback. Small concise changes that don't change too much have a good chance of being accepted.

@trulede trulede reopened this May 28, 2026
@RichardHopperProGrammar
Copy link
Copy Markdown
Author

Thanks @trulede — I'll keep it open then. Good call about #1902 being dormant and the project moving away from go generate. Let me incorporate the feedback (switch to fmt.Errorf with %w, add test for the include cycle error) and push an update soon. Appreciate the guidance!

@RichardHopperProGrammar
Copy link
Copy Markdown
Author

Thanks @trulede — I see #1902 is dormant so I will keep mine open and incorporate feedback. The test for missing taskfile vs. cycle error is already included (line 1069 in task_test.go — verifies the error says "taskfile not found" and not "include cycle detected"). Happy to make any other refinements you suggest.

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.

2 participants