Skip to content

Commit 33acfcb

Browse files
authored
Fix regressions for well-known types in LSP (#4186)
1 parent 0f12b3b commit 33acfcb

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44

55
- Disable format on unknown or invalid syntax.
6+
- Fix regression in LSP functionality for well-known types.
67

78
## [v1.60.0] - 2025-11-14
89

private/buf/buflsp/file.go

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (f *file) IndexSymbols(ctx context.Context) {
321321
for _, sym := range unresolved {
322322
switch kind := sym.kind.(type) {
323323
case *reference:
324-
def := f.resolveASTDefinition(kind.def, kind.fullName)
324+
def := f.resolveASTDefinition(ctx, kind.def, kind.fullName)
325325
sym.def = def
326326
if def == nil {
327327
// In the case where the symbol is not resolved, we continue
@@ -339,7 +339,7 @@ func (f *file) IndexSymbols(ctx context.Context) {
339339
}
340340
referenceable.references = append(referenceable.references, sym)
341341
case *option:
342-
def := f.resolveASTDefinition(kind.def, kind.defFullName)
342+
def := f.resolveASTDefinition(ctx, kind.def, kind.defFullName)
343343
sym.def = def
344344
if def != nil {
345345
referenceable, ok := def.kind.(*referenceable)
@@ -354,7 +354,7 @@ func (f *file) IndexSymbols(ctx context.Context) {
354354
referenceable.references = append(referenceable.references, sym)
355355
}
356356
}
357-
typeDef := f.resolveASTDefinition(kind.typeDef, kind.typeDefFullName)
357+
typeDef := f.resolveASTDefinition(ctx, kind.typeDef, kind.typeDefFullName)
358358
sym.typeDef = typeDef
359359
default:
360360
// This shouldn't happen, logging a warning
@@ -756,36 +756,21 @@ func (f *file) messageToSymbolsHelper(msg ir.MessageValue, index int, parents []
756756

757757
// resolveASTDefinition is a helper for resolving the [ast.DeclDef] to the *[symbol], if
758758
// there is a matching indexed *[symbol].
759-
func (f *file) resolveASTDefinition(def ast.DeclDef, defName ir.FullName) *symbol {
760-
// No workspace, we cannot resolve the AST definition
761-
if f.workspace == nil {
762-
return nil
759+
func (f *file) resolveASTDefinition(ctx context.Context, def ast.DeclDef, defName ir.FullName) *symbol {
760+
// First check if the definition is in the current file.
761+
if def.Span().Path() == f.objectInfo.LocalPath() {
762+
return f.referenceableSymbols[defName]
763763
}
764-
// We resolve the import path of the span of the AST definition and search for it in our
765-
// workspace.
766-
fileInfo, ok := f.workspace.fileNameToFileInfo[def.Span().Path()]
767-
if !ok {
768-
// Unable to resolve an importable path for the file in our workspace, log a debug
769-
// statement and return no definition.
770-
f.lsp.logger.Debug(
771-
"unable to resolve an importable file path for local path",
772-
slog.String("file", f.uri.Filename()),
773-
slog.String("local path", def.Span().Path()),
774-
)
764+
// No workspace, we cannot resolve the AST definition from outside of the file.
765+
if f.workspace == nil {
775766
return nil
776767
}
777-
file, ok := f.workspace.PathToFile()[fileInfo.Path()]
778-
if !ok {
779-
// Check current file
780-
if def.Span().Path() != f.objectInfo.Path() {
781-
// If there is no file for the [ast.DeclDef] span, which can if it is a predeclared
782-
// type, for example, or if the file we are checking has not indexed imports, then
783-
// we return nil.
784-
return nil
768+
for _, file := range f.workspace.PathToFile() {
769+
if file.file.Path() == def.Span().Path() {
770+
return file.referenceableSymbols[defName]
785771
}
786-
file = f
787772
}
788-
return file.referenceableSymbols[defName]
773+
return nil
789774
}
790775

791776
// SymbolAt finds a symbol in this file at the given cursor position, if one exists.

private/buf/buflsp/workspace.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package buflsp
1616

1717
import (
1818
"context"
19+
"errors"
1920
"fmt"
21+
"io"
2022
"iter"
2123
"log/slog"
2224

@@ -296,11 +298,11 @@ func (w *workspace) fileInfos(ctx context.Context) iter.Seq[storage.ObjectInfo]
296298
if err := w.lsp.wktBucket.Walk(ctx, "", func(objectInfo storage.ObjectInfo) error {
297299
if _, ok := seen[objectInfo.Path()]; !ok {
298300
if !yield(wktObjectInfo{objectInfo}) {
299-
return nil
301+
return io.EOF
300302
}
301303
}
302304
return nil
303-
}); err != nil {
305+
}); err != nil && !errors.Is(err, io.EOF) {
304306
w.lsp.logger.Error("wkt bucket failed", xslog.ErrorAttr(err))
305307
}
306308
}

0 commit comments

Comments
 (0)