]> Cypherpunks repositories - gostls13.git/commitdiff
cmd: extract cmd/go's cfg.LookPath into separate pathcache package
authorThan McIntosh <thanm@google.com>
Sat, 27 Jul 2024 14:59:42 +0000 (14:59 +0000)
committerThan McIntosh <thanm@google.com>
Mon, 29 Jul 2024 15:38:33 +0000 (15:38 +0000)
Lift out the LookPath cached lookup utility function into a separate
"cmd/internal/pathcache" package, so that it can be reused in other
commands in addition to cmd/go. No change in functionality.

Change-Id: Ica7fa627000843360c3e353d40a9a70605fbe033
Reviewed-on: https://go-review.googlesource.com/c/go/+/601479
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

14 files changed:
src/cmd/go/internal/cfg/bench_test.go
src/cmd/go/internal/cfg/cfg.go
src/cmd/go/internal/generate/generate.go
src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/script/cmds.go
src/cmd/go/internal/script/scripttest/scripttest.go
src/cmd/go/internal/toolchain/select.go
src/cmd/go/internal/vcs/vcs.go
src/cmd/go/internal/work/build.go
src/cmd/go/internal/work/buildid.go
src/cmd/go/internal/work/exec.go
src/cmd/go/internal/work/gccgo.go
src/cmd/go/internal/work/shell.go
src/cmd/internal/pathcache/lookpath.go [moved from src/cmd/go/internal/cfg/lookpath.go with 96% similarity]

index 2dd99319fc4cecf3e27e7e54e11812ed74742821..1ed663125a64417e1d5ac9f7c6f626774200bde0 100644 (file)
@@ -5,6 +5,7 @@
 package cfg
 
 import (
+       "cmd/internal/pathcache"
        "internal/testenv"
        "testing"
 )
@@ -13,7 +14,7 @@ func BenchmarkLookPath(b *testing.B) {
        testenv.MustHaveExecPath(b, "go")
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
-               _, err := LookPath("go")
+               _, err := pathcache.LookPath("go")
                if err != nil {
                        b.Fatal(err)
                }
index 3715a19a96dfaa8eeb1b845c79e5c90066271b65..b2545ca4ea4da3f80c3c94906b3b60b2e2714202 100644 (file)
@@ -21,6 +21,7 @@ import (
        "sync"
 
        "cmd/go/internal/fsys"
+       "cmd/internal/pathcache"
 )
 
 // Global build parameters (used during package load)
@@ -162,7 +163,7 @@ func defaultContext() build.Context {
                if ctxt.CgoEnabled {
                        if os.Getenv("CC") == "" {
                                cc := DefaultCC(ctxt.GOOS, ctxt.GOARCH)
-                               if _, err := LookPath(cc); err != nil {
+                               if _, err := pathcache.LookPath(cc); err != nil {
                                        defaultCgoEnabled = false
                                }
                        }
index 6371353e2024356305bf50e606af233cc4b4641d..3a3b95786aa5889ecef209c638e5ac719c213bb0 100644 (file)
@@ -28,6 +28,7 @@ import (
        "cmd/go/internal/modload"
        "cmd/go/internal/str"
        "cmd/go/internal/work"
+       "cmd/internal/pathcache"
 )
 
 var CmdGenerate = &base.Command{
@@ -489,7 +490,7 @@ func (g *Generator) exec(words []string) {
                // intends to use the same 'go' as 'go generate' itself.
                // Prefer to resolve the binary from GOROOT/bin, and for consistency
                // prefer to resolve any other commands there too.
-               gorootBinPath, err := cfg.LookPath(filepath.Join(cfg.GOROOTbin, path))
+               gorootBinPath, err := pathcache.LookPath(filepath.Join(cfg.GOROOTbin, path))
                if err == nil {
                        path = gorootBinPath
                }
index 0e871758b38be3d5e11779583a54c6aca8e5a631..bf432f0bb78c6fd255a38a76bc0e42cf9f1ae484 100644 (file)
@@ -44,6 +44,7 @@ import (
        "cmd/go/internal/trace"
        "cmd/go/internal/vcs"
        "cmd/internal/par"
+       "cmd/internal/pathcache"
        "cmd/internal/pkgpattern"
 
        "golang.org/x/mod/modfile"
@@ -2443,7 +2444,7 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
                        goto omitVCS
                }
                if cfg.BuildBuildvcs == "auto" && vcsCmd != nil && vcsCmd.Cmd != "" {
-                       if _, err := cfg.LookPath(vcsCmd.Cmd); err != nil {
+                       if _, err := pathcache.LookPath(vcsCmd.Cmd); err != nil {
                                // We fould a repository, but the required VCS tool is not present.
                                // "-buildvcs=auto" means that we should silently drop the VCS metadata.
                                goto omitVCS
index 3ea9193cb13de869ec701e097f6e91aceaa88a86..7a930caf355d666f66bac35c0856a94aee293a6d 100644 (file)
@@ -5,7 +5,7 @@
 package script
 
 import (
-       "cmd/go/internal/cfg"
+       "cmd/internal/pathcache"
        "cmd/internal/robustio"
        "errors"
        "fmt"
@@ -825,7 +825,7 @@ func Program(name string, cancel func(*exec.Cmd) error, waitDelay time.Duration)
                },
                func(s *State, args ...string) (WaitFunc, error) {
                        lookPathOnce.Do(func() {
-                               path, pathErr = cfg.LookPath(name)
+                               path, pathErr = pathcache.LookPath(name)
                        })
                        if pathErr != nil {
                                return nil, pathErr
index 6d7bd7863b9f42b1bebf1906e794c5119d599e34..07183cd7bb4f9599df4d05d74c3b9e191be468e0 100644 (file)
@@ -7,8 +7,8 @@ package scripttest
 
 import (
        "bufio"
-       "cmd/go/internal/cfg"
        "cmd/go/internal/script"
+       "cmd/internal/pathcache"
        "errors"
        "io"
        "strings"
@@ -137,7 +137,7 @@ func CachedExec() script.Cond {
        return script.CachedCondition(
                "<suffix> names an executable in the test binary's PATH",
                func(name string) (bool, error) {
-                       _, err := cfg.LookPath(name)
+                       _, err := pathcache.LookPath(name)
                        return err == nil, nil
                })
 }
index 8e93e6c903308736fe5de01998da50936d8a1bbb..b20a2332a4720cdc2073895568a84dc30a82cd00 100644 (file)
@@ -26,6 +26,7 @@ import (
        "cmd/go/internal/modload"
        "cmd/go/internal/run"
        "cmd/go/internal/work"
+       "cmd/internal/pathcache"
        "cmd/internal/telemetry/counter"
 
        "golang.org/x/mod/module"
@@ -308,7 +309,7 @@ func Exec(gotoolchain string) {
        // Look in PATH for the toolchain before we download one.
        // This allows custom toolchains as well as reuse of toolchains
        // already installed using go install golang.org/dl/go1.2.3@latest.
-       if exe, err := cfg.LookPath(gotoolchain); err == nil {
+       if exe, err := pathcache.LookPath(gotoolchain); err == nil {
                execGoToolchain(gotoolchain, "", exe)
        }
 
index 19a6a5ef6b0b7cadf82c28b733f365202a14ad7f..2e7b5b0bea4a6305f892f75b00b6b74711342b8c 100644 (file)
@@ -27,6 +27,7 @@ import (
        "cmd/go/internal/search"
        "cmd/go/internal/str"
        "cmd/go/internal/web"
+       "cmd/internal/pathcache"
 
        "golang.org/x/mod/module"
 )
@@ -678,7 +679,7 @@ func (v *Cmd) run1(dir string, cmdline string, keyval []string, verbose bool) ([
                args = args[2:]
        }
 
-       _, err := cfg.LookPath(v.Cmd)
+       _, err := pathcache.LookPath(v.Cmd)
        if err != nil {
                fmt.Fprintf(os.Stderr,
                        "go: missing %s command. See https://golang.org/s/gogetcmd\n",
index ccfb4622e25a449b2953260695f9d5f62ebd230e..83caea9525fde18c85ebdb350f2a4513ddeafd4f 100644 (file)
@@ -23,6 +23,7 @@ import (
        "cmd/go/internal/modload"
        "cmd/go/internal/search"
        "cmd/go/internal/trace"
+       "cmd/internal/pathcache"
 )
 
 var CmdBuild = &base.Command{
@@ -901,7 +902,7 @@ func FindExecCmd() []string {
        if cfg.Goos == runtime.GOOS && cfg.Goarch == runtime.GOARCH {
                return ExecCmd
        }
-       path, err := cfg.LookPath(fmt.Sprintf("go_%s_%s_exec", cfg.Goos, cfg.Goarch))
+       path, err := pathcache.LookPath(fmt.Sprintf("go_%s_%s_exec", cfg.Goos, cfg.Goarch))
        if err == nil {
                ExecCmd = []string{path}
        }
index 4ee43e24369286264ef69f9a373f25edb1140ca2..2134079f839ab1fff3aae4f6f97405629e58bb71 100644 (file)
@@ -18,6 +18,7 @@ import (
        "cmd/go/internal/fsys"
        "cmd/go/internal/str"
        "cmd/internal/buildid"
+       "cmd/internal/pathcache"
        "cmd/internal/quoted"
        "cmd/internal/telemetry/counter"
 )
@@ -292,7 +293,7 @@ func (b *Builder) gccToolID(name, language string) (id, exe string, err error) {
                }
                exe = fields[0]
                if !strings.ContainsAny(exe, `/\`) {
-                       if lp, err := cfg.LookPath(exe); err == nil {
+                       if lp, err := pathcache.LookPath(exe); err == nil {
                                exe = lp
                        }
                }
index c4852d82aeb818cec01f1eb7d2527e6fe619e7dd..5b17ef48111098a30ee11d8d262c85f878df5710 100644 (file)
@@ -9,6 +9,7 @@ package work
 import (
        "bytes"
        "cmd/internal/cov/covcmd"
+       "cmd/internal/pathcache"
        "context"
        "crypto/sha256"
        "encoding/json"
@@ -2576,7 +2577,7 @@ func (b *Builder) gccCompilerID(compiler string) (id cache.ActionID, ok bool) {
        //
        // Otherwise, we compute a new validation description
        // and compiler id (below).
-       exe, err := cfg.LookPath(compiler)
+       exe, err := pathcache.LookPath(compiler)
        if err != nil {
                return cache.ActionID{}, false
        }
index 71f37e8d47bc2307d161e3afab3e925dbf26bb26..84d8c9e35063fb47c77736d0bb75a8e7b7707d18 100644 (file)
@@ -18,6 +18,7 @@ import (
        "cmd/go/internal/fsys"
        "cmd/go/internal/load"
        "cmd/go/internal/str"
+       "cmd/internal/pathcache"
        "cmd/internal/pkgpath"
 )
 
@@ -33,7 +34,7 @@ func init() {
        if GccgoName == "" {
                GccgoName = "gccgo"
        }
-       GccgoBin, gccgoErr = cfg.LookPath(GccgoName)
+       GccgoBin, gccgoErr = pathcache.LookPath(GccgoName)
 }
 
 func (gccgoToolchain) compiler() string {
index 869f6777c7b5b8defa68d19cbaa54f30a2d661fd..6bbd73c05d828d56ff8ba58336fcb32917669436 100644 (file)
@@ -12,6 +12,7 @@ import (
        "cmd/go/internal/load"
        "cmd/go/internal/str"
        "cmd/internal/par"
+       "cmd/internal/pathcache"
        "errors"
        "fmt"
        "internal/lazyregexp"
@@ -606,7 +607,7 @@ func (sh *Shell) runOut(dir string, env []string, cmdargs ...any) ([]byte, error
        }
 
        var buf bytes.Buffer
-       path, err := cfg.LookPath(cmdline[0])
+       path, err := pathcache.LookPath(cmdline[0])
        if err != nil {
                return nil, err
        }
similarity index 96%
rename from src/cmd/go/internal/cfg/lookpath.go
rename to src/cmd/internal/pathcache/lookpath.go
index f095cd6a65723946120542a0d0c2c4e7fc110738..5d1875af535d54e49d3db92a72de99653ec3457e 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package cfg
+package pathcache
 
 import (
        "cmd/internal/par"