From 23bb64b66b3370a0d6a4e0e5a571b36e11a30564 Mon Sep 17 00:00:00 2001 From: Klaus Hartl Date: Tue, 15 Aug 2023 09:19:28 +0200 Subject: [PATCH] Fix running golangci-lint in different packages 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. https://github.com/golangci/golangci-lint/issues/1574#issuecomment-804500358 https://github.com/golangci/golangci-lint/issues/2912 --- go.yaml | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/go.yaml b/go.yaml index 7788ce1..893e35c 100644 --- a/go.yaml +++ b/go.yaml @@ -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: