From 768dca053b81d18e8c842e441fb194ba13c5e151 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Fri, 18 Apr 2025 22:27:30 -0300 Subject: [PATCH] fix: `.USER_WORKING_DIR` should contain the value of `--dir` if given (#2186) Closes #2102 Closes #2103 Co-authored-by: jaynis --- executor.go | 2 ++ task_test.go | 2 +- website/docs/reference/templating.mdx | 2 +- website/docs/usage.mdx | 6 ++++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/executor.go b/executor.go index a04877f0..329ddc13 100644 --- a/executor.go +++ b/executor.go @@ -4,6 +4,7 @@ import ( "context" "io" "os" + "path/filepath" "sync" "time" @@ -121,6 +122,7 @@ type dirOption struct { } func (o *dirOption) ApplyToExecutor(e *Executor) { + e.UserWorkingDir, _ = filepath.Abs(o.dir) e.Dir = o.dir } diff --git a/task_test.go b/task_test.go index b4e226f8..a2400cd3 100644 --- a/task_test.go +++ b/task_test.go @@ -2148,7 +2148,7 @@ func TestUserWorkingDirectory(t *testing.T) { var buff bytes.Buffer e := task.NewExecutor( - task.WithDir("testdata/user_working_dir"), + task.WithEntrypoint("testdata/user_working_dir/Taskfile.yml"), task.WithStdout(&buff), task.WithStderr(&buff), ) diff --git a/website/docs/reference/templating.mdx b/website/docs/reference/templating.mdx index 98d45bae..fc5e4fb0 100644 --- a/website/docs/reference/templating.mdx +++ b/website/docs/reference/templating.mdx @@ -115,7 +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. | -| `USER_WORKING_DIR` | The absolute path of the directory `task` was called from. | +| `USER_WORKING_DIR` | The absolute path of the directory `task` was called from, or the value of `--dir` (`-d`) if given. | | `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`. | | `TASK_VERSION` | The current version of task. | diff --git a/website/docs/usage.mdx b/website/docs/usage.mdx index 1942d947..e672cf82 100644 --- a/website/docs/usage.mdx +++ b/website/docs/usage.mdx @@ -61,6 +61,12 @@ In this example, we can run `cd ` and `task up` and as long as the `` directory contains a `docker-compose.yml`, the Docker composition will be brought up. +:::info + +`.USER_WORKING_DIR` will contain the value of the `--dir` (`-d`) flag, if given. + +::: + ### Running a global Taskfile If you call Task with the `--global` (alias `-g`) flag, it will look for your