Skip to content

Commit c457787

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/cache: avoid reporting bugs when go/packages has errors
When go/packages.Load returns an error, or has packages with errors, we should not assume that assumptions of the resulting packages still hold. I believe this is the cause of the two telemetry bug reports below. Fixes golang/go#66204 Fixes golang/go#69895 Change-Id: I9beab020ddb37d36a8826cb36a576990463d7bce Reviewed-on: https://go-review.googlesource.com/c/tools/+/621859 Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 401eca0 commit c457787

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

gopls/internal/cache/load.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ func (s *Snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc
156156
}
157157
}
158158

159+
if err != nil {
160+
return fmt.Errorf("packages.Load error: %w", err)
161+
}
162+
159163
if standalone {
160164
// Handle standalone package result.
161165
//
@@ -173,20 +177,29 @@ func (s *Snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc
173177
if s.view.typ == GoPackagesDriverView {
174178
errorf = fmt.Errorf // all bets are off
175179
}
180+
for _, pkg := range pkgs {
181+
// Don't report bugs if any packages have errors.
182+
// For example: given go list errors, go/packages may synthesize a
183+
// package with ID equal to the query.
184+
if len(pkg.Errors) > 0 {
185+
errorf = fmt.Errorf
186+
break
187+
}
188+
}
176189

177190
var standalonePkg *packages.Package
178191
for _, pkg := range pkgs {
179192
if pkg.ID == "command-line-arguments" {
180193
if standalonePkg != nil {
181-
return errorf("internal error: go/packages returned multiple standalone packages")
194+
return errorf("go/packages returned multiple standalone packages")
182195
}
183196
standalonePkg = pkg
184197
} else if packagesinternal.GetForTest(pkg) == "" && !strings.HasSuffix(pkg.ID, ".test") {
185-
return errorf("internal error: go/packages returned unexpected package %q for standalone file", pkg.ID)
198+
return errorf("go/packages returned unexpected package %q for standalone file", pkg.ID)
186199
}
187200
}
188201
if standalonePkg == nil {
189-
return errorf("internal error: go/packages failed to return non-test standalone package")
202+
return errorf("go/packages failed to return non-test standalone package")
190203
}
191204
if len(standalonePkg.CompiledGoFiles) > 0 {
192205
pkgs = []*packages.Package{standalonePkg}
@@ -196,10 +209,7 @@ func (s *Snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc
196209
}
197210

198211
if len(pkgs) == 0 {
199-
if err == nil {
200-
err = errNoPackages
201-
}
202-
return fmt.Errorf("packages.Load error: %w", err)
212+
return fmt.Errorf("packages.Load error: %w", errNoPackages)
203213
}
204214

205215
moduleErrs := make(map[string][]packages.Error) // module path -> errors

0 commit comments

Comments
 (0)