From 6604b9a8cccc072d1d2ed8b801ff5a167bef1554 Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Mon, 10 Mar 2025 11:46:07 +0000 Subject: [PATCH] fix: special variable type errors in vars with no task context (#2107) * fix: stop dotenv trying to fetch variables when no dotenv specified * fix: set special variables to "" when they can't be calculated --- compiler.go | 7 +++++++ setup.go | 4 ++++ taskfile/dotenv.go | 4 ---- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/compiler.go b/compiler.go index 1426db08..753053ab 100644 --- a/compiler.go +++ b/compiler.go @@ -209,9 +209,16 @@ func (c *Compiler) getSpecialVars(t *ast.Task, call *Call) (map[string]string, e allVars["TASK_DIR"] = filepathext.SmartJoin(c.Dir, t.Dir) allVars["TASKFILE"] = t.Location.Taskfile allVars["TASKFILE_DIR"] = filepath.Dir(t.Location.Taskfile) + } else { + allVars["TASK"] = "" + allVars["TASK_DIR"] = "" + allVars["TASKFILE"] = "" + allVars["TASKFILE_DIR"] = "" } if call != nil { allVars["ALIAS"] = call.Task + } else { + allVars["ALIAS"] = "" } return allVars, nil diff --git a/setup.go b/setup.go index b188e40f..f94bf54a 100644 --- a/setup.go +++ b/setup.go @@ -209,6 +209,10 @@ func (e *Executor) setupCompiler() error { } func (e *Executor) readDotEnvFiles() error { + if e.Taskfile == nil || len(e.Taskfile.Dotenv) == 0 { + return nil + } + if e.Taskfile.Version.LessThan(ast.V3) { return nil } diff --git a/taskfile/dotenv.go b/taskfile/dotenv.go index 6bf7cd35..a86a9eed 100644 --- a/taskfile/dotenv.go +++ b/taskfile/dotenv.go @@ -12,10 +12,6 @@ import ( ) func Dotenv(vars *ast.Vars, tf *ast.Taskfile, dir string) (*ast.Vars, error) { - if len(tf.Dotenv) == 0 { - return nil, nil - } - env := ast.NewVars() cache := &templater.Cache{Vars: vars}