Fingerprinting Source and Generated Files
Avoid unnecessary work by only running tasks when their source files have changed. Taskfile compares checksums ofsources files to detect changes.
Taskfile.yaml
| Field | Description |
|---|---|
sources | Files to fingerprint. Task only runs if they’ve changed. Supports glob patterns. |
generates | Files produced by the task. If deleted, task re-runs. |
exclude | Exclude files from fingerprinting (must follow a positive glob). |
method: checksum | Default. Compares file checksums. |
method: timestamp | Compares file modification times. Faster for large files. |
method: none | Skips validation — task always runs. |
Demo and Output
A
.task directory is created in the source directory to store fingerprint data. Add it to .gitignore to avoid committing it.To customize its location: export TASK_TEMP_DIR=/path/to/custom/.taskProgrammatic Status Checks
Use thestatus field to define shell commands that indicate whether a task is up to date. If all status commands exit with code 0, the task is skipped.
- On the first run, status checks fail (files don’t exist), so commands execute.
- On subsequent runs, status checks pass, so the task is skipped.
Taskfile.yaml
Demo and Output
Combining sources and status
When both are used, the task runs if either the sources change or a status check fails:
Taskfile.yaml
Precondition Checks
Usepreconditions to run checks before the task and its dependencies execute. If any precondition fails, the task and all its dependencies are canceled.
Taskfile.yaml
Taskfile.yaml — dependency cancellation example
status vs preconditions
| Feature | status | preconditions |
|---|---|---|
| Purpose | Check if task is up to date | Ensure requirements are met before running |
| Effect on Task | Skips task if up to date; dependent tasks continue | Fails task and cancels all dependent tasks |
Limiting When Tasks Run
run can also be set at the root level of the Taskfile to apply to all tasks unless overridden individually.run field to control how often a task executes when called multiple times:
| Value | Behavior |
|---|---|
always | Default. Runs every time it is called. |
once | Runs only once, regardless of how many times it’s called. |
when_changed | Runs once per unique set of input variables. |
Taskfile.yaml
Demo and Output
install-deps runs only once even though it is a dependency of generate-file called three times. generate-file with CONTENT: '2' runs only once because when_changed deduplicates identical variable sets.