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: -mod=mod no longer writes go.sum in 1.18 #51818

Closed
matthewmueller opened this issue Mar 19, 2022 · 4 comments
Closed

cmd/go: -mod=mod no longer writes go.sum in 1.18 #51818

matthewmueller opened this issue Mar 19, 2022 · 4 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@matthewmueller
Copy link

matthewmueller commented Mar 19, 2022

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

$ go version
go version go1.18 darwin/amd64

Does this issue reproduce with the latest release?

Yep

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/m/Library/Caches/go-build"
GOENV="/Users/m/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/m/dev/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/m/dev"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/m/dev/src/github.com/go-duo/bud/pkg/di/_tmp/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4f/tcxcr6_55v9bp38d8g4hjlf80000gn/T/go-build1965598951=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I've been relying on go run -mod=mod main.go in test scripts to download any missing dependencies and create a go.sum file.

For the following go.mod

module app.com

require (
  github.com/hexops/valast v1.4.1
)

After upgrading and running go run -mod=mod main.go, I get the following:

        err: exit status 1: stderr: go: github.com/hexops/valast@v1.4.1: missing go.sum entry; to add it:
                go mod download github.com/hexops/valast
        go: github.com/hexops/valast@v1.4.1: missing go.sum entry; to add it:
                go mod download github.com/hexops/valast

I read in the release notes that go.sum is no longer written automatically for certain go mod <command>, but I assumed it would have mentioned something about the -mod=mod behavior changing.

Interestingly, you get a less wrapped error when you just run go run main.go:

go: github.com/hexops/valast@v1.4.1: missing go.sum entry; to add it:
        go mod download github.com/hexops/valast
go: github.com/hexops/valast@v1.4.1: missing go.sum entry; to add it:
        go mod download github.com/hexops/valast

I also downgraded to 1.17 to confirm that this change was introduced in 1.18.

What did you expect to see?

I expected go run -mod=mod main.go to continue to download any missing dependencies and create a go.sum since you're explicitly opting into updating go.mod:

I also think that behavior would align with what the Module Reference says:

-mod=mod tells the go command to ignore the vendor directory and to automatically update go.mod

Otherwise, I'm not really sure what the use case of -mod=mod is anymore if it only does half of the job :)

What did you see instead?

        err: exit status 1: stderr: go: github.com/hexops/valast@v1.4.1: missing go.sum entry; to add it:
                go mod download github.com/hexops/valast
        go: github.com/hexops/valast@v1.4.1: missing go.sum entry; to add it:
                go mod download github.com/hexops/valast
@seankhliao
Copy link
Member

I can't reproduce this, and the output you've given doesn't look like it corresponds to go run main.go.
What did you actually do?

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 19, 2022
@matthewmueller
Copy link
Author

matthewmueller commented Mar 19, 2022

Hey @seankhliao thanks for the prompt response! Based on your response, I dug deeper.

Here's how to repro it.

.
├── go.mod
├── main.go
└── web
    └── web.go

main.go

package main

import (
	"fmt"

	"app.com/web"
	"github.com/hexops/valast"
)

func main() {
	actual := web.Load()
	fmt.Println(valast.String(actual))
}

go.mod

module app.com

require (
  github.com/hexops/valast v1.4.1
)

web/web.go

package web

func Load() *Web {
	return &Web{}
}

type Web struct{}

Then run go run -mod=mod main.go on go 1.18 and you should see:

err: exit status 1: stderr: go: github.com/hexops/valast@v1.4.1: missing go.sum entry; to add it:
        go mod download github.com/hexops/valast
go: github.com/hexops/valast@v1.4.1: missing go.sum entry; to add it:
        go mod download github.com/hexops/valast

Okay, so I think I know what's happening. github.com/hexops/valast depends on golang.org/x/tools/go/packages, which somewhere spawns out.

By changing the go.mod to use replace

module app.com

require (
  github.com/hexops/valast v1.4.1
)

replace github.com/hexops/valast => $HOME/dev/src/github.com/hexops/valast

And running go run -mod=mod main.go again should give you a more descriptive error:

go [-e -json -compiled=false -test=false -export=false -deps=false -find=true -- app.com/web]: exit status 1: go: github.com/hexops/valast@v1.4.1 requires
        github.com/hexops/autogold@v0.8.1: missing go.sum entry; to add it:
        go mod download github.com/hexops/autogold
go: github.com/hexops/valast@v1.4.1 requires
        github.com/hexops/autogold@v0.8.1: missing go.sum entry; to add it:
        go mod download github.com/hexops/autogold

You can find where this error occurs in golang.org/x/tools/go/packages: https://github.com/golang/tools/blob/5ea13d0d89f92d5ca5468e282dd4ba2ad7503564/go/packages/golist.go#L808-L824

Not sure why, but I was able to fix this by adding an explicit go version to go.mod

module app.com

+ go 1.18

require (
  github.com/hexops/valast v1.4.1
)

So the good news is that it seems like this continues to work as expected, but perhaps something changed or needs to be updated in golang.org/x/tools/go/packages.

Thanks again for the quick response!

@seankhliao
Copy link
Member

This was an intentional change as part of CL 339170.
Listing *.go files directly is special, using package/directory paths works.

@matthewmueller
Copy link
Author

Just curious, does golang.org/x/tools/go/packages need to be updated then?

@golang golang locked and limited conversation to collaborators Mar 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants