Skip to content

Commit 830b745

Browse files
feat(remote): global tempDir when the path is absolute (#1661)
* feat(remote): global tempDir is the path is absolute * --wip-- [skip ci] * fix lint * rename checksum to fingerprint * chore: Empty-Commit to trigger CI * feat: add TASK_REMOTE_DIR * handle relative path for TASK_REMOTE_DIR * Remove unneedded extra blank lines Co-authored-by: Andrey Nering <[email protected]> * add docs about TASK_REMOTE_DIR --------- Co-authored-by: Andrey Nering <[email protected]>
1 parent b52d4e4 commit 830b745

File tree

8 files changed

+79
-33
lines changed

8 files changed

+79
-33
lines changed

help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (e *Executor) ToEditorOutput(tasks []*ast.Task, noStatus bool) (*editors.Ta
190190
}
191191
upToDate, err := fingerprint.IsTaskUpToDate(context.Background(), task,
192192
fingerprint.WithMethod(method),
193-
fingerprint.WithTempDir(e.TempDir),
193+
fingerprint.WithTempDir(e.TempDir.Fingerprint),
194194
fingerprint.WithDry(e.Dry),
195195
fingerprint.WithLogger(e.Logger),
196196
)

setup.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (e *Executor) readTaskfile(node taskfile.Node) error {
6969
e.Download,
7070
e.Offline,
7171
e.Timeout,
72-
e.TempDir,
72+
e.TempDir.Remote,
7373
e.Logger,
7474
)
7575
graph, err := reader.Read()
@@ -104,22 +104,44 @@ func (e *Executor) setupFuzzyModel() {
104104
}
105105

106106
func (e *Executor) setupTempDir() error {
107-
if e.TempDir != "" {
107+
if e.TempDir != (TempDir{}) {
108108
return nil
109109
}
110110

111111
if os.Getenv("TASK_TEMP_DIR") == "" {
112-
e.TempDir = filepathext.SmartJoin(e.Dir, ".task")
112+
e.TempDir = TempDir{
113+
Remote: filepathext.SmartJoin(e.Dir, ".task"),
114+
Fingerprint: filepathext.SmartJoin(e.Dir, ".task"),
115+
}
113116
} else if filepath.IsAbs(os.Getenv("TASK_TEMP_DIR")) || strings.HasPrefix(os.Getenv("TASK_TEMP_DIR"), "~") {
114117
tempDir, err := execext.Expand(os.Getenv("TASK_TEMP_DIR"))
115118
if err != nil {
116119
return err
117120
}
118121
projectDir, _ := filepath.Abs(e.Dir)
119122
projectName := filepath.Base(projectDir)
120-
e.TempDir = filepathext.SmartJoin(tempDir, projectName)
123+
e.TempDir = TempDir{
124+
Remote: tempDir,
125+
Fingerprint: filepathext.SmartJoin(tempDir, projectName),
126+
}
127+
121128
} else {
122-
e.TempDir = filepathext.SmartJoin(e.Dir, os.Getenv("TASK_TEMP_DIR"))
129+
e.TempDir = TempDir{
130+
Remote: filepathext.SmartJoin(e.Dir, os.Getenv("TASK_TEMP_DIR")),
131+
Fingerprint: filepathext.SmartJoin(e.Dir, os.Getenv("TASK_TEMP_DIR")),
132+
}
133+
}
134+
135+
if os.Getenv("TASK_REMOTE_DIR") != "" {
136+
if filepath.IsAbs(os.Getenv("TASK_TEMP_DIR")) || strings.HasPrefix(os.Getenv("TASK_TEMP_DIR"), "~") {
137+
remoteTempDir, err := execext.Expand(filepathext.SmartJoin(e.Dir, ".task"))
138+
if err != nil {
139+
return err
140+
}
141+
e.TempDir.Remote = remoteTempDir
142+
} else {
143+
e.TempDir.Remote = filepathext.SmartJoin(e.Dir, ".task")
144+
}
123145
}
124146

125147
return nil

status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (e *Executor) Status(ctx context.Context, calls ...*ast.Call) error {
2727
// Check if the task is up-to-date
2828
isUpToDate, err := fingerprint.IsTaskUpToDate(ctx, t,
2929
fingerprint.WithMethod(method),
30-
fingerprint.WithTempDir(e.TempDir),
30+
fingerprint.WithTempDir(e.TempDir.Fingerprint),
3131
fingerprint.WithDry(e.Dry),
3232
fingerprint.WithLogger(e.Logger),
3333
)
@@ -46,7 +46,7 @@ func (e *Executor) statusOnError(t *ast.Task) error {
4646
if method == "" {
4747
method = e.Taskfile.Method
4848
}
49-
checker, err := fingerprint.NewSourcesChecker(method, e.TempDir, e.Dry)
49+
checker, err := fingerprint.NewSourcesChecker(method, e.TempDir.Fingerprint, e.Dry)
5050
if err != nil {
5151
return err
5252
}

task.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@ const (
3434
MaximumTaskCall = 1000
3535
)
3636

37+
type TempDir struct {
38+
Remote string
39+
Fingerprint string
40+
}
41+
3742
// Executor executes a Taskfile
3843
type Executor struct {
3944
Taskfile *ast.Taskfile
4045

4146
Dir string
4247
Entrypoint string
43-
TempDir string
48+
TempDir TempDir
4449
Force bool
4550
ForceAll bool
4651
Insecure bool
@@ -212,7 +217,7 @@ func (e *Executor) RunTask(ctx context.Context, call *ast.Call) error {
212217

213218
upToDate, err := fingerprint.IsTaskUpToDate(ctx, t,
214219
fingerprint.WithMethod(method),
215-
fingerprint.WithTempDir(e.TempDir),
220+
fingerprint.WithTempDir(e.TempDir.Fingerprint),
216221
fingerprint.WithDry(e.Dry),
217222
fingerprint.WithLogger(e.Logger),
218223
)

task_test.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ func (fct fileContentTest) Run(t *testing.T) {
6262
}
6363

6464
e := &task.Executor{
65-
Dir: fct.Dir,
66-
TempDir: filepathext.SmartJoin(fct.Dir, ".task"),
65+
Dir: fct.Dir,
66+
TempDir: task.TempDir{
67+
Remote: filepathext.SmartJoin(fct.Dir, ".task"),
68+
Fingerprint: filepathext.SmartJoin(fct.Dir, ".task"),
69+
},
6770
Entrypoint: fct.Entrypoint,
6871
Stdout: io.Discard,
6972
Stderr: io.Discard,
@@ -272,11 +275,14 @@ func TestStatus(t *testing.T) {
272275

273276
var buff bytes.Buffer
274277
e := &task.Executor{
275-
Dir: dir,
276-
TempDir: filepathext.SmartJoin(dir, ".task"),
277-
Stdout: &buff,
278-
Stderr: &buff,
279-
Silent: true,
278+
Dir: dir,
279+
TempDir: task.TempDir{
280+
Remote: filepathext.SmartJoin(dir, ".task"),
281+
Fingerprint: filepathext.SmartJoin(dir, ".task"),
282+
},
283+
Stdout: &buff,
284+
Stderr: &buff,
285+
Silent: true,
280286
}
281287
require.NoError(t, e.Setup())
282288
// gen-foo creates foo.txt, and will always fail it's status check.
@@ -468,7 +474,10 @@ func TestStatusChecksum(t *testing.T) {
468474
}
469475

470476
var buff bytes.Buffer
471-
tempdir := filepathext.SmartJoin(dir, ".task")
477+
tempdir := task.TempDir{
478+
Remote: filepathext.SmartJoin(dir, ".task"),
479+
Fingerprint: filepathext.SmartJoin(dir, ".task"),
480+
}
472481
e := task.Executor{
473482
Dir: dir,
474483
TempDir: tempdir,
@@ -485,15 +494,15 @@ func TestStatusChecksum(t *testing.T) {
485494

486495
// Capture the modification time, so we can ensure the checksum file
487496
// is not regenerated when the hash hasn't changed.
488-
s, err := os.Stat(filepathext.SmartJoin(tempdir, "checksum/"+test.task))
497+
s, err := os.Stat(filepathext.SmartJoin(tempdir.Fingerprint, "checksum/"+test.task))
489498
require.NoError(t, err)
490499
time := s.ModTime()
491500

492501
buff.Reset()
493502
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: test.task}))
494503
assert.Equal(t, `task: Task "`+test.task+`" is up to date`+"\n", buff.String())
495504

496-
s, err = os.Stat(filepathext.SmartJoin(tempdir, "checksum/"+test.task))
505+
s, err = os.Stat(filepathext.SmartJoin(tempdir.Fingerprint, "checksum/"+test.task))
497506
require.NoError(t, err)
498507
assert.Equal(t, time, s.ModTime())
499508
})
@@ -814,8 +823,11 @@ func TestStatusVariables(t *testing.T) {
814823

815824
var buff bytes.Buffer
816825
e := task.Executor{
817-
Dir: dir,
818-
TempDir: filepathext.SmartJoin(dir, ".task"),
826+
Dir: dir,
827+
TempDir: task.TempDir{
828+
Remote: filepathext.SmartJoin(dir, ".task"),
829+
Fingerprint: filepathext.SmartJoin(dir, ".task"),
830+
},
819831
Stdout: &buff,
820832
Stderr: &buff,
821833
Silent: false,
@@ -963,11 +975,14 @@ func TestDryChecksum(t *testing.T) {
963975
_ = os.Remove(checksumFile)
964976

965977
e := task.Executor{
966-
Dir: dir,
967-
TempDir: filepathext.SmartJoin(dir, ".task"),
968-
Stdout: io.Discard,
969-
Stderr: io.Discard,
970-
Dry: true,
978+
Dir: dir,
979+
TempDir: task.TempDir{
980+
Remote: filepathext.SmartJoin(dir, ".task"),
981+
Fingerprint: filepathext.SmartJoin(dir, ".task"),
982+
},
983+
Stdout: io.Discard,
984+
Stderr: io.Discard,
985+
Dry: true,
971986
}
972987
require.NoError(t, e.Setup())
973988
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "default"}))

variables.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
222222
}
223223

224224
if len(origTask.Status) > 0 {
225-
timestampChecker := fingerprint.NewTimestampChecker(e.TempDir, e.Dry)
226-
checksumChecker := fingerprint.NewChecksumChecker(e.TempDir, e.Dry)
225+
timestampChecker := fingerprint.NewTimestampChecker(e.TempDir.Fingerprint, e.Dry)
226+
checksumChecker := fingerprint.NewChecksumChecker(e.TempDir.Fingerprint, e.Dry)
227227

228228
for _, checker := range []fingerprint.SourcesCheckable{timestampChecker, checksumChecker} {
229229
value, err := checker.Value(&new)

website/docs/experiments/remote_taskfiles.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ and look for a cached copy instead. This timeout can be configured by setting
112112
the `--timeout` flag and specifying a duration. For example, `--timeout 5s` will
113113
set the timeout to 5 seconds.
114114

115+
By default, the cache is stored in the Task temp directory, represented by the `TASK_TEMP_DIR` [environment variable](../reference/environment.mdx)
116+
You can override the location of the cache by setting the `TASK_REMOTE_DIR` environment variable. This way, you can share the cache between different projects.
117+
115118
{/* prettier-ignore-start */}
116119
[enabling-experiments]: ./experiments.mdx#enabling-experiments
117120
[man-in-the-middle-attacks]: https://en.wikipedia.org/wiki/Man-in-the-middle_attack

website/docs/reference/environment.mdx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ sidebar_position: 4
88
Task allows you to configure some behavior using environment variables. This
99
page lists all the environment variables that Task supports.
1010

11-
| ENV | Default | Description |
12-
| --------------- | ------- | ----------------------------------------------------------------------------------------------------------------- |
13-
| `TASK_TEMP_DIR` | `.task` | Location of the temp dir. Can relative to the project like `tmp/task` or absolute like `/tmp/.task` or `~/.task`. |
14-
| `FORCE_COLOR` | | Force color output usage. |
11+
| ENV | Default | Description |
12+
| ----------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
13+
| `TASK_TEMP_DIR` | `.task` | Location of the temp dir. Can relative to the project like `tmp/task` or absolute like `/tmp/.task` or `~/.task`. |
14+
| `TASK_REMOTE_DIR` | `TASK_TEMP_DIR` | Location of the remote temp dir (used for caching). Can relative to the project like `tmp/task` or absolute like `/tmp/.task` or `~/.task`. |
15+
| `FORCE_COLOR` | | Force color output usage. |
1516

1617
## Custom Colors
1718

0 commit comments

Comments
 (0)