Does it make sense to add `go mod vendor` to a pre-commit hook?

2.1k Views Asked by At

Setup:

  • Our project is using golang 1.12.14
  • We are using go build -mod=vendor

Issue: When new dependencies are added to go.mod the vendor folder isn't updated and people are committing code and forgetting to run go mod vendor to update the folder. My understanding is that since -mod=vendor specifies to use packages from the vendor folder, the go.mod file will have discrepancies from what we are actually using when building the project.

Question: Should go mod vendor be added to a pre-commit hook?

1

There are 1 best solutions below

0
bcmills On

As of Go 1.14, the go command automatically checks for consistency between the vendor directory and the go.mod file whenever the vendor directory is used. In addition, it uses the vendor directory by default if the module specifies go 1.14 or higher (see https://tip.golang.org/doc/go1.14#go-command).

As of today, the oldest supported version of the Go toolchain is Go 1.15.13.

So if you upgrade to a supported version of the Go toolchain, it should not be necessary to run go mod vendor as a pre-commit hook. The go command itself will flag inconsistencies whenever the vendor directory is used.