Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c72511e
Improve checksum compute and validate error handling; add unit tests.
davidlion Aug 26, 2025
394d45f
Fix yaml lint; sanity check
davidlion Aug 26, 2025
3b5a816
one more sanity check
davidlion Aug 26, 2025
9c935f9
one more
davidlion Aug 26, 2025
07b88fc
last one
davidlion Aug 26, 2025
20802e7
remove test
davidlion Aug 26, 2025
a2ff6f4
Fix err log.
davidlion Aug 26, 2025
194440e
Drop mktemp due to task bug.
davidlion Aug 26, 2025
3a92d53
Add force to rm tmp files.
davidlion Aug 26, 2025
c31ef24
sub shells
davidlion Aug 26, 2025
07fd3e4
Update readme with macos requirements.
davidlion Aug 26, 2025
4ec6588
Tweak default list handling.
davidlion Aug 26, 2025
93a6296
readme tweak
davidlion Aug 26, 2025
5a230b9
doc string tweaks
davidlion Aug 26, 2025
01280e1
fix sub shell and indentation.
davidlion Aug 26, 2025
b6df417
Add -f when removing checksum on failed compute.
davidlion Aug 26, 2025
882c5be
Merge remote-tracking branch 'upstream/main' into checksum-fix
davidlion Sep 22, 2025
fbe8a72
Undo testing comment.
davidlion Sep 22, 2025
8534c24
Apply suggestions from code review.
davidlion Oct 15, 2025
40d4579
Merge remote-tracking branch 'upstream/main' into checksum-fix
davidlion Oct 15, 2025
bbb32bc
Add individual checksum task to tests.
davidlion Oct 28, 2025
d53221e
Merge remote-tracking branch 'upstream/main' into checksum-fix
davidlion Oct 31, 2025
3871d7d
Apply suggestions from code review.
davidlion Nov 14, 2025
35e3773
Merge branch 'main' into checksum-fix
davidlion Nov 14, 2025
d07138e
Address review comments and tweak if statements.
davidlion Nov 14, 2025
8363fc4
Add pipefail note to compute.
davidlion Nov 14, 2025
3bd67a0
Fix coderabbit nits.
davidlion Nov 14, 2025
8f70d9b
Apply suggestions from code review.
davidlion Nov 17, 2025
8a51626
Reflow test. Change writing tmp_err_log to append.
davidlion Nov 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ Before you submit a pull request, ensure you follow the testing and linting inst
* [Task] 3.40 or higher
* [uv] 0.7.10 or higher

### macOS

The exported tasks use GNU utilities that are not always pre-installed on macOS. You may need to
install the following brew packages and add their executables to your PATH:

* [coreutils]\: `md5sum`
* [gnu-tar]\: `gtar`

## Testing

To run all tests:
Expand Down Expand Up @@ -42,5 +50,7 @@ To clean up any generated files:
task clean
```

[coreutils]: https://formulae.brew.sh/formula/coreutils
[gnu-tar]: https://formulae.brew.sh/formula/gnu-tar
[Task]: https://taskfile.dev/
[uv]: https://docs.astral.sh/uv
125 changes: 87 additions & 38 deletions exports/taskfiles/utils/checksum.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
version: "3"

set: ["u", "pipefail"]
shopt: ["globstar"]

tasks:

# Compute the checksum of the given path include/exclude patterns, saving the result to
# `CHECKSUM_FILE`. The calling task can set `FAIL` to "false" if they wish to continue if checksum
# computation fails.
#
# @param {string} CHECKSUM_FILE
# @param {string[]} INCLUDE_PATTERNS Path wildcard patterns to compute the checksum for.
# @param {string[]} [EXCLUDE_PATTERNS] Path wildcard patterns, relative to any `INCLUDE_PATTERNS`,
# to exclude from the checksum.
# @param {string} [FAIL="true"] If set to "false" the task will not fail.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to change this into IGNORE_ERROR with default = false?

  1. the name FAIL can be confusing. i feel IGNORE_ERROR can be more self-explanatory
  2. it's usually better to use false as the default value of any Booleans
  3. we can early return right after rm -f "{{.CHECKSUM_FILE}}"

compute:
desc: "Tries to compute a checksum for the given paths and output it to a file."
internal: true
label: "{{.TASK}}-{{.CHECKSUM_FILE}}"
silent: true
vars:
EXCLUDE_PATTERNS:
ref: "default (list) .EXCLUDE_PATTERNS"
FAIL: "{{if eq \"false\" .FAIL}}false{{else}}true{{end}}"

TAR: "{{if eq OS \"darwin\"}}gtar{{else}}tar{{end}}"
TMP_ERR_LOG: "{{.CHECKSUM_FILE}}.log.tmp"
Copy link
Contributor

@coderabbitai coderabbitai bot Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Prefer capability detection over OS check for TAR

Some Linux/Alpine images ship BusyBox tar which lacks these flags. Consider preferring gtar if present, falling back to tar.

-      TAR: "{{if eq OS \"darwin\"}}gtar{{else}}tar{{end}}"
+      TAR:
+        sh: |
+          if command -v gtar >/dev/null 2>&1; then echo gtar; else echo tar; fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
TAR: "{{if eq OS \"darwin\"}}gtar{{else}}tar{{end}}"
TMP_ERR_LOG: "{{.CHECKSUM_FILE}}.log.tmp"
TAR:
sh: |
if command -v gtar >/dev/null 2>&1; then echo gtar; else echo tar; fi
TMP_ERR_LOG: "{{.CHECKSUM_FILE}}.log.tmp"
🤖 Prompt for AI Agents
In exports/taskfiles/utils/checksum.yaml around lines 23-24, replace the
OS-based TAR selection with a runtime capability check that prefers gtar when
present and falls back to tar; specifically, change the template expression to
run a shell command like `command -v gtar` (or `which gtar`) at runtime and use
gtar if it exists, otherwise use tar, keeping TMP_ERR_LOG unchanged.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will improve this once we add Linux/Alphine CIs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bill-hbrhbr, understood! That makes sense to defer this until you have Linux/Alpine CI environments to properly test the capability detection. Would you like me to create a GitHub issue to track this improvement for when the CI infrastructure is ready?

requires:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add MD5_CMD and temp artifact vars for portability and atomic writes

md5sum is not present by default on macOS; Homebrew installs gmd5sum. Also, writing directly to CHECKSUM_FILE risks partial files on failure. Introduce MD5_CMD and temp files now (used in compute refactor below).

Apply:

       TMP_ERR_LOG: "{{.CHECKSUM_FILE}}.log.tmp"
+      TMP_TAR_FILE: "{{.CHECKSUM_FILE}}.tar.tmp"
+      TMP_CHECKSUM_FILE: "{{.CHECKSUM_FILE}}.tmp"
+      MD5_CMD:
+        sh: |
+          if command -v gmd5sum >/dev/null 2>&1; then
+            echo gmd5sum
+          elif command -v md5sum >/dev/null 2>&1; then
+            echo md5sum
+          elif command -v md5 >/dev/null 2>&1; then
+            # macOS fallback; -r prints digest first to match *md5sum output style
+            echo 'md5 -r'
+          else
+            echo 'false'  # no-op that will fail
+          fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
TAR: "{{if eq OS \"darwin\"}}gtar{{else}}tar{{end}}"
TMP_ERR_LOG: "{{.CHECKSUM_FILE}}.log.tmp"
requires:
TAR: "{{if eq OS \"darwin\"}}gtar{{else}}tar{{end}}"
TMP_ERR_LOG: "{{.CHECKSUM_FILE}}.log.tmp"
TMP_TAR_FILE: "{{.CHECKSUM_FILE}}.tar.tmp"
TMP_CHECKSUM_FILE: "{{.CHECKSUM_FILE}}.tmp"
MD5_CMD:
sh: |
if command -v gmd5sum >/dev/null 2>&1; then
echo gmd5sum
elif command -v md5sum >/dev/null 2>&1; then
echo md5sum
elif command -v md5 >/dev/null 2>&1; then
# macOS fallback; -r prints digest first to match *md5sum output style
echo 'md5 -r'
else
echo 'false' # no-op that will fail
fi
requires:
🤖 Prompt for AI Agents
In exports/taskfiles/utils/checksum.yaml around lines 23-25, add a
platform-aware MD5_CMD variable and explicit temp artifact vars for atomic
writes: define MD5_CMD to use gmd5sum on darwin and md5sum otherwise, and add
CHECKSUM_TMP (e.g. "{{.CHECKSUM_FILE}}.tmp") and retain TMP_ERR_LOG (or rename
to CHECKSUM_ERR_LOG if desired) so compute steps can write to temp files and
then atomically mv to CHECKSUM_FILE; update downstream steps to use these new
vars for generating checksums and error logs.

vars: ["CHECKSUM_FILE", "INCLUDE_PATTERNS"]
vars:
- "CHECKSUM_FILE"
- "INCLUDE_PATTERNS"
cmds:
# We explicitly set `--no-anchored` and `--wildcards` to make the inclusion behaviour match
# the default exclusion behaviour.
Expand All @@ -24,58 +35,96 @@ tasks:
# input patterns cannot be quoted since they're evaluated by the shell and the results are
# passed to `tar` as arguments. If the input patterns are passed to `tar` with quotes, the
# pattern won't be evaluated and will instead be treated literally.
- >-
tar
--create
--file -
--group 0
--mtime "UTC 1970-01-01"
--numeric-owner
--owner 0
--sort name
--no-anchored
--wildcards
{{- range .EXCLUDE_PATTERNS}}
--exclude="{{.}}"
{{- end}}
{{- range .INCLUDE_PATTERNS}}
{{.}}
{{- end}}
2> /dev/null
| md5sum > {{.CHECKSUM_FILE}}
# Ignore errors so that dependent tasks don't fail
ignore_error: true
- defer: "rm -f '{{.TMP_ERR_LOG}}'"
- |-
if ! \
{{.TAR}} \
--create \
--file - \
--group 0 \
--mtime "UTC 1970-01-01" \
--numeric-owner \
--owner 0 \
--sort name \
--no-anchored \
--wildcards \
{{- range .EXCLUDE_PATTERNS}}
--exclude="{{.}}" \
{{- end}}
{{- range .INCLUDE_PATTERNS}}
{{.}} \
{{- end}}
2> "{{.TMP_ERR_LOG}}" \
| md5sum > "{{.CHECKSUM_FILE}}" \
; then
rm -f "{{.CHECKSUM_FILE}}"
{{- if eq "true" .FAIL}}
printf "[{{.TASK}} error] failed with:\n%s\n" "$(cat {{.TMP_ERR_LOG}})"
exit 1
{{- else}}
exit 0
{{- end}}
fi

# Validates the checksum of the given path include/exclude patterns matches the checksum in the
# given file. If validation fails the checksum file is deleted, but the task succeeds so that the
# calling task will be reran (by using the checksum file in their `generates` field). The calling
# task can set `FAIL` to "true" if they wish for this task to fail if validation fails.
#
# @param {string} CHECKSUM_FILE
# @param {string[]} INCLUDE_PATTERNS Path wildcard patterns to validate the checksum for.
# @param {string[]} [EXCLUDE_PATTERNS] Path wildcard patterns, relative to any `INCLUDE_PATTERNS`,
# to exclude from the checksum.
# @param {string} [FAIL="false"] If set to "true" the task fails.
validate:
desc: "Validates the checksum of the given directory matches the checksum in the given file, or
deletes the checksum file otherwise."
internal: true
label: "{{.TASK}}-{{.CHECKSUM_FILE}}"
silent: true
vars:
FAIL: "{{if eq \"true\" .FAIL}}true{{else}}false{{end}}"
TMP_CHECKSUM_FILE: "{{.CHECKSUM_FILE}}.tmp"
TMP_ERR_LOG: "{{.CHECKSUM_FILE}}.log.tmp"
requires:
vars: ["CHECKSUM_FILE", "INCLUDE_PATTERNS"]
vars:
- "CHECKSUM_FILE"
- "INCLUDE_PATTERNS"
cmds:
- task: "compute"
vars:
CHECKSUM_FILE: "{{.TMP_CHECKSUM_FILE}}"
INCLUDE_PATTERNS:
ref: ".INCLUDE_PATTERNS"
EXCLUDE_PATTERNS:
ref: "default (list) .EXCLUDE_PATTERNS"
CHECKSUM_FILE: "{{.TMP_CHECKSUM_FILE}}"
- defer: "rm -f '{{.TMP_CHECKSUM_FILE}}'"
# Check that all paths exist and the checksum matches; otherwise delete the checksum file.
ref: ".EXCLUDE_PATTERNS"
FAIL: "false"
- defer: |-
rm -f "{{.TMP_CHECKSUM_FILE}}"
rm -f "{{.TMP_ERR_LOG}}"
- |-
(
{{- range .INCLUDE_PATTERNS}}
for path in {{.}}; do
test -e "$path"
done
for path in {{.}}; do
test -e "${path}" \
|| (
echo "Include path does not exist: ${path}" > "{{$.TMP_ERR_LOG}}"
exit 1
)
done && \
{{- end}}
diff -q "{{.TMP_CHECKSUM_FILE}}" "{{.CHECKSUM_FILE}}" 2> /dev/null
) || rm -f "{{.CHECKSUM_FILE}}"
(
cmp -s "{{.TMP_CHECKSUM_FILE}}" "{{.CHECKSUM_FILE}}" \
|| (
echo "cmp failed for '{{.TMP_CHECKSUM_FILE}}' '{{.CHECKSUM_FILE}}'" \
> "{{.TMP_ERR_LOG}}"
exit 1
)
)
) \
|| (
{{- if eq "true" .FAIL}}
printf "[{{.TASK}} error] failed with:\n%s\n" "$(cat {{.TMP_ERR_LOG}})"
exit 1
{{- else}}
rm -f "{{.CHECKSUM_FILE}}"
{{- end}}
)
148 changes: 148 additions & 0 deletions taskfiles/checksum/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
version: "3"

includes:
checksum:
internal: true
taskfile: "../../exports/taskfiles/utils/checksum.yaml"

tasks:
default:
cmds:
- task: "checksum-test-rerun"
- task: "checksum-test-skip"
- task: "checksum-test-update"

checksum-test-rerun:
vars:
OUTPUT_DIR: "{{.G_OUTPUT_DIR}}/{{.TASK | replace \":\" \"#\"}}"
SRC_DIR: "{{.OUTPUT_DIR}}/src"

CHECKSUM_FILE: "{{.SRC_DIR}}.md5"
CHECKSUM_FILE_REF: "{{.CHECKSUM_FILE}}.ref"
FILE_0: "{{.SRC_DIR}}/0.txt"
FILE_1: "{{.SRC_DIR}}/1.txt"
cmds:
- task: "checksum-test-init"
vars:
OUTPUT_DIR: "{{.OUTPUT_DIR}}"
- task: "create-dir-with-checksum"
vars:
CHECKSUM_FILE: "{{.CHECKSUM_FILE}}"
FILE_PATH: "{{.FILE_0}}"
- "mv '{{.CHECKSUM_FILE}}' '{{.CHECKSUM_FILE_REF}}'"
- task: "create-dir-with-checksum"
vars:
CHECKSUM_FILE: "{{.CHECKSUM_FILE}}"
FILE_PATH: "{{.FILE_1}}"

# Test create-dir-with-checksum ran the second time and created a different checksum.
- "test ! -e '{{.FILE_0}}'"
- "test -e '{{.FILE_1}}'"
- |-
if ! cmp -s '{{.CHECKSUM_FILE}}' '{{.CHECKSUM_FILE_REF}}'; then
exit 0
fi
exit 1

checksum-test-skip:
vars:
OUTPUT_DIR: "{{.G_OUTPUT_DIR}}/{{.TASK | replace \":\" \"#\"}}"
SRC_DIR: "{{.OUTPUT_DIR}}/src"

CHECKSUM_FILE: "{{.SRC_DIR}}.md5"
CHECKSUM_MOD_TS: "{{.CHECKSUM_FILE}}-mod-ts.txt"
FILE_0: "{{.SRC_DIR}}/0.txt"
FILE_1: "{{.SRC_DIR}}/1.txt"
cmds:
- task: "checksum-test-init"
vars:
OUTPUT_DIR: "{{.OUTPUT_DIR}}"
- task: "create-dir-with-checksum"
vars:
CHECKSUM_FILE: "{{.CHECKSUM_FILE}}"
FILE_PATH: "{{.FILE_0}}"
- "date -r '{{.CHECKSUM_FILE}}' > '{{.CHECKSUM_MOD_TS}}'"
- task: "create-dir-with-checksum"
vars:
CHECKSUM_FILE: "{{.CHECKSUM_FILE}}"
FILE_PATH: "{{.FILE_1}}"

# Test create-dir-with-checksum didn't run the second time and the checksum is unmodified.
- "test -e '{{.FILE_0}}'"
- "test ! -e '{{.FILE_1}}'"
- "cmp -s '{{.CHECKSUM_MOD_TS}}' <(date -r '{{.CHECKSUM_FILE}}')"

checksum-test-update:
vars:
OUTPUT_DIR: "{{.G_OUTPUT_DIR}}/{{.TASK | replace \":\" \"#\"}}"
SRC_DIR: "{{.OUTPUT_DIR}}/src"

CHECKSUM_FILE: "{{.SRC_DIR}}.md5"
CHECKSUM_FILE_REF0: "{{.CHECKSUM_FILE}}.ref0"
CHECKSUM_FILE_REF1: "{{.CHECKSUM_FILE}}.ref1"
FILE_0: "{{.SRC_DIR}}/0.txt"
FILE_1: "{{.SRC_DIR}}/1.txt"
cmds:
- task: "checksum-test-init"
vars:
OUTPUT_DIR: "{{.OUTPUT_DIR}}"
- task: "create-dir-with-checksum"
vars:
CHECKSUM_FILE: "{{.CHECKSUM_FILE}}"
FILE_PATH: "{{.FILE_0}}"
- "cp '{{.CHECKSUM_FILE}}' '{{.CHECKSUM_FILE_REF0}}'"

- "cat '{{.CHECKSUM_FILE}}' > '{{.FILE_0}}'"
- task: "checksum:compute"
vars:
CHECKSUM_FILE: "{{.CHECKSUM_FILE}}"
INCLUDE_PATTERNS: ["{{.SRC_DIR}}"]
- "cp '{{.CHECKSUM_FILE}}' '{{.CHECKSUM_FILE_REF1}}'"

- task: "create-dir-with-checksum"
vars:
CHECKSUM_FILE: "{{.CHECKSUM_FILE}}"
FILE_PATH: "{{.FILE_1}}"

# Test create-dir-with-checksum didn't run the second time and the updated checksum is
# different from the original.
- "test -e '{{.FILE_0}}'"
- "test ! -e '{{.FILE_1}}'"
- "cmp -s '{{.FILE_0}}' '{{.CHECKSUM_FILE_REF0}}'"
- "cmp -s '{{.CHECKSUM_FILE}}' '{{.CHECKSUM_FILE_REF1}}'"
- |-
if ! cmp -s '{{.CHECKSUM_FILE}}' '{{.CHECKSUM_FILE_REF0}}'; then
exit 0
fi
exit 1

checksum-test-init:
internal: true
requires:
vars: ["OUTPUT_DIR"]
cmds:
- "rm -rf '{{.OUTPUT_DIR}}'"
- "mkdir -p '{{.OUTPUT_DIR}}'"

create-dir-with-checksum:
internal: true
vars:
DIR: "{{dir .FILE_PATH}}"
requires:
vars: ["CHECKSUM_FILE", "FILE_PATH"]
sources: ["{{.TASKFILE}}"]
generates: ["{{.CHECKSUM_FILE}}"]
deps:
- task: "checksum:validate"
vars:
CHECKSUM_FILE: "{{.CHECKSUM_FILE}}"
INCLUDE_PATTERNS: ["{{.DIR}}"]
cmds:
- |-
rm -rf "{{.DIR}}"
mkdir -p "{{.DIR}}"
touch "{{.FILE_PATH}}"
- task: "checksum:compute"
vars:
CHECKSUM_FILE: "{{.CHECKSUM_FILE}}"
INCLUDE_PATTERNS: ["{{.DIR}}"]
2 changes: 2 additions & 0 deletions taskfiles/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: "3"

includes:
boost: "boost/tests.yaml"
checksum: "checksum/tests.yaml"
remote: "remote/tests.yaml"
ystdlib-py: "ystdlib-py/tests.yaml"

Expand All @@ -10,6 +11,7 @@ tasks:
internal: true
cmds:
- task: "boost"
- task: "checksum"
- task: "remote"
- task: "ystdlib-py"

Expand Down