Skip to content

Commit 839fe65

Browse files
committed
internal/lsp: improve error handling while parsing
It the context is canceled (or times out) during parsing, we were previously caching the package with no *ast.Files. Any further LSP queries against that package would fail because the package is already loaded, but none of the files are mapped to the package. Fix by checking explicitly for context errors in parseFiles and propagating them instead of continuing.
1 parent 5aed782 commit 839fe65

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

internal/lsp/cache/check.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,18 @@ func (imp *importer) typeCheck(pkgPath string) (*pkg, error) {
103103
// Don't type-check function bodies if we are not in the top-level package.
104104
files, errs := imp.parseFiles(meta.files, imp.ignoreFuncBodies(pkg.pkgPath))
105105
for _, err := range errs {
106+
// Don't cache the package if we didn't finish parsing the files.
107+
if err == context.Canceled || err == context.DeadlineExceeded {
108+
return nil, err
109+
}
106110
appendError(err)
107111
}
112+
113+
// If something unexpected happens, don't cache a package with 0 parsed files.
114+
if len(files) == 0 {
115+
return nil, fmt.Errorf("no parsed files for package %s", pkg.pkgPath)
116+
}
117+
108118
pkg.syntax = files
109119

110120
// Handle circular imports by copying previously seen imports.

internal/lsp/cache/load.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ func (v *view) loadParseTypecheck(ctx context.Context, f *goFile) ([]packages.Er
4242
}
4343
// Type-check package.
4444
pkg, err := imp.getPkg(f.meta.pkgPath)
45-
if pkg == nil || pkg.IsIllTyped() {
45+
if err != nil {
4646
return nil, err
4747
}
48+
if pkg == nil || pkg.IsIllTyped() {
49+
return nil, fmt.Errorf("loadParseTypecheck: %s is ill typed", f.meta.pkgPath)
50+
}
4851
// If we still have not found the package for the file, something is wrong.
4952
if f.pkg == nil {
5053
return nil, fmt.Errorf("parse: no package found for %v", f.filename())

internal/lsp/cache/parse.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (imp *importer) parseFiles(filenames []string, ignoreFuncBodies bool) ([]*a
7676
// We don't have a cached AST for this file, so we read its content and parse it.
7777
data, _, err := gof.Handle(imp.ctx).Read(imp.ctx)
7878
if err != nil {
79+
errors[i] = err
7980
return
8081
}
8182
src := data

0 commit comments

Comments
 (0)