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: Default.Import fails for standard-library packages when built with -trimpath #51483

Closed
bcmills opened this issue Mar 4, 2022 · 8 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@bcmills
Copy link
Member

bcmills commented Mar 4, 2022

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
@bcmills bcmills self-assigned this Mar 4, 2022
@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 4, 2022
@bcmills bcmills added this to the Go1.19 milestone Mar 4, 2022
@rittneje
Copy link

rittneje commented Mar 4, 2022

Related to #51473. It is broken in more cases than just -trimpath.

@gopherbot
Copy link

Change https://go.dev/cl/391801 mentions this issue: cmd/go: use testGOROOT in TestListTemplateContextFunction

@gopherbot
Copy link

Change https://go.dev/cl/391814 mentions this issue: time: fix zoneinfo.zip locating logic when built with -trimpath

@gopherbot
Copy link

Change https://go.dev/cl/391809 mentions this issue: cmd/go: stamp build settings for binaries in cmd

@gopherbot
Copy link

Change https://go.dev/cl/391813 mentions this issue: cmd/doc: use 'go env' to identify GOROOT if it isn't otherwise known

@gopherbot
Copy link

Change https://go.dev/cl/391811 mentions this issue: cmd/go: diagnose an unset GOROOT when built with -trimpath

@gopherbot
Copy link

Change https://go.dev/cl/391805 mentions this issue: go/build: avoid setting Default.GOROOT when runtime.GOROOT() is empty

gopherbot pushed a commit that referenced this issue Mar 14, 2022
This test uses testgo to run 'go list', so it should use the correct
GOROOT for testgo. (This may be particularly relevant when the test
binary itself is build with -trimpath, in which case runtime.GOROOT()
is not valid.)

Updates #51483

Change-Id: I79b310f88e3a200122d6289073df1385e3e97cca
Reviewed-on: https://go-review.googlesource.com/c/go/+/391801
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Trust: Daniel Martí <mvdan@mvdan.cc>
@gopherbot
Copy link

Change https://go.dev/cl/393366 mentions this issue: cmd/doc: use the 'go' command from buildCtx.GOROOT, not whatever is in $PATH

gopherbot pushed a commit that referenced this issue Mar 17, 2022
Updates #51483.

Change-Id: I13d8e58b30639d8a5ed3c9e8b72c8bbaa6a6f1cc
Reviewed-on: https://go-review.googlesource.com/c/go/+/391813
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopherbot pushed a commit that referenced this issue Mar 17, 2022
…n $PATH

For #51483.

Change-Id: I6150fdf97763d858e9ab012e807515da3387c25f
Reviewed-on: https://go-review.googlesource.com/c/go/+/393366
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopherbot pushed a commit that referenced this issue Mar 18, 2022
When the test binary is built with the -trimpath flag,
runtime.GOROOT() is invalid, and must not be used to locate
GOROOT/lib/time/zoneinfo.zip. (We can use other sources instead.)

However, the test for the package expects zoneinfo.zip to definitely
exist. 'go test' runs the test binary in the directory containing its
source code — in this case GOROOT/src/time — so we can use that
information to find the zoneinfo.zip file when runtime.GOROOT isn't
available.

For #51483

Change-Id: I9de35252a988d146b5d746794323214d400e64e5
Reviewed-on: https://go-review.googlesource.com/c/go/+/391814
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopherbot pushed a commit that referenced this issue Mar 18, 2022
Also update cmd/dist to avoid setting gcflags and ldflags explicitly
when the set of flags to be set is empty (a verbose way of specifying
the default behavior).

Stamping was disabled for the Go standard library in CL 356014 due to
the cmd/dist flags causing cmd/go to (correctly) report the resulting
binaries as stale.

With cmd/dist fixed, we can also remove the special case in cmd/go,
which will allow tests of binaries in 'cmd' to read the build info
embedded in the test binary. That build info may be useful to
determine (say) whether runtime.GOROOT ought to work without GOROOT
set in the environment.

For #51483
Updates #37475

Change-Id: I64d04f5990190094eb6c0522db829d3bdfa50ef3
Reviewed-on: https://go-review.googlesource.com/c/go/+/391809
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopherbot pushed a commit that referenced this issue Mar 18, 2022
For #51483

Change-Id: I4546c20cf968b595020a1eba888fe1d9a1c6cfc5
Reviewed-on: https://go-review.googlesource.com/c/go/+/391811
Reviewed-by: Russ Cox <rsc@golang.org>
Trust: Bryan Mills <bcmills@google.com>
@rsc rsc unassigned bcmills Jun 22, 2022
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.
Projects
None yet
Development

No branches or pull requests

3 participants