Repository: go-task/task (Go)

Issue:
Special `CHECKSUM` variable only ever yields `d41d8cd98f00b204e9800998ecf8427e`
The special `CHECKSUM` variable available to `status` commands only ever yields `d41d8cd98f00b204e9800998ecf8427e`. This appears to be irrespective of the contents of any files in the `sources` list! 😮

## Environment

- Task version: `Task version: v3.22.0 (h1:sW+qi054ReRpX7nG8DcKYq3e4IzfSq366RcqEq2lDxc=)`
- Operating System: `macOS Ventura 13.2.1 (22D68)`

## Steps to reproduce

1. Create a `Taskfile.yml` file with the following contents:
   ```yaml
   version: '3'

   tasks:
     checksum:test:
       cmds:
         - cat checksum
       method: checksum
       sources:
         - '*.txt'
       status:
         - echo "{{.CHECKSUM}}" >checksum
   ```
2. Create a `file.txt` file with any contents
3. Run `task --force checksum:test` and note the printed checksum
4. Make any change to the contents of `file.txt`
5. Run `task --force checksum:test` again and note the printed checksum

## Actual results

- Both printed checksums are `d41d8cd98f00b204e9800998ecf8427e` 😢

## Expected results

- The second checksum is different from the first
- Both checksums align with the contents of `file.txt` at the time they're printed


Discussion:
@treykasada Thanks for the report. This should be fixed in #1080. As I suggested there, I don't think this ever worked. The [original PR](https://github.com/go-task/task/pull/216) tests were checking for the value that you reported is always being returned.
Wow, thanks for putting up a fix so quickly @pd93! 😄



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b2fe27f905..a7420c5f4e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@
   (`{{splitArgs "foo bar 'foo bar baz'"}}`) to ensure string is splitted as
   arguments not whitespaces
   ([#1040](https://github.com/go-task/task/issues/1040), [#1059](https://github.com/go-task/task/pull/1059) by @dhanusaputra).
+- Fix the value of `{{.CHECKSUM}}` variable in status ([#1076](https://github.com/go-task/task/issues/1076), [#1080](https://github.com/go-task/task/pull/1080) by @pd93).
 
 ## v3.22.0 - 2023-03-10
 
diff --git a/internal/fingerprint/sources_checksum.go b/internal/fingerprint/sources_checksum.go
index 85680da5fb..64e57f3f56 100644
--- a/internal/fingerprint/sources_checksum.go
+++ b/internal/fingerprint/sources_checksum.go
@@ -37,12 +37,7 @@ func (checker *ChecksumChecker) IsUpToDate(t *taskfile.Task) (bool, error) {
 	data, _ := os.ReadFile(checksumFile)
 	oldMd5 := strings.TrimSpace(string(data))
 
-	sources, err := globs(t.Dir, t.Sources)
-	if err != nil {
-		return false, err
-	}
-
-	newMd5, err := checker.checksum(sources...)
+	newMd5, err := checker.checksum(t)
 	if err != nil {
 		return false, nil
 	}
@@ -74,7 +69,7 @@ func (checker *ChecksumChecker) IsUpToDate(t *taskfile.Task) (bool, error) {
 }
 
 func (checker *ChecksumChecker) Value(t *taskfile.Task) (interface{}, error) {
-	return checker.checksum()
+	return checker.checksum(t)
 }
 
 func (checker *ChecksumChecker) OnError(t *taskfile.Task) error {
@@ -88,10 +83,14 @@ func (*ChecksumChecker) Kind() string {
 	return "checksum"
 }
 
-func (c *ChecksumChecker) checksum(files ...string) (string, error) {
-	h := md5.New()
+func (c *ChecksumChecker) checksum(t *taskfile.Task) (string, error) {
+	sources, err := globs(t.Dir, t.Sources)
+	if err != nil {
+		return "", err
+	}
 
-	for _, f := range files {
+	h := md5.New()
+	for _, f := range sources {
 		// also sum the filename, so checksum changes for renaming a file
 		if _, err := io.Copy(h, strings.NewReader(filepath.Base(f))); err != nil {
 			return "", err


Test patch:
diff --git a/task_test.go b/task_test.go
index 36c444f6ec..7aafa6cc42 100644
--- a/task_test.go
+++ b/task_test.go
@@ -691,7 +691,7 @@ func TestStatusVariables(t *testing.T) {
 	assert.NoError(t, e.Setup())
 	assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"}))
 
-	assert.Contains(t, buff.String(), "d41d8cd98f00b204e9800998ecf8427e")
+	assert.Contains(t, buff.String(), "a41e7948dcd321db412ce61d3d5c9864")
 
 	inf, err := os.Stat(filepathext.SmartJoin(dir, "source.txt"))
 	assert.NoError(t, err)