Skip to content

Commit feffeaa

Browse files
xieyuschenGo LUCI
authored and
Go LUCI
committed
go/packages: report an error from Load when GOROOT is misconfigured
Load should return an error when 'go list' command returns the following error: err: exit status 2: stderr: go: no such tool "compile" This occurs when internally running `go list` on a misconfigured GOROOT directory. Fixes golang/go#69606 Change-Id: Ib7bf58293612e26aa33d2cf8230378747ad01820 Reviewed-on: https://go-review.googlesource.com/c/tools/+/615396 Reviewed-by: Michael Podtserkovskii <michaelpo@meta.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Commit-Queue: Tim King <taking@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Tim King <taking@google.com> Reviewed-by: Michael Matloob <matloob@golang.org>
1 parent 50179f2 commit feffeaa

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

go/packages/golist.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,12 @@ func (state *golistState) invokeGo(verb string, args ...string) (*bytes.Buffer,
879879
return nil, friendlyErr
880880
}
881881

882+
// Return an error if 'go list' failed due to missing tools in
883+
// $GOROOT/pkg/tool/$GOOS_$GOARCH (#69606).
884+
if len(stderr.String()) > 0 && strings.Contains(stderr.String(), `go: no such tool`) {
885+
return nil, friendlyErr
886+
}
887+
882888
// Is there an error running the C compiler in cgo? This will be reported in the "Error" field
883889
// and should be suppressed by go list -e.
884890
//

go/packages/packages_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ import (
2323
"sort"
2424
"strings"
2525
"testing"
26+
"testing/fstest"
2627
"time"
2728

2829
"golang.org/x/tools/go/packages"
2930
"golang.org/x/tools/go/packages/packagestest"
3031
"golang.org/x/tools/internal/packagesinternal"
3132
"golang.org/x/tools/internal/testenv"
33+
"golang.org/x/tools/internal/testfiles"
3234
)
3335

3436
// testCtx is canceled when the test binary is about to time out.
@@ -3103,3 +3105,74 @@ func TestLoadOverlayGoMod(t *testing.T) {
31033105
t.Errorf("Load: got %s, want %v", got, want)
31043106
}
31053107
}
3108+
3109+
func overlayFS(overlay map[string][]byte) fstest.MapFS {
3110+
fs := make(fstest.MapFS)
3111+
for name, data := range overlay {
3112+
fs[name] = &fstest.MapFile{Data: data}
3113+
}
3114+
return fs
3115+
}
3116+
3117+
// TestIssue69606a tests when tools in $GOROOT/pkg/tool/$GOOS_$GOARCH are missing,
3118+
// Load should return an error.
3119+
func TestIssue69606a(t *testing.T) {
3120+
testenv.NeedsTool(t, "go")
3121+
overlay := overlayFS(map[string][]byte{
3122+
"io/io.go": []byte("package io"),
3123+
"unsafe/unsafe.go": []byte("package unsafe"),
3124+
})
3125+
goroot := testfiles.CopyToTmp(t, overlay)
3126+
3127+
t.Logf("custom GOROOT: %s", goroot)
3128+
3129+
// load the std packages under a custom GOROOT
3130+
_, err := packages.Load(&packages.Config{
3131+
Mode: packages.NeedName |
3132+
packages.NeedFiles |
3133+
packages.NeedImports |
3134+
packages.NeedTypes,
3135+
Env: append(
3136+
os.Environ(),
3137+
"GO111MODULES=on",
3138+
"GOPATH=",
3139+
"GOWORK=off",
3140+
"GOPROXY=off",
3141+
fmt.Sprintf("GOROOT=%s", goroot)),
3142+
}, "std")
3143+
3144+
if err == nil {
3145+
t.Fatal("Expected to get an error because missing tool 'compile' but got a nil error")
3146+
}
3147+
}
3148+
3149+
// TestIssue69606b tests when loading std from a fake goroot without a unsafe package,
3150+
// Load should return an error.
3151+
func TestIssue69606b(t *testing.T) {
3152+
testenv.NeedsTool(t, "go")
3153+
overlay := overlayFS(map[string][]byte{
3154+
"io/io.go": []byte("package io"),
3155+
})
3156+
goroot := testfiles.CopyToTmp(t, overlay)
3157+
3158+
t.Logf("custom GOROOT: %s", goroot)
3159+
3160+
// load the std packages under a custom GOROOT
3161+
_, err := packages.Load(&packages.Config{
3162+
Mode: packages.NeedName |
3163+
packages.NeedFiles |
3164+
packages.NeedImports |
3165+
packages.NeedTypes,
3166+
Env: append(
3167+
os.Environ(),
3168+
"GO111MODULES=on",
3169+
"GOPATH=",
3170+
"GOWORK=off",
3171+
"GOPROXY=off",
3172+
fmt.Sprintf("GOROOT=%s", goroot)),
3173+
}, "std")
3174+
3175+
if err == nil {
3176+
t.Fatal("Expected to get an error because missing unsafe package but got a nil error")
3177+
}
3178+
}

0 commit comments

Comments
 (0)