Skip to content

Commit 77c16e6

Browse files
committed
internal: fix unused errors reported by ineffassign
Updates #76704
1 parent c3feb70 commit 77c16e6

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

internal/goroot/importcfg.go

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,31 @@ import (
1616
"sync"
1717
)
1818

19-
var (
20-
stdlibPkgfileMap map[string]string
21-
stdlibPkgfileErr error
22-
once sync.Once
23-
)
24-
2519
// PkgfileMap returns a map of package paths to the location on disk
2620
// of the .a file for the package.
2721
// The caller must not modify the map.
2822
func PkgfileMap() (map[string]string, error) {
29-
once.Do(func() {
30-
m := make(map[string]string)
31-
output, err := exec.Command("go", "list", "-export", "-e", "-f", "{{.ImportPath}} {{.Export}}", "std", "cmd").Output()
32-
if err != nil {
33-
stdlibPkgfileErr = err
23+
return pkgFileMapOnce()
24+
}
25+
26+
var pkgFileMapOnce = sync.OnceValues(func() (map[string]string, error) {
27+
m := make(map[string]string)
28+
output, err := exec.Command("go", "list", "-export", "-e", "-f", "{{.ImportPath}} {{.Export}}", "std", "cmd").Output()
29+
if err != nil {
30+
return m, err
31+
}
32+
for line := range strings.SplitSeq(string(output), "\n") {
33+
if line == "" {
34+
continue
3435
}
35-
for line := range strings.SplitSeq(string(output), "\n") {
36-
if line == "" {
37-
continue
38-
}
39-
sp := strings.SplitN(line, " ", 2)
40-
if len(sp) != 2 {
41-
err = fmt.Errorf("determining pkgfile map: invalid line in go list output: %q", line)
42-
return
43-
}
44-
importPath, export := sp[0], sp[1]
45-
if export != "" {
46-
m[importPath] = export
47-
}
36+
sp := strings.SplitN(line, " ", 2)
37+
if len(sp) != 2 {
38+
return m, fmt.Errorf("determining pkgfile map: invalid line in go list output: %q", line)
4839
}
49-
stdlibPkgfileMap = m
50-
})
51-
return stdlibPkgfileMap, stdlibPkgfileErr
52-
}
40+
importPath, export := sp[0], sp[1]
41+
if export != "" {
42+
m[importPath] = export
43+
}
44+
}
45+
return m, nil
46+
})

internal/imports/sourcex_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func TestSource(t *testing.T) {
7878
opts := imports.Options{}
7979
// ApplyFixes needs a non-nil opts
8080
got, err := imports.ApplyFixes(fixes, "tfile.go", []byte(fx), &opts, 0)
81+
if err != nil {
82+
t.Fatal(err)
83+
}
8184

8285
fxwant := "package main\n\nimport \"bar.com/foo\"\n\nvar _ = foo.Foo\nvar _ = foo.Bar\n"
8386
if diff := cmp.Diff(string(got), fxwant); diff != "" {

0 commit comments

Comments
 (0)