Skip to content

Feature/precommit hooks - #517

Open
danielschlegel wants to merge 2 commits into
mainfrom
feature/precommit-hooks
Open

Feature/precommit hooks#517
danielschlegel wants to merge 2 commits into
mainfrom
feature/precommit-hooks

Conversation

@danielschlegel

@danielschlegel danielschlegel commented May 27, 2026

Copy link
Copy Markdown
Collaborator

Add prek pre-commit hooks for lint auto-fix

What

Introduces prek as a Git pre-commit hook manager to automatically fix Go linting and formatting issues before every commit.

Changes

prek.toml (new) — declares two hooks that run on staged .go files:

  • gofumpt — formats code in-place using the same formatter enabled in .golangci.yaml.
  • golangci-lint --fix — applies all auto-fixable lint corrections. Runs against full packages (pass_filenames = false) so type-checking is correct.

.mise.toml — adds three new tools so mise install sets up the full dev environment in one step:

  • prek 0.4.3 — the hook manager itself.
  • golangci-lint v2.11.4 (via go: backend) — pinned to match the version in Earthfile.
  • gofumpt v0.10.0 (via go: backend) — pinned to the current latest release.

Both tool versions carry # renovate: comments so Renovate will keep them in sync automatically.

CONTRIBUTING.md — adds a Pre-commit hooks section documenting the two-command setup:

mise install   # installs all tools
prek install   # wires up .git/hooks/pre-commit

How it works

On git commit, prek runs the hooks against staged .go files only (non-Go commits are unaffected). If a hook modifies a file, the commit is blocked so you can review the diff, re-stage, and commit again. To pre-apply all fixes before staging: prek run --all-files.

@danielschlegel danielschlegel self-assigned this May 27, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces pre-commit hooks using prek, updates various tool versions in .mise.toml, migrates repository references from the earthly organization to EarthBuild/earthbuild, and upgrades several Go dependencies in go.mod. The reviewer identified three critical issues: invalid module paths and non-existent versions for both golangci-lint and gofumpt in .mise.toml which will cause installation failures, and a mismatched variable name (BUILDKIT_BRANCH instead of BUILDKIT_GIT_BRANCH) in buildkitd/Earthfile that results in empty branch names in logs.

Comment thread .mise.toml Outdated
Comment thread buildkitd/Earthfile Outdated
@danielschlegel

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

Comment thread prek.toml Outdated
@gilescope

Copy link
Copy Markdown

Are these several things going on here? Is this the minimal changes for the precommit? Could we get these split out into smaller PRs please?

@danielschlegel
danielschlegel force-pushed the feature/precommit-hooks branch from a9634ba to 9ef9821 Compare June 4, 2026 11:36
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

⚠️ Are we earthbuild yet?

Warning: "earthly" occurrences have increased by 6 (0.13%)

📈 Overall Progress

Branch Total Count
main 4572
This PR 4578
Difference +6 (0.13%)

📁 Changes by file type:

File Type Change
Go files (.go) ➖ No change
Documentation (.md) ➖ No change
Earthfiles ❌ +2

Keep up the great work migrating from Earthly to Earthbuild! 🚀

💡 Tips for finding more occurrences

Run locally to see detailed breakdown:

./.github/scripts/count-earthly.sh

Note that the goal is not to reach 0.
There is anticipated to be at least some occurences of earthly in the source code due to backwards compatibility with config files and language constructs.

@gilescope gilescope left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good but let's eat our own dogfood and use earth to run the commands for reproducibility. If earth is slow used in this fashion we'll have good reason to speed it up.

@danielschlegel
danielschlegel force-pushed the feature/precommit-hooks branch from fd1e1cb to d371b5b Compare July 16, 2026 14:41
@danielschlegel
danielschlegel marked this pull request as ready for review August 5, 2026 15:54
@danielschlegel
danielschlegel requested a review from a team as a code owner August 5, 2026 15:54
@danielschlegel
danielschlegel requested review from janishorsts and a lite review from Copilot and removed request for a team August 5, 2026 15:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a prek-based pre-commit workflow and updates the Earthly lint/format targets to support automated formatting and linting, alongside repo-wide formatting and shellcheck-driven fixes across Go and shell test scripts.

Changes:

  • Adds prek.toml to define local pre-commit hooks (Go formatting/linting and shellcheck).
  • Refactors Earthfile lint targets by introducing +lint-deps and adding a containerized +lint-fix, plus a repo-wide +lint-scripts implementation and a +fmt-go --files mode.
  • Applies gofmt/gofumpt and shellcheck-driven cleanups (imports, spacing, quoting, safer shell patterns) across multiple files.

Reviewed changes

Copilot reviewed 53 out of 56 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
util/proj/helheim_mocks_test.go gofmt/gofumpt formatting adjustments in mocks test
util/llbutil/authprovider/helheim_mocks_test.go gofmt/gofumpt formatting adjustments in mocks test
util/gitutil/detectgit.go gofmt formatting (function call wrapping)
util/flagutil/remote_cache.go gofmt formatting (error formatting call wrapping)
util/dockerutil/docker.go gofmt formatting (printf argument wrapping)
tests/with-docker/test-cgroup-v2.sh Adds shellcheck directive for externally-provided var
tests/wait-block/save-artifact/test.sh Fixes quoting of forwarded args ("$@")
tests/wait-block/save-artifact-multi-ref/test.sh Fixes quoting of forwarded args ("$@")
tests/wait-block/run-push/test.sh Improves quoting and adds shellcheck directive for sh -c positional args
tests/wait-block/common/test.sh Fixes quoting for $@, temp file paths, and rm
tests/try-catch/try-finally-two-files/test.sh Adjusts args quoting; modifies expected-failure flow
tests/try-catch/try-finally-pass/test.sh Fixes quoting of forwarded args ("$@")
tests/try-catch/try-finally-if-exists/test.sh Rewrites negated commands to explicit failure checks
tests/try-catch/try-finally-fail/test.sh Fixes quoting of forwarded args ("$@")
tests/try-catch/try-catch-not-currently-implemented/test.sh Rewrites negated pipeline to explicit failure checks
tests/try-catch/test.sh Safer printf usage and minor cleanup
tests/try-catch/docker-try-finally-fail/test.sh Fixes quoting for $@ and $frontend
tests/remote-cache/test.sh Fixes quoting and export pattern for REGISTRY_IP
tests/remote-buildkit/remote-buildkit-test.sh Improves quoting and replaces ls *.pem counting with find
tests/registry-certs/test.sh Fixes quoting around realpath/dirname
tests/oidc/test-aws.sh Switches to bash and adds shellcheck directive for externally-provided var
tests/bootstrap/test-bootstrap.sh Fixes quoting for $HOME paths
states/visited-legacy.go gofmt formatting (error call wrapping)
states/states.go gofmt formatting (struct literal wrapping)
release/common-repo/assume-developer-role.sh Quotes substitutions/vars for safer CLI invocation
release/apt-repo/generate-release.sh Replaces for f in $(find ...) with safer `find
prek.toml Adds prek hook definitions for gofumpt, golangci-lint, lint-fix (manual), and shellcheck
logstream/manifest.pb.go gofmt formatting in generated protobuf code
logstream/delta.pb.go gofmt formatting in generated protobuf code
logbus/solvermon/solvermon.go gofmt formatting (call wrapping)
examples/readme/proto/pb/api.pb.go gofmt formatting in generated example protobuf code
examples/integration-test/src/smoketest/smoketest.sh Renames variable and updates assertion call
examples/integration-test/src/smoketest/assert.sh Adjusts parameter expansion patterns to handle quoting better
earthfile2llb/with_docker_run_tar.go gofmt formatting (call wrapping)
earthfile2llb/with_docker_run_reg.go gofmt formatting (call wrapping)
earthfile2llb/with_docker_run_local_tar.go gofmt formatting (call wrapping)
earthfile2llb/with_docker_run_local_reg.go gofmt formatting (call wrapping)
earthfile2llb/with_docker_run_base.go gofmt formatting (call wrapping)
earthfile2llb/interpretererror.go gofmt formatting (fmt.Sprintf arg wrapping)
Earthfile Adds lint-deps/lint-fix, refactors lint-scripts, and adds fmt-go --files support
domain/reference.go gofmt formatting (error call wrapping)
docker2earth/convert_test.go gofmt formatting (call wrapping)
cmd/earthly/subcmd/prune_cmds.go gofmt formatting (call wrapping)
cmd/earthly/subcmd/ls_cmds.go gofmt formatting (call wrapping)
cmd/earthly/subcmd/doc_cmds.go gofmt formatting (error call wrapping)
cmd/earthly/subcmd/debug_cmds.go gofmt formatting (printf arg wrapping)
cmd/earthly/base/buildkit.go gofmt formatting (call wrapping)
cmd/earthly/app/run.go gofmt formatting (Warn call wrapping)
cmd/earthly/app/create.go gofmt formatting (setter call wrapping)
builder/image_solver.go gofmt formatting (Sprintf arg wrapping)
buildcontext/parsefeatures.go gofmt formatting (Printf arg wrapping)
buildcontext/git.go gofmt formatting (call wrapping)
.mise.toml Adds tool pins for prek and gofumpt
.github/scripts/test-earthly-count.sh Quotes numeric test operand
.github/scripts/count-earthly.sh Shellcheck-friendly variable handling and safer $GITHUB_OUTPUT writes
.claude/settings.local.json Expands allowed command patterns for local Claude tooling
Files not reviewed (3)
  • examples/readme/proto/pb/api.pb.go: Generated file
  • logstream/delta.pb.go: Generated file
  • logstream/manifest.pb.go: Generated file

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 28 to 30
set +e
"$earthly" $@ +test --fail=yesplease
exit_code="$?"
"$earthly" "$@" +test --fail=yesplease

Comment thread Earthfile Outdated
Comment on lines +183 to +185
LOCALLY
RUN gofumpt -w .
ARG files="."
RUN gofumpt -w ${files}
Comment thread prek.toml
Comment on lines +19 to +42
# Run golangci-lint to catch lint issues on every commit.
# Runs via `earth +lint` so the pinned version and install logic live in one
# place (the Earthfile) and lint runs in the same containerized environment
# as CI, rather than depending on a locally installed golangci-lint.
# pass_filenames = false so golangci-lint analyses full packages rather than
# individual files, which is required for correct type-checking.
[[repos.hooks]]
id = "golangci-lint"
name = "golangci-lint"
language = "system"
entry = "earth +lint"
types = ["go"]
pass_filenames = false

# Run golangci-lint --fix to auto-correct fixable lint issues.
# Runs manually only: prek run --hook-stage manual golangci-lint-fix
[[repos.hooks]]
id = "golangci-lint-fix"
name = "golangci-lint (auto-fix)"
language = "system"
entry = "earth +lint-fix"
types = ["go"]
pass_filenames = false
stages = ["manual"]
Comment thread .mise.toml Outdated
Comment on lines +1 to +6
[tools]
go = "1.26.0"
# renovate: datasource=github-releases packageName=j178/prek
"aqua:j178/prek" = "0.4.9"
# renovate: datasource=go packageName=mvdan.cc/gofumpt
"go:mvdan.cc/gofumpt" = "0.10.0"
Add prek.toml with hooks for Go formatting (gofumpt, goimports,
golines via `earth +fmt-go`), golangci-lint, and shellcheck, so all
tools run through earth without requiring local installs.

Refactor the Earthfile lint targets: extract +golangci-lint-install
so the pinned version lives in one place, split +lint into
+lint-deps / +lint, and rework +fmt-go to run golangci-lint's
configured formatters in a container with a --files ARG for scoping
to staged files. Enable goimports (EarthBuild local-prefixes) and
golines (max-len 120) as golangci-lint formatters.
Apply the formatting and lint fixes required for a clean
`prek run --all-files`:

- Fix all shellcheck findings across tests/, release/, examples/,
  and .github/scripts/ (SC2068 unquoted $@, SC2251 ! under set -e,
  SC2295 glob leakage, SC2086/SC2046 unquoted expansions, SC2044
  for-over-find, SC3040 pipefail under /bin/sh, SC2059 printf
  injection, and others).
- Apply gofumpt, goimports, and golines formatting to Go sources.
Copilot AI review requested due to automatic review settings August 5, 2026 16:09
@danielschlegel
danielschlegel force-pushed the feature/precommit-hooks branch from d371b5b to 0675c8c Compare August 5, 2026 16:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 31 out of 34 changed files in this pull request and generated no new comments.

Files not reviewed (3)
  • examples/readme/proto/pb/api.pb.go: Generated file
  • logstream/delta.pb.go: Generated file
  • logstream/manifest.pb.go: Generated file
Suppressed comments (7)

tests/try-catch/try-finally-two-files/test.sh:30

  • set +e is enabled before running the expected-failure command, but it’s never turned back on and the script doesn’t assert that the command actually failed. This can make the test pass even if the command succeeds, and can also mask failures in the subsequent test assertions (since -e stays disabled). Capture the exit code, re-enable set -e, and assert non-zero before continuing.
set +e
"$earthly" "$@" +test --fail=yesplease

prek.toml:43

  • The PR description says golangci-lint auto-fix runs before every commit, but this hook is configured with stages = ["manual"], so it won’t run on the default pre-commit stage. Either update the PR description to match the behavior, or adjust the hook stages so auto-fix runs on commit as intended.
# Run golangci-lint --fix to auto-correct fixable lint issues.
# Runs manually only: prek run --hook-stage manual golangci-lint-fix
[[repos.hooks]]
id = "golangci-lint-fix"
name = "golangci-lint (auto-fix)"
language = "system"
entry = "earth +lint-fix"
types = ["go"]
pass_filenames = false
stages = ["manual"]

.claude/settings.local.json:18

  • The allowlist additions Bash(gh run *) / Bash(gh pr *) are very broad and enable potentially mutating gh operations (commenting, editing, merging, etc.). Prefer enumerating the specific read-only subcommands needed (e.g., gh run view/list and gh pr diff/checks) rather than wildcarding all gh run/gh pr commands.
      "Bash(gh run *)",
      "Bash(gh pr *)",

Earthfile:193

  • This new comment uses earthly +fmt-go ..., but project guidance is to use earth as the CLI/build tool name in new docs/comments. Update the example command accordingly.
# instead of the whole repo, e.g. for use from a pre-commit hook:
# earthly +fmt-go --files="a.go b.go"
fmt-go:

.mise.toml:4

  • The PR description says .mise.toml adds multiple tools (prek, golangci-lint, gofumpt), but this change only pins prek. If the intent is to rely on earth targets for lint/format (no local golangci-lint/gofumpt), update the PR description accordingly; otherwise add the missing tool pins here.
[tools]
go = "1.26.0"
# renovate: datasource=github-releases packageName=j178/prek
"aqua:j178/prek" = "0.4.9"

Earthfile:175

  • This comment says lint runs against the “earthly” project, but the project is being renamed to EarthBuild; new comments/docs should avoid the old name unless it’s a literal identifier. Consider updating this wording to “earthbuild”/“EarthBuild”.
# lint runs basic go linters against the earthly project.
lint:
    FROM +lint-deps

prek.toml:17

  • The go-fmt hook uses $*, which collapses argument boundaries and can break when staged filenames contain spaces (or other special characters). Use a shell-escaped join of "$@" so the file list survives being passed through --files and re-expanded inside the Earthfile.
entry = "bash -c 'earth +fmt-go --files=\"$*\"' --"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants