Feature/precommit hooks - #517
Conversation
There was a problem hiding this comment.
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.
|
@copilot resolve the merge conflicts in this pull request |
|
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? |
a9634ba to
9ef9821
Compare
|
| 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.shNote 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.
b26d605 to
9cb24c5
Compare
9cb24c5 to
a387cc9
Compare
gilescope
left a comment
There was a problem hiding this comment.
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.
fd1e1cb to
d371b5b
Compare
There was a problem hiding this comment.
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.tomlto define local pre-commit hooks (Go formatting/linting and shellcheck). - Refactors
Earthfilelint targets by introducing+lint-depsand adding a containerized+lint-fix, plus a repo-wide+lint-scriptsimplementation and a+fmt-go --filesmode. - 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.
| set +e | ||
| "$earthly" $@ +test --fail=yesplease | ||
| exit_code="$?" | ||
| "$earthly" "$@" +test --fail=yesplease | ||
|
|
| LOCALLY | ||
| RUN gofumpt -w . | ||
| ARG files="." | ||
| RUN gofumpt -w ${files} |
| # 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"] |
| [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.
d371b5b to
0675c8c
Compare
There was a problem hiding this comment.
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 +eis 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 subsequenttestassertions (since-estays disabled). Capture the exit code, re-enableset -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 mutatingghoperations (commenting, editing, merging, etc.). Prefer enumerating the specific read-only subcommands needed (e.g.,gh run view/listandgh pr diff/checks) rather than wildcarding allgh run/gh prcommands.
"Bash(gh run *)",
"Bash(gh pr *)",
Earthfile:193
- This new comment uses
earthly +fmt-go ..., but project guidance is to useearthas 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.tomladds multiple tools (prek, golangci-lint, gofumpt), but this change only pins prek. If the intent is to rely onearthtargets 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-fmthook 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--filesand re-expanded inside the Earthfile.
entry = "bash -c 'earth +fmt-go --files=\"$*\"' --"
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.gofiles:.golangci.yaml.--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 somise installsets up the full dev environment in one step:prek 0.4.3— the hook manager itself.golangci-lint v2.11.4(viago:backend) — pinned to match the version inEarthfile.gofumpt v0.10.0(viago: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:How it works
On
git commit, prek runs the hooks against staged.gofiles 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.