1
0
mirror of https://github.com/go-task/task.git synced 2025-02-13 13:59:32 +02:00

fix: special variables are defined with dotenv at task level (#1810)

This commit is contained in:
Valentin Maerten 2024-10-09 03:14:23 -04:00 committed by GitHub
parent bdb3ffddd1
commit e078261f12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"maps"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -45,14 +46,12 @@ func (c *Compiler) FastGetVariables(t *ast.Task, call *ast.Call) (*ast.Vars, err
func (c *Compiler) getVariables(t *ast.Task, call *ast.Call, evaluateShVars bool) (*ast.Vars, error) { func (c *Compiler) getVariables(t *ast.Task, call *ast.Call, evaluateShVars bool) (*ast.Vars, error) {
result := GetEnviron() result := GetEnviron()
if t != nil { specialVars, err := c.getSpecialVars(t, call)
specialVars, err := c.getSpecialVars(t, call) if err != nil {
if err != nil { return nil, err
return nil, err }
} for k, v := range specialVars {
for k, v := range specialVars { result.Set(k, ast.Var{Value: v})
result.Set(k, ast.Var{Value: v})
}
} }
getRangeFunc := func(dir string) func(k string, v ast.Var) error { getRangeFunc := func(dir string) func(k string, v ast.Var) error {
@ -180,15 +179,18 @@ func (c *Compiler) ResetCache() {
} }
func (c *Compiler) getSpecialVars(t *ast.Task, call *ast.Call) (map[string]string, error) { func (c *Compiler) getSpecialVars(t *ast.Task, call *ast.Call) (map[string]string, error) {
return map[string]string{ allVars := map[string]string{
"TASK": t.Task,
"ALIAS": call.Task,
"TASK_EXE": filepath.ToSlash(os.Args[0]), "TASK_EXE": filepath.ToSlash(os.Args[0]),
"ROOT_TASKFILE": filepathext.SmartJoin(c.Dir, c.Entrypoint), "ROOT_TASKFILE": filepathext.SmartJoin(c.Dir, c.Entrypoint),
"ROOT_DIR": c.Dir, "ROOT_DIR": c.Dir,
"TASKFILE": t.Location.Taskfile,
"TASKFILE_DIR": filepath.Dir(t.Location.Taskfile),
"USER_WORKING_DIR": c.UserWorkingDir, "USER_WORKING_DIR": c.UserWorkingDir,
"TASK_VERSION": version.GetVersion(), "TASK_VERSION": version.GetVersion(),
}, nil }
if t != nil {
maps.Copy(allVars, map[string]string{"TASK": t.Task, "TASKFILE": t.Location.Taskfile, "TASKFILE_DIR": filepath.Dir(t.Location.Taskfile)})
}
if call != nil {
maps.Copy(allVars, map[string]string{"ALIAS": call.Task})
}
return allVars, nil
} }