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

go/build: Passes bad $GOROOT to go list subprocess #35056

Closed
tv42 opened this issue Oct 21, 2019 · 8 comments
Closed

go/build: Passes bad $GOROOT to go list subprocess #35056

tv42 opened this issue Oct 21, 2019 · 8 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@tv42
Copy link

tv42 commented Oct 21, 2019

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

# this sdk came from `go get golang.org/dl/go1.13.3`,
# I'm fiddling with PATH to eliminate other installed Go
# versions to reproduce with a known good version,
# without parent/child version mismatch later.
$ PATH="$HOME/sdk/go1.13.3/bin:/bin:/usr/bin" go version
go version go1.13.3 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
PATH="$HOME/sdk/go1.13.3/bin:/bin:/usr/bin" go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/tv/.cache/go-build"
GOENV="/home/tv/.config/go/env"
GOEXE=""
GOFLAGS="-trimpath"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY="mntn.llc/go,*.mntn.llc,eagain.net/2019,eagain.net/tools"
GONOSUMDB="mntn.llc/go,*.mntn.llc,eagain.net/2019,eagain.net/tools"
GOOS="linux"
GOPATH="/home/tv/go"
GOPRIVATE="mntn.llc/go,*.mntn.llc,eagain.net/2019,eagain.net/tools"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/tv/sdk/go1.13.3"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/tv/sdk/go1.13.3/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/tv/z/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=/home/tv/tmp/go-build434657787=/tmp/go-build -gno-record-gcc-switches"

What did you do?

~$ mkdir z
~$ cd z
~/z$ go mod init example.com/m
go: creating new go.mod: module example.com/m
~/z$ cat >main.go
package main

import (
	"fmt"
	"go/build"
	"os"
	"runtime"
)

func main() {
	fmt.Printf("runtime.GOROOT=%v\n", runtime.GOROOT())
	_, err := build.Import("example.com/m", ".", build.ImportComment)
	if err != nil {
		fmt.Printf("error: %v", err)
		os.Exit(1)
	}
}
~/z$ PATH="$HOME/sdk/go1.13.3/bin:/bin:/usr/bin" go run .
runtime.GOROOT=go
error: go/build: importGo example.com/m: exit status 2
go: cannot find GOROOT directory: go


exit status 1

What did you expect to see?

Successful run, with ImportDir returning metadata about the package example.com/m that I just created.

What did you see instead?

The go list subprocess is spawned with a broken GOROOT=go in environment, an explicitly set $GOROOT prevents the child from deducing GOROOT from /proc/self/exe, and this causes it to fail.

Confirmation that $GOROOT is passed:

~/z$ PATH="$HOME/sdk/go1.13.3/bin:/bin:/usr/bin" sh -c 'go build && strace -f -o log -v -e execve ./m >/dev/null; grep execve log|tail -1'
1190329 execve("/home/tv/sdk/go1.13.3/bin/go", ["go", "list", "-compiler=gc", "-tags=", "-installsuffix=", "-f={{.Dir}}\n{{.ImportPath}}\n{{.R"..., "example.com/m"], [...skipped things..., "GOOS=linux", "GOARCH=amd64", "GOROOT=go", "GOPATH=/home/tv/go", "CGO_ENABLED=1"]) = 0
@tv42
Copy link
Author

tv42 commented Oct 21, 2019

It seems golang.org/dl/go1.13.3 contains GOROOT munging logic. But that doesn't help here unless that executable is in PATH as "go" (not "go1.13.3"). This bug could be "fixed" by documenting that nobody must ever call anything under ~/sdk directly -- but that leaves people who just unpack a tarball just as broken.

@tv42
Copy link
Author

tv42 commented Oct 21, 2019

This only triggers with modules, not with GOPATH (go/build doesn't spawn subprocesses in gopath mode).

Real world code that broke because of this: https://github.com/google/ko

@bcmills

This comment has been minimized.

@bcmills bcmills marked this as a duplicate of #34860 Oct 22, 2019
@bcmills bcmills closed this as completed Oct 22, 2019
@bcmills

This comment has been minimized.

@bcmills bcmills reopened this Oct 22, 2019
@bcmills
Copy link
Member

bcmills commented Oct 22, 2019

The fact that the

	fmt.Printf("runtime.GOROOT=%v\n", runtime.GOROOT())

line prints

runtime.GOROOT=go

suggests that this isn't actually coming from go/build at all: it's just passing on what the go command told it.

@bcmills
Copy link
Member

bcmills commented Oct 22, 2019

I'm unable to reproduce the reported behavior.

m$ go1.13.3 mod init example.com/m
go: creating new go.mod: module example.com/m

m$ cat >main.go
package main

import (
        "fmt"
        "go/build"
        "os"
        "runtime"
)

func main() {
        fmt.Printf("runtime.GOROOT=%v\n", runtime.GOROOT())
        _, err := build.Import("example.com/m", ".", build.ImportComment)
        if err != nil {
                fmt.Printf("error: %v", err)
                os.Exit(1)
        }
}

m$ PATH="$HOME/sdk/go1.13.3/bin:/bin:/usr/bin" go run .
runtime.GOROOT=/usr/local/google/home/bcmills/sdk/go1.13.3

m$ PATH="$HOME/sdk/go1.13.3/bin" go run .
runtime.GOROOT=/usr/local/google/home/bcmills/sdk/go1.13.3

m$

Is it possible that you have a GOROOT entry in /home/tv/.config/go/env, or an explicit GOROOT environment variable set?

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 22, 2019
@tv42
Copy link
Author

tv42 commented Oct 22, 2019

I have no GOROOT in environment, only GOFLAGS=-trimpath in ~/.config/go/env.

As far as I can understand, the problem comes from go/build using runtime.GOROOT()'s poor guess (internal/sys.DefaultGoroot as the fallback).

I'll try to make an effort to reproduce inside a reproducible container or something.

@bcmills bcmills added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Feb 11, 2020
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Mar 11, 2021
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