From 9910b84aa03d590ea49f165446722ea4813a237f Mon Sep 17 00:00:00 2001 From: Marcus Spading Date: Fri, 21 Jul 2023 17:11:02 +0200 Subject: [PATCH] fix: wrong working directory with USER_WORKING_DIR in included Taskfiles (#1250) When using `dir: '{{.USER_WORKING_DIR}}'` in an included task, the working directory was set incorrectly by concatenating the base Taskfile directory and the USER_WORKING_DIR. --- internal/compiler/v3/compiler_v3.go | 4 ++++ task_test.go | 14 ++++++++++++++ testdata/user_working_dir_pwd/Taskfile.yml | 5 +++++ .../user_working_dir_pwd/included/Taskfile.yml | 8 ++++++++ 4 files changed, 31 insertions(+) create mode 100644 testdata/user_working_dir_pwd/Taskfile.yml create mode 100644 testdata/user_working_dir_pwd/included/Taskfile.yml diff --git a/internal/compiler/v3/compiler_v3.go b/internal/compiler/v3/compiler_v3.go index a5c6a48a9b..9fabae34de 100644 --- a/internal/compiler/v3/compiler_v3.go +++ b/internal/compiler/v3/compiler_v3.go @@ -84,6 +84,10 @@ func (c *CompilerV3) getVariables(t *taskfile.Task, call *taskfile.Call, evaluat var taskRangeFunc func(k string, v taskfile.Var) error if t != nil { + if strings.HasSuffix(t.Dir, "{{.USER_WORKING_DIR}}") { + t.Dir = "{{.USER_WORKING_DIR}}" + } + // NOTE(@andreynering): We're manually joining these paths here because // this is the raw task, not the compiled one. tr := templater.Templater{Vars: result, RemoveNoValue: true} diff --git a/task_test.go b/task_test.go index c3b2bc3e86..ff2604303d 100644 --- a/task_test.go +++ b/task_test.go @@ -1922,6 +1922,20 @@ func TestUserWorkingDirectory(t *testing.T) { assert.Equal(t, fmt.Sprintf("%s\n", wd), buff.String()) } +func TestUserWorkingDirectoryPwd(t *testing.T) { + var buff bytes.Buffer + e := task.Executor{ + Dir: "testdata/user_working_dir_pwd", + Stdout: &buff, + Stderr: &buff, + } + wd, err := os.Getwd() + require.NoError(t, err) + require.NoError(t, e.Setup()) + require.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "included:default"})) + assert.Equal(t, fmt.Sprintf("%s\n", wd), buff.String()) +} + func TestPlatforms(t *testing.T) { var buff bytes.Buffer e := task.Executor{ diff --git a/testdata/user_working_dir_pwd/Taskfile.yml b/testdata/user_working_dir_pwd/Taskfile.yml new file mode 100644 index 0000000000..a541c54110 --- /dev/null +++ b/testdata/user_working_dir_pwd/Taskfile.yml @@ -0,0 +1,5 @@ +version: '3' + +includes: + included: + taskfile: ./included diff --git a/testdata/user_working_dir_pwd/included/Taskfile.yml b/testdata/user_working_dir_pwd/included/Taskfile.yml new file mode 100644 index 0000000000..913cc3df3b --- /dev/null +++ b/testdata/user_working_dir_pwd/included/Taskfile.yml @@ -0,0 +1,8 @@ +version: '3' + +tasks: + default: + dir: '{{.USER_WORKING_DIR}}' + cmds: + - pwd + silent: true