golang/go

`cmd/go`: missing diagnostic when the default `GOPATH` conflicts with an explicit `GOROOT`

Open

#60,492 opened on May 29, 2023

View on GitHub
 (6 comments) (1 reaction) (0 assignees)Go (19,008 forks)batch import
GoCommandNeedsFixhelp wanted

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

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

Does this issue reproduce with the latest release?

Yes.

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

What did you do?

It used to be possible to have go run inside //go:generate lines. It is very handy since a project can require the specific generator version via go.mod.

Following examples by cilium/ebpf, I created a main.go file

package main

import (
	_ "github.com/cilium/ebpf"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go

...and invoked go generate ./....

(I'm using a specific generator in my bug report, but it looks like a generic issue affecting anyone using go run inside go:generate technique. Therefore I believe that the specific generator being used is irrelevant.)

What did you expect to see?

The generator to complain about missing arguments.

What did you see instead?

$ go generate ./...
go: module cache not found: neither GOMODCACHE nor GOPATH is set
cmd/test/main.go:10: running "env": exit status 1

Further info

If I invoke go run directly, everything works as expected.

$ go run github.com/cilium/ebpf/cmd/bpf2go
Error: missing package, are you running via go generate?
exit status 1

$ GOPACKAGE=main go run github.com/cilium/ebpf/cmd/bpf2go
Error: expected at least two arguments
exit status 1

It's clear that the generator gets executed and complains as expected.

However, if I set GOROOT (as go generate does):

$ GOROOT=/home/nickz/go GOPACKAGE=main go run github.com/cilium/ebpf/cmd/bpf2go
go: module cache not found: neither GOMODCACHE nor GOPATH is set

Changing go:generate line as below also "fixes" the problem:

//go:generate env --unset=GOROOT go run github.com/cilium/ebpf/cmd/bpf2go
$ go generate ./...
Error: expected at least two arguments
exit status 1

I believe that I have the most boring go setup possible. $ env | grep GO yields nothing.

Contributor guide