github jdx/hk v2.13.0
v2.13.0: Opt-in step result caching and ref range access for steps

57 minutes ago

hk 2.13.0 adds two step features. With cache = true, a check that already passed is skipped when its input files haven't changed. Steps can also read the --from-ref/--to-ref range. This release also fixes terraform_validate/tofu_validate on Windows and Python detection for uv run --script scripts.

Added

  • Skip checks that already passed on the same file contents (#1835, @jdx). Set cache = true on a step. If its check exited 0 before and the same check runs again on the same file contents, hk skips the command and shows "passed before, unchanged". This helps when pre-commit reruns checks that hk check just passed. If a step reads more than the files it was given, such as a workspace-wide build or cargo clippy, list those inputs in cache_inputs. Those globs replace the job's file list in the cache key:

    ["clippy"] {
        glob = "*.rs"
        check = "cargo clippy --workspace -- -D warnings"
        cache = true
        cache_inputs = List("**/*.rs", "**/Cargo.toml", "Cargo.lock", "mise.toml")
    }

    The cache key includes:

    • the hk version and step name
    • the rendered command
    • the working directory
    • the step's env, shell and stdin
    • the contents of each input file

    Modification times are not part of the key, so touching a file does not trigger a rerun, but editing it does. Tool versions are not in the key either. To rerun when a tool version changes, add the file that pins it (for example mise.toml) to cache_inputs. Without cache_inputs, only set cache on a command that reads nothing but its own files. hk check and pre-commit share a cache entry only when they select the same files and render the same command.

    hk never skips fix commands, interactive steps or failed checks. Caching is off by default. Entries are stored in HK_CACHE_DIR/results. Entries unused for 14 days are deleted, and at most 2000 are kept. Set HK_CACHE=0 to turn the cache off.

  • Ref range available to steps (#1830, @jdx). Steps can now read the range that hk used to select files. It is available as the template variables {{from_ref}} / {{to_ref}}, which also work in condition expressions, and as the environment variables HK_FROM_REF / HK_TO_REF. The values come from --from-ref, --to-ref, --pr or the pre-push hook. They are empty when there is no range, as in a plain pre-commit, and a step's own env can override them. This lets tools that need a base commit share hk's range, so you no longer pass the ref twice:

    ["eslint"] {
        glob = "*.js"
        check = "eslint {{files}}"
        env { ["ESLINT_PLUGIN_DIFF_COMMIT"] = "{{from_ref}}" }
    }

    hk passes the refs exactly as given, without resolving them to a SHA or merge base. hk itself diffs from the merge base, so a tool given the raw from_ref may compare against a different base than hk used. Because HK_FROM_REF and HK_TO_REF are always set, steps that list them as required no longer get skipped.

Fixed

  • terraform_validate and tofu_validate on Windows (#1839, @jdx). On Windows, both builtins failed before running with f was unexpected at this time. because their POSIX shell pipeline ran through cmd.exe. They now set shell = "sh -o errexit -c". On Windows, this needs a POSIX sh with dirname and sort on PATH, such as the one from Git for Windows. Linux and macOS are unaffected.
  • uv run --script scripts detected as Python (#1847, @jdx). Extensionless scripts with a shebang like #!/usr/bin/env -S uv run --script (or -s, including forms with extra uv options) now get the python file type. Before, steps using types = List("python") skipped these scripts. uv shebangs without --script, such as uv run or uv tool run, still get no language tag. Arguments passed through env -S are now read correctly, including quoted and escaped ones.

Changed

  • Pkl evaluator updated to pklr 6 (#1850). hk's built-in Pkl evaluator now behaves more like Apple Pkl. Configs that pklr 5 accepted but Pkl rejects, such as type constraint violations or undeclared properties in amending modules, may now fail to evaluate.

Full Changelog: v2.12.0...v2.13.0

💚 Sponsor hk

hk is built and maintained by @jdx, an open source developer at entire.io, the title sponsor of his open source work.

If hk speeds up your pre-commit loop or makes linting less painful, please consider becoming an individual or company sponsor. Your support funds ongoing development and helps keep hk fast, free, and independent.

Don't miss a new hk release

NewReleases is sending notifications on new releases.