Skip to content

Commit

Permalink
gopls/internal/cache: avoid reporting bugs when go/packages has errors
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
findleyr authored and gopherbot committed Oct 22, 2024
1 parent 401eca0 commit c457787
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions gopls/internal/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ func (s *Snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc
}
}

if err != nil {
return fmt.Errorf("packages.Load error: %w", err)
}

if standalone {
// Handle standalone package result.
//
Expand All @@ -173,20 +177,29 @@ func (s *Snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc
if s.view.typ == GoPackagesDriverView {
errorf = fmt.Errorf // all bets are off
}
for _, pkg := range pkgs {
// Don't report bugs if any packages have errors.
// For example: given go list errors, go/packages may synthesize a
// package with ID equal to the query.
if len(pkg.Errors) > 0 {
errorf = fmt.Errorf
break
}
}

var standalonePkg *packages.Package
for _, pkg := range pkgs {
if pkg.ID == "command-line-arguments" {
if standalonePkg != nil {
return errorf("internal error: go/packages returned multiple standalone packages")
return errorf("go/packages returned multiple standalone packages")
}
standalonePkg = pkg
} else if packagesinternal.GetForTest(pkg) == "" && !strings.HasSuffix(pkg.ID, ".test") {
return errorf("internal error: go/packages returned unexpected package %q for standalone file", pkg.ID)
return errorf("go/packages returned unexpected package %q for standalone file", pkg.ID)
}
}
if standalonePkg == nil {
return errorf("internal error: go/packages failed to return non-test standalone package")
return errorf("go/packages failed to return non-test standalone package")
}
if len(standalonePkg.CompiledGoFiles) > 0 {
pkgs = []*packages.Package{standalonePkg}
Expand All @@ -196,10 +209,7 @@ func (s *Snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc
}

if len(pkgs) == 0 {
if err == nil {
err = errNoPackages
}
return fmt.Errorf("packages.Load error: %w", err)
return fmt.Errorf("packages.Load error: %w", errNoPackages)
}

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

0 comments on commit c457787

Please sign in to comment.