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

x/tools/go/packages: NeedExportFile causes error about invalid go.mod file to be dropped #67638

Open
dominikh opened this issue May 24, 2024 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@dominikh
Copy link
Member

Go version

go version devel go1.23-722d59436b Thu May 16 02:00:26 2024 +0000 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/dominikh/.cache/go-build'
GOENV='/home/dominikh/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/dominikh/prj/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/dominikh/prj'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/dominikh/prj/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/dominikh/prj/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='devel go1.23-722d59436b Thu May 16 02:00:26 2024 +0000'
GODEBUG=''
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/dominikh/bar/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2083983019=/tmp/go-build -gno-record-gcc-switches'

What did you do?

In the following archive, main.go uses go/packages to load ./... inside the bar directory. bar is its own Go module with an invalid go.mod file.

-- bar/bar.go --
package bar
-- bar/go.mod --
module example.com/bar

go invalid
-- go.mod --
module example.com

go 1.23

require golang.org/x/tools v0.21.1-0.20240524143747-e1b14a191503

require (
	golang.org/x/mod v0.17.0 // indirect
	golang.org/x/sync v0.7.0 // indirect
)
-- go.sum --
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.21.1-0.20240524143747-e1b14a191503 h1:940DNuWWMxP2YF92TFWOux6YSm9Oy12cgj2LNNwX6x0=
golang.org/x/tools v0.21.1-0.20240524143747-e1b14a191503/go.mod h1:bqv7PJ/TtlrzgJKhOAGdDUkUltQapRik/UEHubLVBWo=
-- main.go --
package main

import (
	"fmt"

	"golang.org/x/tools/go/packages"
)

func main() {
	cfg := &packages.Config{
		Mode: packages.NeedExportFile | packages.NeedFiles,
		// Mode: packages.NeedFiles,
		Dir: "./bar",
	}
	pkgs, err := packages.Load(cfg, "./...")
	fmt.Println(len(pkgs), err)
}

What did you see happen?

packages.Load returns no packages and no error.

What did you expect to see?

An error to be returned.

When removing NeedExportFile from the mode, we correctly get the following error:

err: exit status 1: stderr: go: errors parsing go.mod:
go.mod:3: invalid go version 'invalid': must match format 1.23.0
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label May 24, 2024
@gopherbot gopherbot added this to the Unreleased milestone May 24, 2024
@cagedmantis
Copy link
Contributor

cc @matloob

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 24, 2024
@adonovan
Copy link
Member

Thanks for the very helpful reproducible test case.

It appears that this logic in go/packages/golist.go (which is as old as go/packages itself and covered in my grubby fingerprints) is to blame:

func (state *golistState) invokeGo(verb string, args ...string) (*bytes.Buffer, error) {
...
		// Export mode entails a build.
		// If that build fails, errors appear on stderr
		// (despite the -e flag) and the Export field is blank.
		// Do not fail in that case.
		if !usesExportData(cfg) ....  {
			return nil, fmt.Errorf("go %v: %s: %s", args, exitErr, stderr)
		}

I think the comment must have become false during the introduction of modules: if the build fails, errors (in particular, go.mod errors) are not printed to stderr, they are returned in the err value, which should be returned so that the Load fails.

I'm not sure what the required fix is. I would like it to be as simple as replacing this with "if true", but perhaps that has other consequences. @matloob and @findleyr may know more.

@matloob
Copy link
Contributor

matloob commented Jun 3, 2024

I think we're good to remove the conditions and always return the error. The underlying issues have been fixed in Go 1.19+:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants