Source file src/cmd/go/internal/cfg/cfg.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package cfg holds configuration shared by multiple parts
     6  // of the go command.
     7  package cfg
     8  
     9  import (
    10  	"bytes"
    11  	"context"
    12  	"fmt"
    13  	"go/build"
    14  	"internal/buildcfg"
    15  	"internal/cfg"
    16  	"internal/platform"
    17  	"io"
    18  	"io/fs"
    19  	"os"
    20  	"path/filepath"
    21  	"runtime"
    22  	"strings"
    23  	"sync"
    24  	"time"
    25  
    26  	"cmd/go/internal/fsys"
    27  	"cmd/internal/pathcache"
    28  )
    29  
    30  // Global build parameters (used during package load)
    31  var (
    32  	Goos   = envOr("GOOS", build.Default.GOOS)
    33  	Goarch = envOr("GOARCH", build.Default.GOARCH)
    34  
    35  	ExeSuffix = exeSuffix()
    36  
    37  	// ModulesEnabled specifies whether the go command is running
    38  	// in module-aware mode (as opposed to GOPATH mode).
    39  	// It is equal to modload.Enabled, but not all packages can import modload.
    40  	ModulesEnabled bool
    41  )
    42  
    43  func exeSuffix() string {
    44  	if Goos == "windows" {
    45  		return ".exe"
    46  	}
    47  	return ""
    48  }
    49  
    50  // Configuration for tools installed to GOROOT/bin.
    51  // Normally these match runtime.GOOS and runtime.GOARCH,
    52  // but when testing a cross-compiled cmd/go they will
    53  // indicate the GOOS and GOARCH of the installed cmd/go
    54  // rather than the test binary.
    55  var (
    56  	installedGOOS   string
    57  	installedGOARCH string
    58  )
    59  
    60  // ToolExeSuffix returns the suffix for executables installed
    61  // in build.ToolDir.
    62  func ToolExeSuffix() string {
    63  	if installedGOOS == "windows" {
    64  		return ".exe"
    65  	}
    66  	return ""
    67  }
    68  
    69  // These are general "build flags" used by build and other commands.
    70  var (
    71  	BuildA                 bool     // -a flag
    72  	BuildBuildmode         string   // -buildmode flag
    73  	BuildBuildvcs          = "auto" // -buildvcs flag: "true", "false", or "auto"
    74  	BuildContext           = defaultContext()
    75  	BuildMod               string                  // -mod flag
    76  	BuildModExplicit       bool                    // whether -mod was set explicitly
    77  	BuildModReason         string                  // reason -mod was set, if set by default
    78  	BuildLinkshared        bool                    // -linkshared flag
    79  	BuildMSan              bool                    // -msan flag
    80  	BuildASan              bool                    // -asan flag
    81  	BuildCover             bool                    // -cover flag
    82  	BuildCoverMode         string                  // -covermode flag
    83  	BuildCoverPkg          []string                // -coverpkg flag
    84  	BuildJSON              bool                    // -json flag
    85  	BuildN                 bool                    // -n flag
    86  	BuildO                 string                  // -o flag
    87  	BuildP                 = runtime.GOMAXPROCS(0) // -p flag
    88  	BuildPGO               string                  // -pgo flag
    89  	BuildPkgdir            string                  // -pkgdir flag
    90  	BuildRace              bool                    // -race flag
    91  	BuildToolexec          []string                // -toolexec flag
    92  	BuildToolchainName     string
    93  	BuildToolchainCompiler func() string
    94  	BuildToolchainLinker   func() string
    95  	BuildTrimpath          bool // -trimpath flag
    96  	BuildV                 bool // -v flag
    97  	BuildWork              bool // -work flag
    98  	BuildX                 bool // -x flag
    99  
   100  	ModCacheRW bool   // -modcacherw flag
   101  	ModFile    string // -modfile flag
   102  
   103  	CmdName string // "build", "install", "list", "mod tidy", etc.
   104  
   105  	DebugActiongraph  string // -debug-actiongraph flag (undocumented, unstable)
   106  	DebugTrace        string // -debug-trace flag
   107  	DebugRuntimeTrace string // -debug-runtime-trace flag (undocumented, unstable)
   108  
   109  	// GoPathError is set when GOPATH is not set. it contains an
   110  	// explanation why GOPATH is unset.
   111  	GoPathError   string
   112  	GOPATHChanged bool
   113  	CGOChanged    bool
   114  )
   115  
   116  func defaultContext() build.Context {
   117  	ctxt := build.Default
   118  
   119  	ctxt.JoinPath = filepath.Join // back door to say "do not use go command"
   120  
   121  	// Override defaults computed in go/build with defaults
   122  	// from go environment configuration file, if known.
   123  	ctxt.GOPATH, GOPATHChanged = EnvOrAndChanged("GOPATH", gopath(ctxt))
   124  	ctxt.GOOS = Goos
   125  	ctxt.GOARCH = Goarch
   126  
   127  	// Clear the GOEXPERIMENT-based tool tags, which we will recompute later.
   128  	var save []string
   129  	for _, tag := range ctxt.ToolTags {
   130  		if !strings.HasPrefix(tag, "goexperiment.") {
   131  			save = append(save, tag)
   132  		}
   133  	}
   134  	ctxt.ToolTags = save
   135  
   136  	// The go/build rule for whether cgo is enabled is:
   137  	//  1. If $CGO_ENABLED is set, respect it.
   138  	//  2. Otherwise, if this is a cross-compile, disable cgo.
   139  	//  3. Otherwise, use built-in default for GOOS/GOARCH.
   140  	//
   141  	// Recreate that logic here with the new GOOS/GOARCH setting.
   142  	// We need to run steps 2 and 3 to determine what the default value
   143  	// of CgoEnabled would be for computing CGOChanged.
   144  	defaultCgoEnabled := false
   145  	if buildcfg.DefaultCGO_ENABLED == "1" {
   146  		defaultCgoEnabled = true
   147  	} else if buildcfg.DefaultCGO_ENABLED == "0" {
   148  	} else if runtime.GOARCH == ctxt.GOARCH && runtime.GOOS == ctxt.GOOS {
   149  		defaultCgoEnabled = platform.CgoSupported(ctxt.GOOS, ctxt.GOARCH)
   150  		// Use built-in default cgo setting for GOOS/GOARCH.
   151  		// Note that ctxt.GOOS/GOARCH are derived from the preference list
   152  		// (1) environment, (2) go/env file, (3) runtime constants,
   153  		// while go/build.Default.GOOS/GOARCH are derived from the preference list
   154  		// (1) environment, (2) runtime constants.
   155  		//
   156  		// We know ctxt.GOOS/GOARCH == runtime.GOOS/GOARCH;
   157  		// no matter how that happened, go/build.Default will make the
   158  		// same decision (either the environment variables are set explicitly
   159  		// to match the runtime constants, or else they are unset, in which
   160  		// case go/build falls back to the runtime constants), so
   161  		// go/build.Default.GOOS/GOARCH == runtime.GOOS/GOARCH.
   162  		// So ctxt.CgoEnabled (== go/build.Default.CgoEnabled) is correct
   163  		// as is and can be left unmodified.
   164  		//
   165  		// All that said, starting in Go 1.20 we layer one more rule
   166  		// on top of the go/build decision: if CC is unset and
   167  		// the default C compiler we'd look for is not in the PATH,
   168  		// we automatically default cgo to off.
   169  		// This makes go builds work automatically on systems
   170  		// without a C compiler installed.
   171  		if ctxt.CgoEnabled {
   172  			if os.Getenv("CC") == "" {
   173  				cc := DefaultCC(ctxt.GOOS, ctxt.GOARCH)
   174  				if _, err := pathcache.LookPath(cc); err != nil {
   175  					defaultCgoEnabled = false
   176  				}
   177  			}
   178  		}
   179  	}
   180  	ctxt.CgoEnabled = defaultCgoEnabled
   181  	if v := Getenv("CGO_ENABLED"); v == "0" || v == "1" {
   182  		ctxt.CgoEnabled = v[0] == '1'
   183  	}
   184  	CGOChanged = ctxt.CgoEnabled != defaultCgoEnabled
   185  
   186  	ctxt.OpenFile = func(path string) (io.ReadCloser, error) {
   187  		return fsys.Open(path)
   188  	}
   189  	ctxt.ReadDir = func(path string) ([]fs.FileInfo, error) {
   190  		// Convert []fs.DirEntry to []fs.FileInfo using dirInfo.
   191  		dirs, err := fsys.ReadDir(path)
   192  		infos := make([]fs.FileInfo, len(dirs))
   193  		for i, dir := range dirs {
   194  			infos[i] = &dirInfo{dir}
   195  		}
   196  		return infos, err
   197  	}
   198  	ctxt.IsDir = func(path string) bool {
   199  		isDir, err := fsys.IsDir(path)
   200  		return err == nil && isDir
   201  	}
   202  
   203  	return ctxt
   204  }
   205  
   206  func init() {
   207  	SetGOROOT(Getenv("GOROOT"), false)
   208  }
   209  
   210  // ForceHost forces GOOS and GOARCH to runtime.GOOS and runtime.GOARCH.
   211  // This is used by go tool to build tools for the go command's own
   212  // GOOS and GOARCH.
   213  func ForceHost() {
   214  	Goos = runtime.GOOS
   215  	Goarch = runtime.GOARCH
   216  	ExeSuffix = exeSuffix()
   217  	GO386 = buildcfg.DefaultGO386
   218  	GOAMD64 = buildcfg.DefaultGOAMD64
   219  	GOARM = buildcfg.DefaultGOARM
   220  	GOARM64 = buildcfg.DefaultGOARM64
   221  	GOMIPS = buildcfg.DefaultGOMIPS
   222  	GOMIPS64 = buildcfg.DefaultGOMIPS64
   223  	GOPPC64 = buildcfg.DefaultGOPPC64
   224  	GORISCV64 = buildcfg.DefaultGORISCV64
   225  	GOWASM = ""
   226  
   227  	// Recompute the build context using Goos and Goarch to
   228  	// set the correct value for ctx.CgoEnabled.
   229  	BuildContext = defaultContext()
   230  	// Recompute experiments: the settings determined depend on GOOS and GOARCH.
   231  	// This will also update the BuildContext's tool tags to include the new
   232  	// experiment tags.
   233  	computeExperiment()
   234  }
   235  
   236  // SetGOROOT sets GOROOT and associated variables to the given values.
   237  //
   238  // If isTestGo is true, build.ToolDir is set based on the TESTGO_GOHOSTOS and
   239  // TESTGO_GOHOSTARCH environment variables instead of runtime.GOOS and
   240  // runtime.GOARCH.
   241  func SetGOROOT(goroot string, isTestGo bool) {
   242  	BuildContext.GOROOT = goroot
   243  
   244  	GOROOT = goroot
   245  	if goroot == "" {
   246  		GOROOTbin = ""
   247  		GOROOTpkg = ""
   248  		GOROOTsrc = ""
   249  	} else {
   250  		GOROOTbin = filepath.Join(goroot, "bin")
   251  		GOROOTpkg = filepath.Join(goroot, "pkg")
   252  		GOROOTsrc = filepath.Join(goroot, "src")
   253  	}
   254  
   255  	installedGOOS = runtime.GOOS
   256  	installedGOARCH = runtime.GOARCH
   257  	if isTestGo {
   258  		if testOS := os.Getenv("TESTGO_GOHOSTOS"); testOS != "" {
   259  			installedGOOS = testOS
   260  		}
   261  		if testArch := os.Getenv("TESTGO_GOHOSTARCH"); testArch != "" {
   262  			installedGOARCH = testArch
   263  		}
   264  	}
   265  
   266  	if runtime.Compiler != "gccgo" {
   267  		if goroot == "" {
   268  			build.ToolDir = ""
   269  		} else {
   270  			// Note that we must use the installed OS and arch here: the tool
   271  			// directory does not move based on environment variables, and even if we
   272  			// are testing a cross-compiled cmd/go all of the installed packages and
   273  			// tools would have been built using the native compiler and linker (and
   274  			// would spuriously appear stale if we used a cross-compiled compiler and
   275  			// linker).
   276  			//
   277  			// This matches the initialization of ToolDir in go/build, except for
   278  			// using ctxt.GOROOT and the installed GOOS and GOARCH rather than the
   279  			// GOROOT, GOOS, and GOARCH reported by the runtime package.
   280  			build.ToolDir = filepath.Join(GOROOTpkg, "tool", installedGOOS+"_"+installedGOARCH)
   281  		}
   282  	}
   283  }
   284  
   285  // Experiment configuration.
   286  var (
   287  	// RawGOEXPERIMENT is the GOEXPERIMENT value set by the user.
   288  	RawGOEXPERIMENT = envOr("GOEXPERIMENT", buildcfg.DefaultGOEXPERIMENT)
   289  	// CleanGOEXPERIMENT is the minimal GOEXPERIMENT value needed to reproduce the
   290  	// experiments enabled by RawGOEXPERIMENT.
   291  	CleanGOEXPERIMENT = RawGOEXPERIMENT
   292  
   293  	Experiment    *buildcfg.ExperimentFlags
   294  	ExperimentErr error
   295  )
   296  
   297  func init() {
   298  	computeExperiment()
   299  }
   300  
   301  func computeExperiment() {
   302  	Experiment, ExperimentErr = buildcfg.ParseGOEXPERIMENT(Goos, Goarch, RawGOEXPERIMENT)
   303  	if ExperimentErr != nil {
   304  		return
   305  	}
   306  
   307  	// GOEXPERIMENT is valid, so convert it to canonical form.
   308  	CleanGOEXPERIMENT = Experiment.String()
   309  
   310  	// Add build tags based on the experiments in effect.
   311  	exps := Experiment.Enabled()
   312  	expTags := make([]string, 0, len(exps)+len(BuildContext.ToolTags))
   313  	for _, exp := range exps {
   314  		expTags = append(expTags, "goexperiment."+exp)
   315  	}
   316  	BuildContext.ToolTags = append(expTags, BuildContext.ToolTags...)
   317  }
   318  
   319  // An EnvVar is an environment variable Name=Value.
   320  type EnvVar struct {
   321  	Name    string
   322  	Value   string
   323  	Changed bool // effective Value differs from default
   324  }
   325  
   326  // OrigEnv is the original environment of the program at startup.
   327  var OrigEnv []string
   328  
   329  // CmdEnv is the new environment for running go tool commands.
   330  // User binaries (during go test or go run) are run with OrigEnv,
   331  // not CmdEnv.
   332  var CmdEnv []EnvVar
   333  
   334  var envCache struct {
   335  	once   sync.Once
   336  	m      map[string]string
   337  	goroot map[string]string
   338  }
   339  
   340  // EnvFile returns the name of the Go environment configuration file,
   341  // and reports whether the effective value differs from the default.
   342  func EnvFile() (string, bool, error) {
   343  	if file := os.Getenv("GOENV"); file != "" {
   344  		if file == "off" {
   345  			return "", false, fmt.Errorf("GOENV=off")
   346  		}
   347  		return file, true, nil
   348  	}
   349  	dir, err := os.UserConfigDir()
   350  	if err != nil {
   351  		return "", false, err
   352  	}
   353  	if dir == "" {
   354  		return "", false, fmt.Errorf("missing user-config dir")
   355  	}
   356  	return filepath.Join(dir, "go/env"), false, nil
   357  }
   358  
   359  func initEnvCache() {
   360  	envCache.m = make(map[string]string)
   361  	envCache.goroot = make(map[string]string)
   362  	if file, _, _ := EnvFile(); file != "" {
   363  		readEnvFile(file, "user")
   364  	}
   365  	goroot := findGOROOT(envCache.m["GOROOT"])
   366  	if goroot != "" {
   367  		readEnvFile(filepath.Join(goroot, "go.env"), "GOROOT")
   368  	}
   369  
   370  	// Save the goroot for func init calling SetGOROOT,
   371  	// and also overwrite anything that might have been in go.env.
   372  	// It makes no sense for GOROOT/go.env to specify
   373  	// a different GOROOT.
   374  	envCache.m["GOROOT"] = goroot
   375  }
   376  
   377  func readEnvFile(file string, source string) {
   378  	if file == "" {
   379  		return
   380  	}
   381  	data, err := os.ReadFile(file)
   382  	if err != nil {
   383  		return
   384  	}
   385  
   386  	for len(data) > 0 {
   387  		// Get next line.
   388  		line := data
   389  		i := bytes.IndexByte(data, '\n')
   390  		if i >= 0 {
   391  			line, data = line[:i], data[i+1:]
   392  		} else {
   393  			data = nil
   394  		}
   395  
   396  		i = bytes.IndexByte(line, '=')
   397  		if i < 0 || line[0] < 'A' || 'Z' < line[0] {
   398  			// Line is missing = (or empty) or a comment or not a valid env name. Ignore.
   399  			// This should not happen in the user file, since the file should be maintained almost
   400  			// exclusively by "go env -w", but better to silently ignore than to make
   401  			// the go command unusable just because somehow the env file has
   402  			// gotten corrupted.
   403  			// In the GOROOT/go.env file, we expect comments.
   404  			continue
   405  		}
   406  		key, val := line[:i], line[i+1:]
   407  
   408  		if source == "GOROOT" {
   409  			envCache.goroot[string(key)] = string(val)
   410  			// In the GOROOT/go.env file, do not overwrite fields loaded from the user's go/env file.
   411  			if _, ok := envCache.m[string(key)]; ok {
   412  				continue
   413  			}
   414  		}
   415  		envCache.m[string(key)] = string(val)
   416  	}
   417  }
   418  
   419  // Getenv gets the value for the configuration key.
   420  // It consults the operating system environment
   421  // and then the go/env file.
   422  // If Getenv is called for a key that cannot be set
   423  // in the go/env file (for example GODEBUG), it panics.
   424  // This ensures that CanGetenv is accurate, so that
   425  // 'go env -w' stays in sync with what Getenv can retrieve.
   426  func Getenv(key string) string {
   427  	if !CanGetenv(key) {
   428  		switch key {
   429  		case "CGO_TEST_ALLOW", "CGO_TEST_DISALLOW", "CGO_test_ALLOW", "CGO_test_DISALLOW":
   430  			// used by internal/work/security_test.go; allow
   431  		default:
   432  			panic("internal error: invalid Getenv " + key)
   433  		}
   434  	}
   435  	val := os.Getenv(key)
   436  	if val != "" {
   437  		return val
   438  	}
   439  	envCache.once.Do(initEnvCache)
   440  	return envCache.m[key]
   441  }
   442  
   443  // CanGetenv reports whether key is a valid go/env configuration key.
   444  func CanGetenv(key string) bool {
   445  	envCache.once.Do(initEnvCache)
   446  	if _, ok := envCache.m[key]; ok {
   447  		// Assume anything in the user file or go.env file is valid.
   448  		return true
   449  	}
   450  	return strings.Contains(cfg.KnownEnv, "\t"+key+"\n")
   451  }
   452  
   453  var (
   454  	GOROOT string
   455  
   456  	// Either empty or produced by filepath.Join(GOROOT, …).
   457  	GOROOTbin string
   458  	GOROOTpkg string
   459  	GOROOTsrc string
   460  
   461  	GOBIN                           = Getenv("GOBIN")
   462  	GOCACHEPROG, GOCACHEPROGChanged = EnvOrAndChanged("GOCACHEPROG", "")
   463  	GOMODCACHE, GOMODCACHEChanged   = EnvOrAndChanged("GOMODCACHE", gopathDir("pkg/mod"))
   464  
   465  	// Used in envcmd.MkEnv and build ID computations.
   466  	GOARM64, goARM64Changed     = EnvOrAndChanged("GOARM64", buildcfg.DefaultGOARM64)
   467  	GOARM, goARMChanged         = EnvOrAndChanged("GOARM", buildcfg.DefaultGOARM)
   468  	GO386, go386Changed         = EnvOrAndChanged("GO386", buildcfg.DefaultGO386)
   469  	GOAMD64, goAMD64Changed     = EnvOrAndChanged("GOAMD64", buildcfg.DefaultGOAMD64)
   470  	GOMIPS, goMIPSChanged       = EnvOrAndChanged("GOMIPS", buildcfg.DefaultGOMIPS)
   471  	GOMIPS64, goMIPS64Changed   = EnvOrAndChanged("GOMIPS64", buildcfg.DefaultGOMIPS64)
   472  	GOPPC64, goPPC64Changed     = EnvOrAndChanged("GOPPC64", buildcfg.DefaultGOPPC64)
   473  	GORISCV64, goRISCV64Changed = EnvOrAndChanged("GORISCV64", buildcfg.DefaultGORISCV64)
   474  	GOWASM, goWASMChanged       = EnvOrAndChanged("GOWASM", fmt.Sprint(buildcfg.GOWASM))
   475  
   476  	GOFIPS140, GOFIPS140Changed = EnvOrAndChanged("GOFIPS140", buildcfg.DefaultGOFIPS140)
   477  	GOPROXY, GOPROXYChanged     = EnvOrAndChanged("GOPROXY", "")
   478  	GOSUMDB, GOSUMDBChanged     = EnvOrAndChanged("GOSUMDB", "")
   479  	GOPRIVATE                   = Getenv("GOPRIVATE")
   480  	GONOPROXY, GONOPROXYChanged = EnvOrAndChanged("GONOPROXY", GOPRIVATE)
   481  	GONOSUMDB, GONOSUMDBChanged = EnvOrAndChanged("GONOSUMDB", GOPRIVATE)
   482  	GOINSECURE                  = Getenv("GOINSECURE")
   483  	GOVCS                       = Getenv("GOVCS")
   484  	GOAUTH, GOAUTHChanged       = EnvOrAndChanged("GOAUTH", "netrc")
   485  )
   486  
   487  // EnvOrAndChanged returns the environment variable value
   488  // and reports whether it differs from the default value.
   489  func EnvOrAndChanged(name, def string) (v string, changed bool) {
   490  	val := Getenv(name)
   491  	if val != "" {
   492  		v = val
   493  		if g, ok := envCache.goroot[name]; ok {
   494  			changed = val != g
   495  		} else {
   496  			changed = val != def
   497  		}
   498  		return v, changed
   499  	}
   500  	return def, false
   501  }
   502  
   503  var SumdbDir = gopathDir("pkg/sumdb")
   504  
   505  // GetArchEnv returns the name and setting of the
   506  // GOARCH-specific architecture environment variable.
   507  // If the current architecture has no GOARCH-specific variable,
   508  // GetArchEnv returns empty key and value.
   509  func GetArchEnv() (key, val string, changed bool) {
   510  	switch Goarch {
   511  	case "arm":
   512  		return "GOARM", GOARM, goARMChanged
   513  	case "arm64":
   514  		return "GOARM64", GOARM64, goARM64Changed
   515  	case "386":
   516  		return "GO386", GO386, go386Changed
   517  	case "amd64":
   518  		return "GOAMD64", GOAMD64, goAMD64Changed
   519  	case "mips", "mipsle":
   520  		return "GOMIPS", GOMIPS, goMIPSChanged
   521  	case "mips64", "mips64le":
   522  		return "GOMIPS64", GOMIPS64, goMIPS64Changed
   523  	case "ppc64", "ppc64le":
   524  		return "GOPPC64", GOPPC64, goPPC64Changed
   525  	case "riscv64":
   526  		return "GORISCV64", GORISCV64, goRISCV64Changed
   527  	case "wasm":
   528  		return "GOWASM", GOWASM, goWASMChanged
   529  	}
   530  	return "", "", false
   531  }
   532  
   533  // envOr returns Getenv(key) if set, or else def.
   534  func envOr(key, def string) string {
   535  	val := Getenv(key)
   536  	if val == "" {
   537  		val = def
   538  	}
   539  	return val
   540  }
   541  
   542  // There is a copy of findGOROOT, isSameDir, and isGOROOT in
   543  // x/tools/cmd/godoc/goroot.go.
   544  // Try to keep them in sync for now.
   545  
   546  // findGOROOT returns the GOROOT value, using either an explicitly
   547  // provided environment variable, a GOROOT that contains the current
   548  // os.Executable value, or else the GOROOT that the binary was built
   549  // with from runtime.GOROOT().
   550  //
   551  // There is a copy of this code in x/tools/cmd/godoc/goroot.go.
   552  func findGOROOT(env string) string {
   553  	if env == "" {
   554  		// Not using Getenv because findGOROOT is called
   555  		// to find the GOROOT/go.env file. initEnvCache
   556  		// has passed in the setting from the user go/env file.
   557  		env = os.Getenv("GOROOT")
   558  	}
   559  	if env != "" {
   560  		return filepath.Clean(env)
   561  	}
   562  	def := ""
   563  	if r := runtime.GOROOT(); r != "" {
   564  		def = filepath.Clean(r)
   565  	}
   566  	if runtime.Compiler == "gccgo" {
   567  		// gccgo has no real GOROOT, and it certainly doesn't
   568  		// depend on the executable's location.
   569  		return def
   570  	}
   571  
   572  	// canonical returns a directory path that represents
   573  	// the same directory as dir,
   574  	// preferring the spelling in def if the two are the same.
   575  	canonical := func(dir string) string {
   576  		if isSameDir(def, dir) {
   577  			return def
   578  		}
   579  		return dir
   580  	}
   581  
   582  	exe, err := os.Executable()
   583  	if err == nil {
   584  		exe, err = filepath.Abs(exe)
   585  		if err == nil {
   586  			// cmd/go may be installed in GOROOT/bin or GOROOT/bin/GOOS_GOARCH,
   587  			// depending on whether it was cross-compiled with a different
   588  			// GOHOSTOS (see https://go.dev/issue/62119). Try both.
   589  			if dir := filepath.Join(exe, "../.."); isGOROOT(dir) {
   590  				return canonical(dir)
   591  			}
   592  			if dir := filepath.Join(exe, "../../.."); isGOROOT(dir) {
   593  				return canonical(dir)
   594  			}
   595  
   596  			// Depending on what was passed on the command line, it is possible
   597  			// that os.Executable is a symlink (like /usr/local/bin/go) referring
   598  			// to a binary installed in a real GOROOT elsewhere
   599  			// (like /usr/lib/go/bin/go).
   600  			// Try to find that GOROOT by resolving the symlinks.
   601  			exe, err = filepath.EvalSymlinks(exe)
   602  			if err == nil {
   603  				if dir := filepath.Join(exe, "../.."); isGOROOT(dir) {
   604  					return canonical(dir)
   605  				}
   606  				if dir := filepath.Join(exe, "../../.."); isGOROOT(dir) {
   607  					return canonical(dir)
   608  				}
   609  			}
   610  		}
   611  	}
   612  	return def
   613  }
   614  
   615  // isSameDir reports whether dir1 and dir2 are the same directory.
   616  func isSameDir(dir1, dir2 string) bool {
   617  	if dir1 == dir2 {
   618  		return true
   619  	}
   620  	info1, err1 := os.Stat(dir1)
   621  	info2, err2 := os.Stat(dir2)
   622  	return err1 == nil && err2 == nil && os.SameFile(info1, info2)
   623  }
   624  
   625  // isGOROOT reports whether path looks like a GOROOT.
   626  //
   627  // It does this by looking for the path/pkg/tool directory,
   628  // which is necessary for useful operation of the cmd/go tool,
   629  // and is not typically present in a GOPATH.
   630  //
   631  // There is a copy of this code in x/tools/cmd/godoc/goroot.go.
   632  func isGOROOT(path string) bool {
   633  	stat, err := os.Stat(filepath.Join(path, "pkg", "tool"))
   634  	if err != nil {
   635  		return false
   636  	}
   637  	return stat.IsDir()
   638  }
   639  
   640  func gopathDir(rel string) string {
   641  	list := filepath.SplitList(BuildContext.GOPATH)
   642  	if len(list) == 0 || list[0] == "" {
   643  		return ""
   644  	}
   645  	return filepath.Join(list[0], rel)
   646  }
   647  
   648  // Keep consistent with go/build.defaultGOPATH.
   649  func gopath(ctxt build.Context) string {
   650  	if len(ctxt.GOPATH) > 0 {
   651  		return ctxt.GOPATH
   652  	}
   653  	env := "HOME"
   654  	if runtime.GOOS == "windows" {
   655  		env = "USERPROFILE"
   656  	} else if runtime.GOOS == "plan9" {
   657  		env = "home"
   658  	}
   659  	if home := os.Getenv(env); home != "" {
   660  		def := filepath.Join(home, "go")
   661  		if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) {
   662  			GoPathError = "cannot set GOROOT as GOPATH"
   663  		}
   664  		return ""
   665  	}
   666  	GoPathError = fmt.Sprintf("%s is not set", env)
   667  	return ""
   668  }
   669  
   670  // WithBuildXWriter returns a Context in which BuildX output is written
   671  // to given io.Writer.
   672  func WithBuildXWriter(ctx context.Context, xLog io.Writer) context.Context {
   673  	return context.WithValue(ctx, buildXContextKey{}, xLog)
   674  }
   675  
   676  type buildXContextKey struct{}
   677  
   678  // BuildXWriter returns nil if BuildX is false, or
   679  // the writer to which BuildX output should be written otherwise.
   680  func BuildXWriter(ctx context.Context) (io.Writer, bool) {
   681  	if !BuildX {
   682  		return nil, false
   683  	}
   684  	if v := ctx.Value(buildXContextKey{}); v != nil {
   685  		return v.(io.Writer), true
   686  	}
   687  	return os.Stderr, true
   688  }
   689  
   690  // A dirInfo implements fs.FileInfo from fs.DirEntry.
   691  // We know that go/build doesn't use the non-DirEntry parts,
   692  // so we can panic instead of doing difficult work.
   693  type dirInfo struct {
   694  	dir fs.DirEntry
   695  }
   696  
   697  func (d *dirInfo) Name() string      { return d.dir.Name() }
   698  func (d *dirInfo) IsDir() bool       { return d.dir.IsDir() }
   699  func (d *dirInfo) Mode() fs.FileMode { return d.dir.Type() }
   700  
   701  func (d *dirInfo) Size() int64        { panic("dirInfo.Size") }
   702  func (d *dirInfo) ModTime() time.Time { panic("dirInfo.ModTime") }
   703  func (d *dirInfo) Sys() any           { panic("dirInfo.Sys") }
   704  

View as plain text