Skip to content

Commit 6c10975

Browse files
committed
internal/lsp/cache: honor RunDespiteErrors=false
This change prevents analysis from running on a package containing any kind of error unless the analyzer has indicated that it is robust. This is presumed to be the cause of several panics in production. And a test. Updates golang/go#54762 Change-Id: I9327e18ef8d7773c943ea45fc786991188358131 Reviewed-on: https://go-review.googlesource.com/c/tools/+/426803 Run-TryBot: Alan Donovan <[email protected]> Auto-Submit: Alan Donovan <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 49ab44d commit 6c10975

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

internal/lsp/cache/analysis.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ func actionImpl(ctx context.Context, snapshot *snapshot, deps []*actionHandle, a
329329
}
330330
analysisinternal.SetTypeErrors(pass, pkg.typeErrors)
331331

332-
// TODO(adonovan): fix: don't run analyzers on packages
333-
// containing type errors unless the analyzers have
334-
// RunDespiteErrors=true.
332+
if (pkg.HasListOrParseErrors() || pkg.HasTypeErrors()) && !analyzer.RunDespiteErrors {
333+
return nil, fmt.Errorf("skipping analysis %s because package %s contains errors", analyzer.Name, pkg.ID())
334+
}
335335

336336
// Recover from panics (only) within the analyzer logic.
337337
// (Use an anonymous function to limit the recover scope.)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package rundespiteerrors
2+
3+
// This test verifies that analyzers without RunDespiteErrors are not
4+
// executed on a package containing type errors (see issue #54762).
5+
func _() {
6+
// A type error.
7+
_ = 1 + "" //@diag("1", "compiler", "mismatched types|cannot convert", "error")
8+
9+
// A violation of an analyzer for which RunDespiteErrors=false:
10+
// no diagnostic is produced; the diag comment is merely illustrative.
11+
for _ = range "" { //diag("for _", "simplifyrange", "simplify range expression", "warning")
12+
13+
}
14+
}

0 commit comments

Comments
 (0)