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: generate panics when go files provided as arguments and modules are on #29097

Closed
SamWhited opened this Issue Dec 4, 2018 · 6 comments

Comments

Projects
None yet
5 participants
@SamWhited
Member

SamWhited commented Dec 4, 2018

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

(also happens on a non-dirty tree):

go version devel +c2412a7681d5 20181204001955 linux/amd64

Does this issue reproduce with the latest release?

No, 1.11.2 works as expected.

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

go env Output
$ go env
GOARCH="amd64"
GOBIN="/home/sam/bin"
GOCACHE="/home/sam/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/sam/go"
GOPROXY=""
GORACE=""
GOROOT="/home/sam/Projects/go"
GOTMPDIR=""
GOTOOLDIR="/home/sam/Projects/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/sam/test/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build948663473=/tmp/go-build -gno-record-gcc-switches"

What did you do?

$ export GO111MODULE="on"
$ echo "package main" > test.go
$ go generate test.go
 TestmainGo:<nil> Asmflags:[] Gcflags:[] Ldflags:[] Gccgoflags:[]}}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x50 pc=0x89fea7]

goroutine 1 [running]:
cmd/go/internal/generate.runGenerate(0xe11a40, 0xc0000a8170, 0x1, 0x1)
        /home/sam/Projects/go/src/cmd/go/internal/generate/generate.go:167 +0x117
main.main()
        /home/sam/Projects/go/src/cmd/go/main.go:219 +0x82d

What did you expect to see?

Nothing happens because the file has no generate comment.

What did you see instead?

pkg.Module is nil, so Go generate panics (cmd/go/internal/generate/generate.go:167)

@agnivade agnivade changed the title from Go generate panics when go files provided as arguments and modules are on to cmd/go: generate panics when go files provided as arguments and modules are on Dec 5, 2018

@agnivade agnivade added this to the Go1.12 milestone Dec 5, 2018

@agnivade

This comment has been minimized.

Member

agnivade commented Dec 5, 2018

/cc @bcmills

Tentatively marking as release blocker.

@agnivade agnivade added the GoCommand label Dec 5, 2018

@jayconrod jayconrod self-assigned this Dec 5, 2018

@jayconrod

This comment has been minimized.

Contributor

jayconrod commented Dec 7, 2018

I looked into this today. I think this started with CL 148517, which added support for "go get" outside of a module. Several commands including fix, generate, list, run, build either panic or print a stack (via modload.die) when run outside of a module.

I have a CL mostly ready that fixes this. However, it's not clear to me that we want to allow all of these commands to run outside of modules. These commands could all do reasonable things (e.g., go generate test.go, or go run hello.go), but I'm worried about introducing a lot of corner cases without adding much value.

Will discuss with @bcmills tomorrow.

@SamWhited

This comment has been minimized.

Member

SamWhited commented Dec 7, 2018

when run outside of a module.

I have a CL mostly ready that fixes this. However, it's not clear to me that we want to allow all of these commands to run outside of modules

Note that this also happens within modules. If you add a go.mod file to the example in the original post, go generate test.go will still panic. It's trying to load only test.go and considering that not a module; but it's still part of a module.

@bcmills

This comment has been minimized.

Member

bcmills commented Dec 7, 2018

@jayconrod, in general any command should work as long as it (a) does not exist solely for editing the go.mod file and (b) does not need the name of the main module to resolve an import path.

@gopherbot

This comment has been minimized.

gopherbot commented Dec 7, 2018

Change https://golang.org/cl/153158 mentions this issue: cmd/go: fix errors for commands run outside of modules

@gopherbot

This comment has been minimized.

gopherbot commented Dec 7, 2018

Change https://golang.org/cl/153159 mentions this issue: cmd/go/internal/modload: add missing build constraint in testgo.go

gopherbot pushed a commit that referenced this issue Dec 7, 2018

cmd/go/internal/modload: add missing build constraint in testgo.go
That file is supposed to make unexpected dependencies on the main
module easier to diagnose in 'go test cmd/go', but I accidentally left
off the build constraint, so it was triggering outside of the test.

Updates #29097

Change-Id: I1cde3fe6c1d80add37c98a8c95ce48524ea05024
Reviewed-on: https://go-review.googlesource.com/c/153159
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>

@gopherbot gopherbot closed this in 89df035 Dec 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment