Skip to content

Commit c1eaea2

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

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

internal/goroot/importcfg.go

Lines changed: 21 additions & 27 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-
)
19+
var stdlibPkgfileMap, stdlibPkgfileErr = sync.OnceValues(func() (map[string]string, error) {
20+
m := make(map[string]string)
21+
output, err := exec.Command("go", "list", "-export", "-e", "-f", "{{.ImportPath}} {{.Export}}", "std", "cmd").Output()
22+
if err != nil {
23+
return m, err
24+
}
25+
for line := range strings.SplitSeq(string(output), "\n") {
26+
if line == "" {
27+
continue
28+
}
29+
sp := strings.SplitN(line, " ", 2)
30+
if len(sp) != 2 {
31+
return m, fmt.Errorf("determining pkgfile map: invalid line in go list output: %q", line)
32+
}
33+
importPath, export := sp[0], sp[1]
34+
if export != "" {
35+
m[importPath] = export
36+
}
37+
}
38+
return m, nil
39+
})()
2440

2541
// PkgfileMap returns a map of package paths to the location on disk
2642
// of the .a file for the package.
2743
// The caller must not modify the map.
2844
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
34-
}
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-
}
48-
}
49-
stdlibPkgfileMap = m
50-
})
5145
return stdlibPkgfileMap, stdlibPkgfileErr
5246
}

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)