From ec14ead7f2abc20ae75c680ca2c569bb38790baf Mon Sep 17 00:00:00 2001 From: Frederic Ruget Date: Mon, 23 Nov 2026 22:10:39 +0000 Subject: [PATCH 1/2] Index a file's own options in the LSP Go-to-definition and references found nothing for a custom file option, such as option (my.file_option) = "x";, while they work for a message's or a field's options. indexSymbols indexes each symbol's options, but the file itself is not a symbol, so its options were never reached. Index f.ir.Options() there, the same way. --- CHANGELOG.md | 3 + private/buf/buflsp/definition_test.go | 59 +++++++++++++++++++ private/buf/buflsp/file.go | 3 + .../testdata/definition/file_options.proto | 11 ++++ .../buflsp/testdata/definition/options.proto | 13 ++++ 5 files changed, 89 insertions(+) create mode 100644 private/buf/buflsp/testdata/definition/file_options.proto create mode 100644 private/buf/buflsp/testdata/definition/options.proto diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b43e6f6d2..e4b73f82ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## [Unreleased] +- Fix LSP go-to-definition and references for custom file options, such as + `option (my.file_option) = "x";`: a file's own options were never indexed, unlike a message's or + a field's. - Fix the `PROTOVALIDATE` lint rule to check constant arguments to `duration`, `timestamp`, and `matches` in CEL expressions, such as `duration('1d')` or an invalid regex, which otherwise only fail when the rules are evaluated. diff --git a/private/buf/buflsp/definition_test.go b/private/buf/buflsp/definition_test.go index 0421d7b8c5..0acfaf00a7 100644 --- a/private/buf/buflsp/definition_test.go +++ b/private/buf/buflsp/definition_test.go @@ -208,6 +208,65 @@ func TestDefinition(t *testing.T) { } } +// TestDefinitionFileOption checks that a file's own options are indexed, as +// a message's or a field's are: go-to-definition on a custom file option +// reaches its extension's declaration. +func TestDefinitionFileOption(t *testing.T) { + t.Parallel() + + ctx := t.Context() + + testProtoPath, err := filepath.Abs("testdata/definition/file_options.proto") + require.NoError(t, err) + optionsProtoPath, err := filepath.Abs("testdata/definition/options.proto") + require.NoError(t, err) + + clientJSONConn, testURI := setupLSPServer(t, testProtoPath) + optionsURI := buflsp.FilePathToURI(optionsProtoPath) + + tests := []struct { + name string + line uint32 + character uint32 + expectedDefLine uint32 + }{ + { + name: "definition_of_file_option", + line: 6, // Line with "option (definition.v1.file_tag) = "f";" + character: 22, // On "file_tag" + expectedDefLine: 7, // string file_tag = 50001; + }, + { + name: "definition_of_field_option", + line: 9, // Line with "string x = 1 [(definition.v1.field_tag) = "g"];" + character: 31, // On "field_tag" + expectedDefLine: 11, // string field_tag = 50002; + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + var locations []protocol.Location + _, defErr := clientJSONConn.Call(ctx, protocol.MethodTextDocumentDefinition, protocol.DefinitionParams{ + TextDocumentPositionParams: protocol.TextDocumentPositionParams{ + TextDocument: protocol.TextDocumentIdentifier{ + URI: testURI, + }, + Position: protocol.Position{ + Line: tt.line, + Character: tt.character, + }, + }, + }, &locations) + require.NoError(t, defErr) + require.Len(t, locations, 1, "expected exactly one definition location") + assert.Equal(t, optionsURI, locations[0].URI) + assert.Equal(t, tt.expectedDefLine, locations[0].Range.Start.Line) + }) + } +} + // TestDefinitionURLEncoding verifies that file paths with special characters // like '@' are properly URL-encoded in the URI responses. func TestDefinitionURLEncoding(t *testing.T) { diff --git a/private/buf/buflsp/file.go b/private/buf/buflsp/file.go index ed62325ff3..9fc626b0c0 100644 --- a/private/buf/buflsp/file.go +++ b/private/buf/buflsp/file.go @@ -465,6 +465,9 @@ func (f *file) indexSymbols() ([]*symbol, []*symbol) { resolved = append(resolved, resolvedSyms...) unresolved = append(unresolved, unresolvedSyms...) } + // The file's own options: the file is no symbol, so the loop above, + // which indexes each symbol's options, never reaches them. + unresolved = append(unresolved, f.messageToSymbols(f.ir.Options())...) return resolved, unresolved } diff --git a/private/buf/buflsp/testdata/definition/file_options.proto b/private/buf/buflsp/testdata/definition/file_options.proto new file mode 100644 index 0000000000..eba7ae0f19 --- /dev/null +++ b/private/buf/buflsp/testdata/definition/file_options.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package definition.v1; + +import "options.proto"; + +option (definition.v1.file_tag) = "f"; + +message Tagged { + string x = 1 [(definition.v1.field_tag) = "g"]; +} diff --git a/private/buf/buflsp/testdata/definition/options.proto b/private/buf/buflsp/testdata/definition/options.proto new file mode 100644 index 0000000000..4a13cdae39 --- /dev/null +++ b/private/buf/buflsp/testdata/definition/options.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; + +package definition.v1; + +import "google/protobuf/descriptor.proto"; + +extend google.protobuf.FileOptions { + string file_tag = 50001; +} + +extend google.protobuf.FieldOptions { + string field_tag = 50002; +} From 166c6e2b3ef32032b926b427a69687234ac68abf Mon Sep 17 00:00:00 2001 From: Edward McFarlane Date: Mon, 30 Nov 2026 20:28:58 +0000 Subject: [PATCH 2/2] Test references and simplify --- CHANGELOG.md | 4 +- private/buf/buflsp/definition_test.go | 54 ++++--------------- private/buf/buflsp/file.go | 2 - private/buf/buflsp/references_test.go | 29 ++++++++++ .../testdata/definition/file_options.proto | 4 -- .../buflsp/testdata/definition/options.proto | 4 -- 6 files changed, 40 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4b73f82ac..1e961fa1de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,7 @@ ## [Unreleased] -- Fix LSP go-to-definition and references for custom file options, such as - `option (my.file_option) = "x";`: a file's own options were never indexed, unlike a message's or - a field's. +- Fix LSP go-to-definition, references, hover, and rename for file options. - Fix the `PROTOVALIDATE` lint rule to check constant arguments to `duration`, `timestamp`, and `matches` in CEL expressions, such as `duration('1d')` or an invalid regex, which otherwise only fail when the rules are evaluated. diff --git a/private/buf/buflsp/definition_test.go b/private/buf/buflsp/definition_test.go index 0acfaf00a7..c95564c398 100644 --- a/private/buf/buflsp/definition_test.go +++ b/private/buf/buflsp/definition_test.go @@ -208,9 +208,6 @@ func TestDefinition(t *testing.T) { } } -// TestDefinitionFileOption checks that a file's own options are indexed, as -// a message's or a field's are: go-to-definition on a custom file option -// reaches its extension's declaration. func TestDefinitionFileOption(t *testing.T) { t.Parallel() @@ -222,49 +219,18 @@ func TestDefinitionFileOption(t *testing.T) { require.NoError(t, err) clientJSONConn, testURI := setupLSPServer(t, testProtoPath) - optionsURI := buflsp.FilePathToURI(optionsProtoPath) - tests := []struct { - name string - line uint32 - character uint32 - expectedDefLine uint32 - }{ - { - name: "definition_of_file_option", - line: 6, // Line with "option (definition.v1.file_tag) = "f";" - character: 22, // On "file_tag" - expectedDefLine: 7, // string file_tag = 50001; - }, - { - name: "definition_of_field_option", - line: 9, // Line with "string x = 1 [(definition.v1.field_tag) = "g"];" - character: 31, // On "field_tag" - expectedDefLine: 11, // string field_tag = 50002; + var locations []protocol.Location + _, err = clientJSONConn.Call(ctx, protocol.MethodTextDocumentDefinition, protocol.DefinitionParams{ + TextDocumentPositionParams: protocol.TextDocumentPositionParams{ + TextDocument: protocol.TextDocumentIdentifier{URI: testURI}, + Position: protocol.Position{Line: 6, Character: 22}, }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - t.Parallel() - var locations []protocol.Location - _, defErr := clientJSONConn.Call(ctx, protocol.MethodTextDocumentDefinition, protocol.DefinitionParams{ - TextDocumentPositionParams: protocol.TextDocumentPositionParams{ - TextDocument: protocol.TextDocumentIdentifier{ - URI: testURI, - }, - Position: protocol.Position{ - Line: tt.line, - Character: tt.character, - }, - }, - }, &locations) - require.NoError(t, defErr) - require.Len(t, locations, 1, "expected exactly one definition location") - assert.Equal(t, optionsURI, locations[0].URI) - assert.Equal(t, tt.expectedDefLine, locations[0].Range.Start.Line) - }) - } + }, &locations) + require.NoError(t, err) + require.Len(t, locations, 1) + assert.Equal(t, buflsp.FilePathToURI(optionsProtoPath), locations[0].URI) + assert.Equal(t, protocol.Position{Line: 7, Character: 9}, locations[0].Range.Start) } // TestDefinitionURLEncoding verifies that file paths with special characters diff --git a/private/buf/buflsp/file.go b/private/buf/buflsp/file.go index 9fc626b0c0..3c7b397d50 100644 --- a/private/buf/buflsp/file.go +++ b/private/buf/buflsp/file.go @@ -465,8 +465,6 @@ func (f *file) indexSymbols() ([]*symbol, []*symbol) { resolved = append(resolved, resolvedSyms...) unresolved = append(unresolved, unresolvedSyms...) } - // The file's own options: the file is no symbol, so the loop above, - // which indexes each symbol's options, never reaches them. unresolved = append(unresolved, f.messageToSymbols(f.ir.Options())...) return resolved, unresolved } diff --git a/private/buf/buflsp/references_test.go b/private/buf/buflsp/references_test.go index 7a1e3ec9a6..e76f26bcd0 100644 --- a/private/buf/buflsp/references_test.go +++ b/private/buf/buflsp/references_test.go @@ -276,6 +276,35 @@ func TestReferencesStableAcrossReindex(t *testing.T) { assert.Len(t, slices.Compact(slices.Clone(after)), len(after), "re-indexing introduced duplicate references") } +func TestReferencesFileOption(t *testing.T) { + t.Parallel() + + ctx := t.Context() + + fileOptionsProtoPath, err := filepath.Abs("testdata/definition/file_options.proto") + require.NoError(t, err) + optionsProtoPath, err := filepath.Abs("testdata/definition/options.proto") + require.NoError(t, err) + + clientJSONConn, fileOptionsURI := setupLSPServer(t, fileOptionsProtoPath) + optionsURI := buflsp.FilePathToURI(optionsProtoPath) + + locations := testRequestReferences(ctx, t, clientJSONConn, optionsURI, 7, 10, true) + + type refLocation struct { + uri uri.URI + line uint32 + } + var actual []refLocation + for _, location := range locations { + actual = append(actual, refLocation{uri: location.URI, line: location.Range.Start.Line}) + } + assert.ElementsMatch(t, []refLocation{ + {uri: optionsURI, line: 7}, + {uri: fileOptionsURI, line: 6}, + }, actual) +} + // testRequestReferences sends a textDocument/references request and returns the locations. func testRequestReferences( ctx context.Context, diff --git a/private/buf/buflsp/testdata/definition/file_options.proto b/private/buf/buflsp/testdata/definition/file_options.proto index eba7ae0f19..a81718f1f7 100644 --- a/private/buf/buflsp/testdata/definition/file_options.proto +++ b/private/buf/buflsp/testdata/definition/file_options.proto @@ -5,7 +5,3 @@ package definition.v1; import "options.proto"; option (definition.v1.file_tag) = "f"; - -message Tagged { - string x = 1 [(definition.v1.field_tag) = "g"]; -} diff --git a/private/buf/buflsp/testdata/definition/options.proto b/private/buf/buflsp/testdata/definition/options.proto index 4a13cdae39..89539cad4e 100644 --- a/private/buf/buflsp/testdata/definition/options.proto +++ b/private/buf/buflsp/testdata/definition/options.proto @@ -7,7 +7,3 @@ import "google/protobuf/descriptor.proto"; extend google.protobuf.FileOptions { string file_tag = 50001; } - -extend google.protobuf.FieldOptions { - string field_tag = 50002; -}