Skip to content

Commit d19a73f

Browse files
Matt KeelerMatt Keeler
authored andcommitted
Fix task fingerprinting to account for the dir sources and generates
1 parent 54bdcba commit d19a73f

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package fingerprint
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/zeebo/xxh3"
8+
9+
"github.com/go-task/task/v3/taskfile/ast"
10+
)
11+
12+
type fingerprintIdentity struct {
13+
Task string `json:"task"`
14+
Dir string `json:"dir"`
15+
Sources []string `json:"sources,omitempty"`
16+
Generates []string `json:"generates,omitempty"`
17+
}
18+
19+
func taskFingerprintKey(t *ast.Task) string {
20+
name := taskIdentityName(t)
21+
identity := fingerprintIdentity{
22+
Task: name,
23+
Dir: t.Dir,
24+
Sources: globPatterns(t.Sources),
25+
Generates: globPatterns(t.Generates),
26+
}
27+
28+
encoded, err := json.Marshal(identity)
29+
if err != nil {
30+
return normalizeFilename(name)
31+
}
32+
33+
return normalizeFilename(fmt.Sprintf("%s-%x", name, xxh3.Hash(encoded)))
34+
}
35+
36+
func taskIdentityName(t *ast.Task) string {
37+
if t.FullName != "" {
38+
return t.FullName
39+
}
40+
return t.Task
41+
}
42+
43+
func globPatterns(globs []*ast.Glob) []string {
44+
if len(globs) == 0 {
45+
return nil
46+
}
47+
48+
patterns := make([]string, 0, len(globs))
49+
for _, glob := range globs {
50+
if glob == nil {
51+
continue
52+
}
53+
if glob.Negate {
54+
patterns = append(patterns, "!"+glob.Glob)
55+
continue
56+
}
57+
patterns = append(patterns, glob.Glob)
58+
}
59+
return patterns
60+
}

internal/fingerprint/sources_checksum.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (c *ChecksumChecker) checksum(t *ast.Task) (string, error) {
115115
}
116116

117117
func (checker *ChecksumChecker) checksumFilePath(t *ast.Task) string {
118-
return filepath.Join(checker.tempDir, "checksum", normalizeFilename(t.Name()))
118+
return filepath.Join(checker.tempDir, "checksum", taskFingerprintKey(t))
119119
}
120120

121121
var checksumFilenameRegexp = regexp.MustCompile("[^A-z0-9]")

internal/fingerprint/sources_timestamp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,5 @@ func (*TimestampChecker) OnError(t *ast.Task) error {
147147
}
148148

149149
func (checker *TimestampChecker) timestampFilePath(t *ast.Task) string {
150-
return filepath.Join(checker.tempDir, "timestamp", normalizeFilename(t.Task))
150+
return filepath.Join(checker.tempDir, "timestamp", taskFingerprintKey(t))
151151
}

0 commit comments

Comments
 (0)