-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Description
What operating system and processor architecture are you using (go env)?
go env Output
$ go env not applicable
What did you do?
I was working with ruleguard, which recommends to use the ignore tag to exclude rules form the build.
Quoting from https://github.com/quasilyte/go-ruleguard/blob/master/README.md?plain=1#L72
// +build ignore
package gorules
import "github.com/quasilyte/go-ruleguard/dsl"
...
ruleguard fails to run because the package github.com/quasilyte/go-ruleguard/dsl is not listed in the main module's go.mod file and because of the way it works, it needs to have this package available.
After running:
go get github.com/quasilyte/go-ruleguard/dsl
ruleguard works.
But after running go mod tidy, ruleguard fails again.
Going over go.mod, the package is no longer listed.
What did you expect to see?
The package github.com/quasilyte/go-ruleguard/dsl is still present in go.mod after running go mod tidy.
What did you see instead?
The package github.com/quasilyte/go-ruleguard/dsl is gone from go.mod after running go mod tidy.
It turns out the problem is that go mod tidy will handle the ignore tag in a different way, as mentioned in https://go.dev/ref/mod#go-mod-tidy, which is referenced from the output of go help mod tidy:
go mod tidy works by loading all of the packages in the main module and all of the packages they import, recursively. This includes packages imported by tests (including tests in other modules). go mod tidy acts as if all build tags are enabled, so it will consider platform-specific source files and files that require custom build tags, even if those source files wouldn’t normally be built. There is one exception: the ignore build tag is not enabled, so a file with the build constraint // +build ignore will not be considered. Note that go mod tidy will not consider packages in the main module in directories named testdata or with names that start with . or _ unless those packages are explicitly imported by other packages.
The detail about ignore is not minor, and it should be more readily available.