Skip to content

Commit

Permalink
Fix running golangci-lint in different packages
Browse files Browse the repository at this point in the history
The built-in typecheck cannot run on single files and will cause a
compilation error as soon as we attempt to lint a file that requires a
file from the same package. As a best-effort workaround now linting
the entire package a file has been changed in in the pre-commit hook.

golangci/golangci-lint#1574 (comment)
golangci/golangci-lint#2912
  • Loading branch information
carhartl committed Aug 15, 2023
1 parent 11dcce3 commit 23bb64b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions go.yaml
Expand Up @@ -7,10 +7,15 @@ pre-commit:
golangci-lint:
tags: lint go
glob: "*.go"
# `golangci-lint run` requires all files to be in the same package
# when passing it named files, thus in order to only lint staged files
# in the pre-commit hook linting them one by one...
run: for file in {staged_files}; do golangci-lint run $file; done
# Cannot easily run `golangci-lint run` on single files as imported types from the
# same package will appear as undefined and cause a typecheck compilation error.
# https://github.com/golangci/golangci-lint/issues/1574#issuecomment-804500358
# https://github.com/golangci/golangci-lint/issues/2912
#
# Thus linting the entire package of a staged files..
run: |
packages=$(echo {staged_files} | xargs dirname | uniq)
for package in $packages; do golangci-lint run "$package"; done
pre-push:
parallel: true
gotest:
Expand Down

0 comments on commit 23bb64b

Please sign in to comment.