Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/link: avoid stamping runtime.defaultGOROOT when paths are being t…
…rimmed Previously, runtime.GOROOT() would return the string "go" in a binary build with -trimpath. This change stamps the empty string instead, using a sentinel value passed from cmd/go that looks like the GOROOT environment variable (either "$GOROOT" or "%GOROOT%", depending on the platform). Fixes #51461 Change-Id: I1f10ef2435016a7b6213bd8c547df911f7feeae7 Reviewed-on: https://go-review.googlesource.com/c/go/+/390024 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>
- Loading branch information
Bryan C. Mills
committed
Mar 18, 2022
1 parent
9a932c5
commit 9ac75d3
Showing
4 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Regression test for https://go.dev/issue/51461 and https://go.dev/issue/51483. | ||
# | ||
# When built with -trimpath, runtime.GOROOT() returned the bogus string "go" | ||
# if GOROOT was not set explicitly in the environment. | ||
# It should instead return the empty string, since we know that we don't | ||
# have a valid path to return. | ||
# | ||
# TODO(#51483): when runtime.GOROOT() returns the empty string, | ||
# go/build should default to 'go env GOROOT' instead. | ||
|
||
[short] skip | ||
|
||
env GOROOT= | ||
env GOROOT_FINAL= | ||
|
||
go run . | ||
stdout '^GOROOT '$TESTGO_GOROOT'$' | ||
stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$' | ||
|
||
go test -v . | ||
stdout '^GOROOT '$TESTGO_GOROOT'$' | ||
stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$' | ||
|
||
! go run -trimpath . | ||
stdout '^GOROOT $' | ||
stderr '^package runtime is not in GOROOT \(src'${/}'runtime\)$' | ||
|
||
! go test -trimpath -v . | ||
stdout '^GOROOT $' | ||
stdout '^package runtime is not in GOROOT \(src'${/}'runtime\)$' | ||
|
||
-- go.mod -- | ||
module example | ||
|
||
go 1.19 | ||
-- main.go -- | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"go/build" | ||
"os" | ||
"runtime" | ||
) | ||
|
||
func main() { | ||
fmt.Println("GOROOT", runtime.GOROOT()) | ||
|
||
p, err := build.Default.Import("runtime", "", build.FindOnly) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
fmt.Println("runtime", p.Dir) | ||
} | ||
-- main_test.go -- | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestMain(*testing.M) { | ||
main() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters