Skip to content

go/build: Default.Import fails for standard-library packages when built with -trimpath #51483

@bcmills

Description

@bcmills

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

$ go version
go version devel go1.19-81767e23c Fri Mar 4 01:18:57 2022 +0000 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
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/usr/local/google/home/bcmills/.cache/go-build"
GOENV="/usr/local/google/home/bcmills/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/tmp/tmp.LHtt2MlVH3/.gopath/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/tmp/tmp.LHtt2MlVH3/.gopath"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/google/home/bcmills/sdk/gotip"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/google/home/bcmills/sdk/gotip/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel go1.19-81767e23c Fri Mar 4 01:18:57 2022 +0000"
GCCGO="/usr/local/google/home/bcmills/bin/gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="c++"
CGO_ENABLED="1"
GOMOD="/tmp/tmp.LHtt2MlVH3/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 -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2171881965=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Execute go run for the following program, with or without -trimpath.

-- go.mod --
module example

go 1.19
-- main.go --
package main

import (
	"go/build"
	"log"
	"os"
)

func main() {
	p, err := build.Default.Import("runtime", "", build.FindOnly)
	if err != nil {
		log.Fatal(err)
	}
	os.Stdout.WriteString(p.Dir)
}

What did you expect to see?

Successful output in both cases: go/build already falls back to go list in some cases, and go list knows where to find its own GOROOT.

What did you see instead?

$ go run .
/usr/local/google/home/bcmills/sdk/gotip/src/runtime
$ go run -trimpath .
2022/03/04 13:01:41 go/build: go list runtime: exit status 2
go: cannot find GOROOT directory: go

exit status 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions