I have a large repo in the likes of github.com/myusername/myrepo
Now within that repo (for reasons beyond the scope of this question) I have initialised the following go
module: github.com/myusername/myrepo/eck-user-mgmt
Here is my github.com/myusername/myrepo/eck-user-mgmt/.pre-commit-config.yaml
---
files: ^eck-user-mgmt/
repos:
- repo: git://github.com/dnephin/pre-commit-golang
rev: v0.4.0
hooks:
- id: go-fmt
- id: go-imports
- id: go-unit-tests
- id: go-mod-tidy
- id: golangci-lint
However no go
files/modules/whatever is discovered
▶ g add .pre-commit-config.yaml && pre-commit run
go fmt...............................................(no files to check)Skipped
go imports...........................................(no files to check)Skipped
go-unit-tests........................................(no files to check)Skipped
go-mod-tidy..........................................(no files to check)Skipped
golangci-lint........................................(no files to check)Skipped
This is run from where pre-commit-config.yaml
resides, i.e. ~/work/myrepo/eck-user-mgmt
why is that?
p.s. I know this is not the best practice in terms of go
module management, I just want to know if there is a way with the make pre-commit
work with go
in the context of the specific setup
The go command generally runs in the context of a main module and its dependencies. It finds the main module by looking for a go.mod
file in the current working directory and its parent directories.
If your pre-commit is run in the root directory of the repository and there's no go.mod
file there, commands will be run outside any module. So go mod tidy
and go test ./...
won't do anything for example.
You'll likely need to run those commands within each module. You can locate directories containing go.mod
files in a shell script:
modfiles=($(find . -type f -name go.mod))
for f in "${modfiles[@]}"; do
pushd "$(dirname "$f")"
### presubmit commands
popd
done