Skip to content

Commit 7825736

Browse files
committed
gopls/internal/lsp/cache: simplify critical errors
The critical error logic was hard to follow, and ill defined because it was constructed in two places: once during initialization, and another time in Snapshot.CriticalError. CriticalError is now rebranded as an InitializationError, and constructed only during snapshot initialization. It covers a load error, and an unparsable go.work or go.mod file. Critical errors are applied to orphaned files. For golang/go#57979 Change-Id: Ib3cdf602954202be0c87594c26dbbd0ff7e6458a Reviewed-on: https://go-review.googlesource.com/c/tools/+/553097 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com>
1 parent 88ea935 commit 7825736

File tree

10 files changed

+142
-299
lines changed

10 files changed

+142
-299
lines changed

gopls/internal/lsp/cache/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (s *Snapshot) resolveImportGraph() (*importGraph, error) {
203203
s.mu.Unlock()
204204

205205
openPackages := make(map[PackageID]bool)
206-
for _, fh := range s.overlays() {
206+
for _, fh := range s.Overlays() {
207207
mps, err := s.MetadataForFile(ctx, fh.URI())
208208
if err != nil {
209209
return nil, err

gopls/internal/lsp/cache/diagnostics.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ import (
1212
"golang.org/x/tools/gopls/internal/util/bug"
1313
)
1414

15-
// A CriticalError is a workspace-wide error that generally prevents gopls from
16-
// functioning correctly. In the presence of critical errors, other diagnostics
17-
// in the workspace may not make sense.
18-
type CriticalError struct {
15+
// A InitializationError is an error that causes snapshot initialization to fail.
16+
// It is either the error returned from go/packages.Load, or an error parsing a
17+
// workspace go.work or go.mod file.
18+
//
19+
// Such an error generally indicates that the View is malformed, and will never
20+
// be usable.
21+
type InitializationError struct {
1922
// MainError is the primary error. Must be non-nil.
2023
MainError error
2124

22-
// Diagnostics contains any supplemental (structured) diagnostics.
25+
// Diagnostics contains any supplemental (structured) diagnostics extracted
26+
// from the load error.
2327
Diagnostics map[protocol.DocumentURI][]*Diagnostic
2428
}
2529

gopls/internal/lsp/cache/load.go

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -297,82 +297,6 @@ func (m *moduleErrorMap) Error() string {
297297
return buf.String()
298298
}
299299

300-
// workspaceLayoutError returns an error describing a misconfiguration of the
301-
// workspace, along with related diagnostic.
302-
//
303-
// The unusual argument ordering of results is intentional: if the resulting
304-
// error is nil, so must be the resulting diagnostics.
305-
//
306-
// If ctx is cancelled, it may return ctx.Err(), nil.
307-
//
308-
// TODO(rfindley): separate workspace diagnostics from critical workspace
309-
// errors.
310-
func (s *Snapshot) workspaceLayoutError(ctx context.Context) (error, []*Diagnostic) {
311-
// TODO(rfindley): both of the checks below should be delegated to the workspace.
312-
313-
if s.view.adjustedGO111MODULE() == "off" {
314-
return nil, nil
315-
}
316-
317-
// If the user is using a go.work file, assume that they know what they are
318-
// doing.
319-
//
320-
// TODO(golang/go#53880): improve orphaned file diagnostics when using go.work.
321-
if s.view.typ == GoWorkView {
322-
return nil, nil
323-
}
324-
325-
// Apply diagnostics about the workspace configuration to relevant open
326-
// files.
327-
openFiles := s.overlays()
328-
329-
// If the snapshot does not have a valid build configuration, it may be
330-
// that the user has opened a directory that contains multiple modules.
331-
// Check for that an warn about it.
332-
if s.view.typ == AdHocView {
333-
msg := `gopls was not able to find modules in your workspace.
334-
When outside of GOPATH, gopls needs to know which modules you are working on.
335-
You can fix this by opening your workspace to a folder inside a Go module, or
336-
by using a go.work file to specify multiple modules.
337-
See the documentation for more information on setting up your workspace:
338-
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.`
339-
return fmt.Errorf(msg), s.applyCriticalErrorToFiles(ctx, msg, openFiles)
340-
}
341-
342-
return nil, nil
343-
}
344-
345-
func (s *Snapshot) applyCriticalErrorToFiles(ctx context.Context, msg string, files []*Overlay) []*Diagnostic {
346-
var srcDiags []*Diagnostic
347-
for _, fh := range files {
348-
// Place the diagnostics on the package or module declarations.
349-
var rng protocol.Range
350-
switch s.FileKind(fh) {
351-
case file.Go:
352-
if pgf, err := s.ParseGo(ctx, fh, ParseHeader); err == nil {
353-
// Check that we have a valid `package foo` range to use for positioning the error.
354-
if pgf.File.Package.IsValid() && pgf.File.Name != nil && pgf.File.Name.End().IsValid() {
355-
rng, _ = pgf.PosRange(pgf.File.Package, pgf.File.Name.End())
356-
}
357-
}
358-
case file.Mod:
359-
if pmf, err := s.ParseMod(ctx, fh); err == nil {
360-
if mod := pmf.File.Module; mod != nil && mod.Syntax != nil {
361-
rng, _ = pmf.Mapper.OffsetRange(mod.Syntax.Start.Byte, mod.Syntax.End.Byte)
362-
}
363-
}
364-
}
365-
srcDiags = append(srcDiags, &Diagnostic{
366-
URI: fh.URI(),
367-
Range: rng,
368-
Severity: protocol.SeverityError,
369-
Source: ListError,
370-
Message: msg,
371-
})
372-
}
373-
return srcDiags
374-
}
375-
376300
// buildMetadata populates the updates map with metadata updates to
377301
// apply, based on the given pkg. It recurs through pkg.Imports to ensure that
378302
// metadata exists for all dependencies.

gopls/internal/lsp/cache/mod_tidy.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,6 @@ func (s *Snapshot) ModTidy(ctx context.Context, pm *ParsedModule) (*TidiedModule
7272
}
7373
}
7474

75-
if criticalErr := s.CriticalError(ctx); criticalErr != nil {
76-
return &TidiedModule{
77-
Diagnostics: criticalErr.Diagnostics[fh.URI()],
78-
}, nil
79-
}
80-
if ctx.Err() != nil { // must check ctx after GetCriticalError
81-
return nil, ctx.Err()
82-
}
83-
8475
if err := s.awaitLoaded(ctx); err != nil {
8576
return nil, err
8677
}

0 commit comments

Comments
 (0)