Search code examples
gogo-modules

Behaviour of "go mod tidy" vs "go build"


Suppose I have a project with the following structure:

+-- app/
+-- otherstuff/
+-- test/
+-- go.mod
+-- go.sum
+-- main.go

I can make sure go.mod does not contain unused dependencies by running go mod tidy:

# 1) Verify that no dependency containing the name "modern-go" is found on go.mod
$ grep 'modern-go' go.mod
<-- Empty (nothing found) (OK)


# 2) Run "go mod tidy", verify that nothing changes (i.e. go.mod is already clean)
$ go mod tidy -v
unused github.com/modern-go/concurrent
unused github.com/modern-go/reflect2
<-- messages above are displayed, but go.mod did not change


# 3) Verify that go.mod did not change
$ grep 'modern-go' go.mod
<-- Empty (nothing found) (OK)

Now, if I run go build, go.mod gets updated:

# 4) Run "go build"
$ go build


# 5) go.mod was updated by "go build":
$ grep 'modern-go' go.mod
    github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
    github.com/modern-go/reflect2 v1.0.1 // indirect

I can't figure out what's going on here. I expected go mod tidy would leave go.mod on a clean state, and that running go build would therefore not change it.

Any ideas?


Solution

  • The behavior you’re expecting is usually true.

    Go 1.13 has some fixes in this area. I would suggest trying with that:

    https://twitter.com/golang/status/1164276194524762113