Skip to content

Commit f560bc8

Browse files
committed
internal/lsp/cache: don't set context cancellation as a critical err
Fix a tricky race in gopls/internal/regtest/diagnostics.Test_Issue38211: When reloading the workspace, we can encounter context cancellation if the snapshot is cancelled, and can write this cancellation as a critical error *before* the context is cloned, leading to a state where there is a critical error that won't go away. This should resolve test flakes reported in golang/go#44098. For golang/go#44098 Change-Id: I41c0f49b2fe999131f4c31166e69b2cde85470b7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/419502 gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 8ea5687 commit f560bc8

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

internal/lsp/cache/view.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -656,9 +656,14 @@ func (s *snapshot) loadWorkspace(ctx context.Context, firstAttempt bool) {
656656
}
657657
}()
658658

659-
// If we have multiple modules, we need to load them by paths.
660-
var scopes []interface{}
661-
var modDiagnostics []*source.Diagnostic
659+
// TODO(rFindley): we should only locate template files on the first attempt,
660+
// or guard it via a different mechanism.
661+
s.locateTemplateFiles(ctx)
662+
663+
// Collect module paths to load by parsing go.mod files. If a module fails to
664+
// parse, capture the parsing failure as a critical diagnostic.
665+
var scopes []interface{} // scopes to load
666+
var modDiagnostics []*source.Diagnostic // diagnostics for broken go.mod files
662667
addError := func(uri span.URI, err error) {
663668
modDiagnostics = append(modDiagnostics, &source.Diagnostic{
664669
URI: uri,
@@ -668,20 +673,22 @@ func (s *snapshot) loadWorkspace(ctx context.Context, firstAttempt bool) {
668673
})
669674
}
670675

671-
// TODO(rFindley): we should only locate template files on the first attempt,
672-
// or guard it via a different mechanism.
673-
s.locateTemplateFiles(ctx)
674-
675676
if len(s.workspace.getActiveModFiles()) > 0 {
676677
for modURI := range s.workspace.getActiveModFiles() {
678+
// Be careful not to add context cancellation errors as critical module
679+
// errors.
677680
fh, err := s.GetFile(ctx, modURI)
678681
if err != nil {
679-
addError(modURI, err)
682+
if ctx.Err() == nil {
683+
addError(modURI, err)
684+
}
680685
continue
681686
}
682687
parsed, err := s.ParseMod(ctx, fh)
683688
if err != nil {
684-
addError(modURI, err)
689+
if ctx.Err() == nil {
690+
addError(modURI, err)
691+
}
685692
continue
686693
}
687694
if parsed.File == nil || parsed.File.Module == nil {

0 commit comments

Comments
 (0)