Skip to content

Commit bb41ff4

Browse files
authored
Evict queries.File with ReportError set to both true and false (#4257)
1 parent 7b6a021 commit bb41ff4

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
buf.build/go/standard v0.1.0
1717
connectrpc.com/connect v1.19.1
1818
connectrpc.com/otelconnect v0.8.0
19-
github.com/bufbuild/protocompile v0.14.2-0.20251223142729-db46c1b9d34e
19+
github.com/bufbuild/protocompile v0.14.2-0.20260105175043-4d8d90b1c6b8
2020
github.com/bufbuild/protoplugin v0.0.0-20250218205857-750e09ce93e1
2121
github.com/cli/browser v1.3.0
2222
github.com/docker/docker v28.5.2+incompatible

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ github.com/bmatcuk/doublestar/v4 v4.9.1 h1:X8jg9rRZmJd4yRy7ZeNDRnM+T3ZfHv15JiBJ/
4242
github.com/bmatcuk/doublestar/v4 v4.9.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
4343
github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4=
4444
github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs=
45-
github.com/bufbuild/protocompile v0.14.2-0.20251223142729-db46c1b9d34e h1:LQA+1MyiPkolGHJGC2GMDC5Xu+0RDVH6jGMKech7Exs=
46-
github.com/bufbuild/protocompile v0.14.2-0.20251223142729-db46c1b9d34e/go.mod h1:5UUj46Eu+U+C59C5N6YilaMI7WWfP2bW9xGcOkme2DI=
45+
github.com/bufbuild/protocompile v0.14.2-0.20260105175043-4d8d90b1c6b8 h1:cQYwUyAzyMmYr7AyJU1C6pVCpUrJJBkmx7UunZosxxs=
46+
github.com/bufbuild/protocompile v0.14.2-0.20260105175043-4d8d90b1c6b8/go.mod h1:5UUj46Eu+U+C59C5N6YilaMI7WWfP2bW9xGcOkme2DI=
4747
github.com/bufbuild/protoplugin v0.0.0-20250218205857-750e09ce93e1 h1:V1xulAoqLqVg44rY97xOR+mQpD2N+GzhMHVwJ030WEU=
4848
github.com/bufbuild/protoplugin v0.0.0-20250218205857-750e09ce93e1/go.mod h1:c5D8gWRIZ2HLWO3gXYTtUfw/hbJyD8xikv2ooPxnklQ=
4949
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=

private/buf/buflsp/completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@ func offsetInSpan(offset int, span source.Span) int {
15751575
if offset < span.Start {
15761576
return -1
15771577
} else if offset > span.End {
1578-
// End is inclusive for completions _
1578+
// End is inclusive for completions
15791579
return 1
15801580
}
15811581
return 0

private/buf/buflsp/file.go

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ type file struct {
6262
objectInfo storage.ObjectInfo // Info in the context of the workspace.
6363

6464
ir *ir.File
65-
irQuery queries.IR
66-
fileQuery queries.File
6765
referenceableSymbols map[ir.FullName]*symbol
6866
referenceSymbols []*symbol
6967
symbols []*symbol
@@ -101,9 +99,7 @@ func (f *file) Reset(ctx context.Context) {
10199
}
102100
// Evict the query key if there is a query cached on the file. We cache the [queries.File]
103101
// query since this allows the executor to evict all dependent queries, e.g. AST and IR.
104-
if f.fileQuery.Path != "" {
105-
f.lsp.queryExecutor.Evict(f.fileQuery.Key())
106-
}
102+
f.lsp.queryExecutor.Evict(f.queryFileKeys()...)
107103
// Clear the file as nothing should use it after reset. This asserts that.
108104
*f = file{}
109105
}
@@ -245,21 +241,9 @@ func (f *file) RefreshIR(ctx context.Context) {
245241
// update the opener and set a new query.
246242
if current == nil || current.Text() != file.file.Text() {
247243
openerMap[path] = file.file
248-
// Add the query key for eviction if there is a query cached on the file. We cache
249-
// the [queries.File] query since this allows the executor to evict all dependent
250-
// queries, e.g. AST and IR.
251-
if file.fileQuery.Path != "" {
252-
evictQueryKeys = append(evictQueryKeys, file.fileQuery.Key())
253-
}
254-
file.fileQuery = queries.File{
255-
Opener: file.lsp.opener,
256-
Path: path,
257-
ReportError: true, // [queries.AST] sets this to be true.
258-
}
259-
file.irQuery = queries.IR{
260-
Opener: file.lsp.opener,
261-
Path: file.objectInfo.Path(),
262-
Session: file.lsp.irSession,
244+
if current != nil {
245+
// This indicates the file content has changed, so we evict the query file keys.
246+
evictQueryKeys = append(evictQueryKeys, file.queryFileKeys()...)
263247
}
264248
}
265249
files = append(files, file)
@@ -273,7 +257,7 @@ func (f *file) RefreshIR(ctx context.Context) {
273257
f.lsp.queryExecutor.Evict(evictQueryKeys...)
274258

275259
queries := xslices.Map(files, func(file *file) incremental.Query[*ir.File] {
276-
return file.irQuery
260+
return file.queryIR()
277261
})
278262

279263
results, diagnosticReport, err := incremental.Run(
@@ -319,6 +303,40 @@ func (f *file) RefreshIR(ctx context.Context) {
319303
)
320304
}
321305

306+
// queryIR returns the [queries.IR] for the current file.
307+
func (f *file) queryIR() incremental.Query[*ir.File] {
308+
if f.objectInfo == nil {
309+
return nil
310+
}
311+
return queries.IR{
312+
Opener: f.lsp.opener,
313+
Path: f.objectInfo.Path(),
314+
Session: f.lsp.irSession,
315+
}
316+
}
317+
318+
// queryFileKeys returns the keys for [queries.File] with ReportError set to true and false.
319+
// This is because [queries.AST] depends on ReportError set to true and [queries.IR] depends
320+
// on ReportError set to false, so we need both keys when evicting cached [queries.File]
321+
// results for stale file states.
322+
func (f *file) queryFileKeys() []any {
323+
if f.objectInfo == nil {
324+
return nil
325+
}
326+
return []any{
327+
queries.File{
328+
Opener: f.lsp.opener,
329+
Path: f.objectInfo.Path(),
330+
ReportError: true,
331+
}.Key(),
332+
queries.File{
333+
Opener: f.lsp.opener,
334+
Path: f.objectInfo.Path(),
335+
ReportError: false,
336+
}.Key(),
337+
}
338+
}
339+
322340
// IndexSymbols processes the IR of a file and generates symbols for each symbol in
323341
// the document.
324342
//

0 commit comments

Comments
 (0)