Skip to content

Commit 386dcbc

Browse files
authored
fix: adjust run: when_changed to work correctly with imported tasks (#2511)
1 parent 799bc85 commit 386dcbc

File tree

7 files changed

+82
-5
lines changed

7 files changed

+82
-5
lines changed

internal/hash/hash.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ func Name(t *ast.Task) (string, error) {
2020

2121
func Hash(t *ast.Task) (string, error) {
2222
h, err := hashstructure.Hash(t, hashstructure.FormatV2, nil)
23-
return fmt.Sprintf("%s:%d", t.Task, h), err
23+
return fmt.Sprintf("%s:%s:%d", t.Location.Taskfile, t.LocalName(), h), err
2424
}

task_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,29 @@ func TestRunOnceSharedDeps(t *testing.T) {
18511851
assert.Contains(t, buff.String(), `task: [service-b:build] echo "build b"`)
18521852
}
18531853

1854+
func TestRunWhenChanged(t *testing.T) {
1855+
t.Parallel()
1856+
1857+
const dir = "testdata/run_when_changed"
1858+
1859+
var buff bytes.Buffer
1860+
e := task.NewExecutor(
1861+
task.WithDir(dir),
1862+
task.WithStdout(&buff),
1863+
task.WithStderr(&buff),
1864+
task.WithForceAll(true),
1865+
task.WithSilent(true),
1866+
)
1867+
require.NoError(t, e.Setup())
1868+
require.NoError(t, e.Run(t.Context(), &task.Call{Task: "start"}))
1869+
expectedOutputOrder := strings.TrimSpace(`
1870+
login server=fubar user=fubar
1871+
login server=foo user=foo
1872+
login server=bar user=bar
1873+
`)
1874+
assert.Contains(t, buff.String(), expectedOutputOrder)
1875+
}
1876+
18541877
func TestDeferredCmds(t *testing.T) {
18551878
t.Parallel()
18561879

taskfile/ast/task.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
// Task represents a task
1515
type Task struct {
16-
Task string
16+
Task string `hash:"ignore"`
1717
Cmds []*Cmd
1818
Deps []*Dep
1919
Label string
@@ -36,18 +36,18 @@ type Task struct {
3636
Interactive bool
3737
Internal bool
3838
Method string
39-
Prefix string
39+
Prefix string `hash:"ignore"`
4040
IgnoreError bool
4141
Run string
4242
Platforms []*Platform
4343
Watch bool
4444
Location *Location
4545
// Populated during merging
46-
Namespace string
46+
Namespace string `hash:"ignore"`
4747
IncludeVars *Vars
4848
IncludedTaskfileVars *Vars
4949

50-
FullName string
50+
FullName string `hash:"ignore"`
5151
}
5252

5353
func (t *Task) Name() string {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '3'
2+
3+
includes:
4+
service-a: ./service-a
5+
service-b: ./service-b
6+
7+
tasks:
8+
start:
9+
cmds:
10+
- task: service-a:start
11+
- task: service-b:start
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: '3'
2+
3+
tasks:
4+
login:
5+
run: when_changed
6+
cmds:
7+
- echo "login server={{.SERVER}} user={{.USER}}"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: '3'
2+
3+
includes:
4+
library:
5+
taskfile: ../library/Taskfile.yml
6+
dir: ../library
7+
8+
tasks:
9+
start:
10+
cmds:
11+
- task: library:login
12+
vars:
13+
SERVER: fubar
14+
USER: fubar
15+
- task: library:login
16+
vars:
17+
SERVER: foo
18+
USER: foo
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: '3'
2+
3+
includes:
4+
library:
5+
taskfile: ../library/Taskfile.yml
6+
dir: ../library
7+
8+
tasks:
9+
start:
10+
cmds:
11+
- task: library:login
12+
vars:
13+
SERVER: fubar
14+
USER: fubar
15+
- task: library:login
16+
vars:
17+
SERVER: bar
18+
USER: bar

0 commit comments

Comments
 (0)