Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: go mod tidy removes entries from go.sum needed to execute "go run foo/bar" where "foo" is a required module #45552

Closed
AlexRouSg opened this issue Apr 13, 2021 · 5 comments
Labels
FrozenDueToAge GoCommand cmd/go modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@AlexRouSg
Copy link
Contributor

AlexRouSg commented Apr 13, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.3 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=${HOME}\AppData\Local\go-build
set GOENV=${HOME}\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=${HOME}\go\pkg\mod
set GONOPROXY=github.com/AlexRouSg
set GONOSUMDB=github.com/AlexRouSg
set GOOS=windows
set GOPATH=${HOME}\go
set GOPRIVATE=github.com/AlexRouSg
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=${HOME}\go-workspace\src\examples\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\alexr\AppData\Local\Temp\go-build2819037846=/tmp/go-build -gno-record-gcc-switches

What did you do?

I have a module A that depends on module B and uses packages from module B.
However module B also has a command line tool, lets call it B/cmd/tool.
When trying to execute go run B/cmd/tool while within module A,
for some reason it errors about a missing go.sum entry.
Doing a go get B/cmd/tool adds said entry, however go mod tidy will remove it.

Reproduction instructions:
clone https://github.com/goarrg/examples at hash 1619449c9504a019bbf8299de082bd21bfade42a
go mod tidy -> removes entry from go.sum
go run goarrg.com/cmd/goarrg -> output at bottom of issue

What did you expect to see?

No change that would result in me not being able to do a go run ...

What did you see instead?

Entry in go.sum deleted which results in go run ... erroring about a missing entry.

..\..\..\go\pkg\mod\goarrg.com@v0.0.0-20210413213047-c476baffe378\cmd\goarrg\internal\remote\remote.go:26:2: missing go.sum entry for module providing package golang.org/x/crypto/openpgp (imported by goarrg.com/cmd/goarrg/internal/remote); to add:
        go get goarrg.com/cmd/goarrg/internal/remote@v0.0.0-20210413213047-c476baffe378
@AlexRouSg AlexRouSg changed the title cmd/go: go mod tidy removes entries from go.sum needed to execute "go run foo/bar" where cmd/go: go mod tidy removes entries from go.sum needed to execute "go run foo/bar" where "foo" is a required module Apr 13, 2021
@AlexRouSg AlexRouSg reopened this Apr 13, 2021
@mknyszek mknyszek added this to the Backlog milestone Apr 13, 2021
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 13, 2021
@mknyszek
Copy link
Contributor

@seankhliao
Copy link
Member

go mod tidy removes the module content hash (but keeps the go.mod hash) because it isn't needed to build anything in the main module?

4d3
< golang.org/x/crypto v0.0.0-20201217014255-9d1352758620 h1:3wPMTskHO3+O6jqTEXyFcsnuxMQOqYSaHsDxcbUXpqA=

@AlexRouSg
Copy link
Contributor Author

@seankhliao thanks, that triggered a memory of something long ago saying to put a import _ "B/cmd/tool" to force go.mod and go.sum to have the required entries. And that apparantly works.

I guess we could change the issue to now be about changing the error message to suggest doing so.

@jayconrod
Copy link
Contributor

It sounds like go mod tidy is removing a hash for the content of a module because it's not needed to build any packages in the main module. That seems correct.

I don't think the go run error message should be changed though. It can't really know why the sum is missing; it's possible the import was added to a source file after go mod tidy or go get was run.

I'll close this issue as working as intended, but I definitely agree this is not working well, and something should change to make this workflow easier. For now, the tools.go idiom is the main workaround for this problem.

@AlexRouSg
Copy link
Contributor Author

I don't think the go run error message should be changed though. It can't really know why the sum is missing; it's possible the import was added to a source file after go mod tidy or go get was run.

Can we make go run/install/build check if the target is a external package main in module mode and if so then check if any source file has a import _ "tool" and error before trying to actually build?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge GoCommand cmd/go modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants