Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-task/task/v3/internal/env"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/fingerprint"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/internal/version"
Expand Down Expand Up @@ -184,19 +185,27 @@ func (c *Compiler) ResetCache() {
c.dynamicCache = nil
}

func (c *Compiler) getSpecialVars(t *ast.Task, call *ast.Call) (map[string]string, error) {
allVars := map[string]string{
func (c *Compiler) getSpecialVars(t *ast.Task, call *ast.Call) (map[string]any, error) {
allVars := map[string]any{
"TASK_EXE": filepath.ToSlash(os.Args[0]),
"ROOT_TASKFILE": filepathext.SmartJoin(c.Dir, c.Entrypoint),
"ROOT_DIR": c.Dir,
"USER_WORKING_DIR": c.UserWorkingDir,
"TASK_VERSION": version.GetVersion(),
}
if t != nil {
dir := filepathext.SmartJoin(c.Dir, t.Dir)

taskSources, err := fingerprint.Globs(dir, t.Sources)
if err != nil {
return allVars, fmt.Errorf("expand globs: %v", err)
}
Comment on lines +199 to +202
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be moved to the end of this block so that TASK, TASK_DIR, etc. all still get assigned even if there's an error on glob expansion?


allVars["TASK"] = t.Task
allVars["TASK_DIR"] = filepathext.SmartJoin(c.Dir, t.Dir)
allVars["TASK_DIR"] = dir
allVars["TASKFILE"] = t.Location.Taskfile
allVars["TASKFILE_DIR"] = filepath.Dir(t.Location.Taskfile)
allVars["TASK_SOURCES"] = taskSources
}
if call != nil {
allVars["ALIAS"] = call.Task
Expand Down
12 changes: 12 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ func TestSpecialVars(t *testing.T) {
{target: "print-taskfile-dir", expected: toAbs(dir)},
{target: "print-task-version", expected: "unknown"},
{target: "print-task-dir", expected: toAbs(dir) + "/foo"},
{
target: "print-task-sources",
expected: "[" +
toAbs(dir) + "/sources-as-var/file.txt, " +
toAbs(dir) + "/sources-as-var/subdir1/file.txt, " +
toAbs(dir) + "/sources-as-var/subdir2/file.txt" +
"]",
},
{target: "print-task-sources-empty", expected: "[]"},
// Included
{target: "included:print-task", expected: "included:print-task"},
{target: "included:print-root-dir", expected: toAbs(dir)},
Expand All @@ -251,6 +260,9 @@ func TestSpecialVars(t *testing.T) {
Stderr: &buff,
Silent: true,
EnableVersionCheck: true,
// NOTE needed to enable Force to ignore fingerprints generated
// when setting source.
Force: true,
}
require.NoError(t, e.Setup())
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: test.target}))
Expand Down
11 changes: 11 additions & 0 deletions testdata/special_vars/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ tasks:
print-task-dir:
dir: 'foo'
cmd: echo {{.TASK_DIR}}

print-task-sources:
sources:
- sources-as-var/**/*.txt
cmds:
- echo "[{{.TASK_SOURCES | sortAlpha | join ", "}}]"

print-task-sources-empty:
# No sources defined, yet we try to use TASK_SOURCES.
cmds:
- echo "{{.TASK_SOURCES}}"
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions website/docs/reference/templating.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ special variable will be overridden.
| `TASKFILE` | The absolute path of the included Taskfile. |
| `TASKFILE_DIR` | The absolute path of the included Taskfile directory. |
| `TASK_DIR` | The absolute path of the directory where the task is executed. |
| `TASK_SOURCES` | List of files referenced by task `sources` key. Empty if task does not define `sources`. |
| `USER_WORKING_DIR` | The absolute path of the directory `task` was called from. |
| `CHECKSUM` | The checksum of the files listed in `sources`. Only available within the `status` prop and if method is set to `checksum`. |
| `TIMESTAMP` | The date object of the greatest timestamp of the files listed in `sources`. Only available within the `status` prop and if method is set to `timestamp`. |
Expand Down