Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions internal/goroot/importcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
3 changes: 3 additions & 0 deletions internal/imports/sourcex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down