diff --git a/internal/goroot/importcfg.go b/internal/goroot/importcfg.go index 167360dc4c5..1dc348f85fa 100644 --- a/internal/goroot/importcfg.go +++ b/internal/goroot/importcfg.go @@ -16,37 +16,31 @@ import ( "sync" ) -var ( - stdlibPkgfileMap map[string]string - stdlibPkgfileErr error - once sync.Once -) - // PkgfileMap returns a map of package paths to the location on disk // of the .a file for the package. // The caller must not modify the map. func PkgfileMap() (map[string]string, error) { - once.Do(func() { - m := make(map[string]string) - output, err := exec.Command("go", "list", "-export", "-e", "-f", "{{.ImportPath}} {{.Export}}", "std", "cmd").Output() - if err != nil { - stdlibPkgfileErr = err + return pkgFileMapOnce() +} + +var pkgFileMapOnce = sync.OnceValues(func() (map[string]string, error) { + m := make(map[string]string) + output, err := exec.Command("go", "list", "-export", "-e", "-f", "{{.ImportPath}} {{.Export}}", "std", "cmd").Output() + if err != nil { + return m, err + } + for line := range strings.SplitSeq(string(output), "\n") { + if line == "" { + continue } - for line := range strings.SplitSeq(string(output), "\n") { - if line == "" { - continue - } - sp := strings.SplitN(line, " ", 2) - if len(sp) != 2 { - err = fmt.Errorf("determining pkgfile map: invalid line in go list output: %q", line) - return - } - importPath, export := sp[0], sp[1] - if export != "" { - m[importPath] = export - } + sp := strings.SplitN(line, " ", 2) + if len(sp) != 2 { + return m, fmt.Errorf("determining pkgfile map: invalid line in go list output: %q", line) } - stdlibPkgfileMap = m - }) - return stdlibPkgfileMap, stdlibPkgfileErr -} + importPath, export := sp[0], sp[1] + if export != "" { + m[importPath] = export + } + } + return m, nil +}) diff --git a/internal/imports/sourcex_test.go b/internal/imports/sourcex_test.go index ed3e8f3418c..c8e9a4985cc 100644 --- a/internal/imports/sourcex_test.go +++ b/internal/imports/sourcex_test.go @@ -78,6 +78,9 @@ func TestSource(t *testing.T) { opts := imports.Options{} // ApplyFixes needs a non-nil opts got, err := imports.ApplyFixes(fixes, "tfile.go", []byte(fx), &opts, 0) + if err != nil { + t.Fatal(err) + } fxwant := "package main\n\nimport \"bar.com/foo\"\n\nvar _ = foo.Foo\nvar _ = foo.Bar\n" if diff := cmp.Diff(string(got), fxwant); diff != "" {