Skip to content

Commit 9a932c5

Browse files
author
Bryan C. Mills
committed
internal/buildcfg: initialize GOROOT to runtime.GOROOT
In the beginning the Go compiler was in C, and C had a function 'getgoroot' that returned GOROOT from either the environment or a generated constant. 'getgoroot' was mechanically converted to Go (as obj.Getgoroot) in CL 3046. obj.Getgoroot begat obj.GOROOT. obj.GOROOT begat objabi.GOROOT, which begat buildcfg.GOROOT. As far as I can tell, today's buildcfg.GOROOT is functionally identical to runtime.GOROOT(). Let's reduce some complexity by defining it in those terms. While we're thinking about buildcfg.GOROOT, also check whether it is non-empty: if the toolchain is built with -trimpath, the value of GOROOT might not be valid or meaningful if the user invokes cmd/compile or cmd/link directly, or via a build tool other than cmd/go that doesn't care as much about GOROOT. (As of CL 390024, runtime.GOROOT will return the empty string instead of a bogus one when built with -trimpath.) For #51461. Change-Id: I9fec020d5fa65d4aff0dd39b805f5ca93f86c36e Reviewed-on: https://go-review.googlesource.com/c/go/+/393155 Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 67f6b8c commit 9a932c5

File tree

6 files changed

+8
-8
lines changed

6 files changed

+8
-8
lines changed

src/cmd/compile/internal/logopt/log_opts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ func uriIfy(f string) DocumentURI {
405405
// Return filename, replacing a first occurrence of $GOROOT with the
406406
// actual value of the GOROOT (because LSP does not speak "$GOROOT").
407407
func uprootedPath(filename string) string {
408-
if !strings.HasPrefix(filename, "$GOROOT/") {
408+
if buildcfg.GOROOT == "" || !strings.HasPrefix(filename, "$GOROOT/") {
409409
return filename
410410
}
411411
return buildcfg.GOROOT + filename[len("$GOROOT"):]

src/cmd/compile/internal/noder/reader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (pr *pkgReader) posBaseIdx(idx int) *src.PosBase {
209209
// require being more consistent about when we use native vs UNIX
210210
// file paths.
211211
const dollarGOROOT = "$GOROOT"
212-
if strings.HasPrefix(filename, dollarGOROOT) {
212+
if buildcfg.GOROOT != "" && strings.HasPrefix(filename, dollarGOROOT) {
213213
filename = buildcfg.GOROOT + filename[len(dollarGOROOT):]
214214
}
215215

src/cmd/internal/objabi/line.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func AbsFile(dir, file, rewrites string) string {
3939
}
4040

4141
abs, rewritten := ApplyRewrites(abs, rewrites)
42-
if !rewritten && hasPathPrefix(abs, buildcfg.GOROOT) {
42+
if !rewritten && buildcfg.GOROOT != "" && hasPathPrefix(abs, buildcfg.GOROOT) {
4343
abs = "$GOROOT" + abs[len(buildcfg.GOROOT):]
4444
}
4545

src/cmd/link/internal/ld/lib.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ func libinit(ctxt *Link) {
390390
suffix = "asan"
391391
}
392392

393-
Lflag(ctxt, filepath.Join(buildcfg.GOROOT, "pkg", fmt.Sprintf("%s_%s%s%s", buildcfg.GOOS, buildcfg.GOARCH, suffixsep, suffix)))
393+
if buildcfg.GOROOT != "" {
394+
Lflag(ctxt, filepath.Join(buildcfg.GOROOT, "pkg", fmt.Sprintf("%s_%s%s%s", buildcfg.GOOS, buildcfg.GOARCH, suffixsep, suffix)))
395+
}
394396

395397
mayberemoveoutfile()
396398

src/cmd/link/internal/ld/main.go

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func Main(arch *sys.Arch, theArch Arch) {
121121

122122
final := gorootFinal()
123123
addstrdata1(ctxt, "runtime.defaultGOROOT="+final)
124-
addstrdata1(ctxt, "internal/buildcfg.defaultGOROOT="+final)
125124

126125
buildVersion := buildcfg.Version
127126
if goexperiment := buildcfg.Experiment.String(); goexperiment != "" {

src/internal/buildcfg/cfg.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ import (
1515
"fmt"
1616
"os"
1717
"path/filepath"
18+
"runtime"
1819
"strings"
1920
)
2021

2122
var (
22-
defaultGOROOT string // set by linker
23-
24-
GOROOT = envOr("GOROOT", defaultGOROOT)
23+
GOROOT = runtime.GOROOT() // cached for efficiency
2524
GOARCH = envOr("GOARCH", defaultGOARCH)
2625
GOOS = envOr("GOOS", defaultGOOS)
2726
GO386 = envOr("GO386", defaultGO386)

0 commit comments

Comments
 (0)