Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fix LSP go-to-definition, references, hover, and rename for file options.
- Fix an LSP crash when organizing imports in a workspace with a file that fails to build.
- Fix LSP go-to-definition and references for the message named in an `extend` block.
- Fix LSP go-to-definition, references, and rename for enum values used as option values.
- 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
Expand Down
90 changes: 90 additions & 0 deletions private/buf/buflsp/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package buflsp_test

import (
"path/filepath"
"strings"
"testing"

"github.com/bufbuild/buf/private/buf/buflsp"
Expand Down Expand Up @@ -346,6 +347,95 @@ func TestDefinitionOptionEnumValue(t *testing.T) {
}
}

func TestDefinitionExtendee(t *testing.T) {
t.Parallel()

ctx := t.Context()

testProtoPath, err := filepath.Abs("testdata/definition/extendee.proto")
require.NoError(t, err)

clientJSONConn, testURI := setupLSPServer(t, testProtoPath)

tests := []struct {
name string
line uint32
character uint32
expectedDefURI uri.URI // Empty for descriptor.proto, checked by its path.
expectedDefLine uint32
}{
{
name: "definition_of_extendee",
line: 14, // Line with "extend Base {"
character: 7, // On "Base"
expectedDefURI: testURI,
expectedDefLine: 6, // message Base
},
{
name: "definition_of_nested_extendee",
line: 19, // Line with " extend Base {" in message Holder
character: 9, // On "Base"
expectedDefURI: testURI,
expectedDefLine: 6, // message Base
},
{
name: "definition_of_options_extendee",
line: 10, // Line with "extend google.protobuf.FileOptions {"
character: 23, // On "FileOptions"
},
}

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")
if tt.expectedDefURI == "" {
assert.True(t, strings.HasSuffix(string(locations[0].URI), "google/protobuf/descriptor.proto"), locations[0].URI)
return
}
assert.Equal(t, tt.expectedDefURI, locations[0].URI)
assert.Equal(t, tt.expectedDefLine, locations[0].Range.Start.Line)
})
}

t.Run("references_include_extend_blocks", func(t *testing.T) {
t.Parallel()
var locations []protocol.Location
_, refErr := clientJSONConn.Call(ctx, protocol.MethodTextDocumentReferences, protocol.ReferenceParams{
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: testURI,
},
Position: protocol.Position{
Line: 6, // Line with "message Base {"
Character: 8, // On "Base"
},
},
Context: protocol.ReferenceContext{IncludeDeclaration: false},
}, &locations)
require.NoError(t, refErr)
var lines []uint32
for _, location := range locations {
assert.Equal(t, testURI, location.URI)
lines = append(lines, location.Range.Start.Line)
}
assert.ElementsMatch(t, []uint32{14, 19}, lines)
})
}

// TestDefinitionURLEncoding verifies that file paths with special characters
// like '@' are properly URL-encoded in the URI responses.
func TestDefinitionURLEncoding(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions private/buf/buflsp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,21 @@ func (f *file) indexSymbols() ([]*symbol, []*symbol) {
unresolved = append(unresolved, unresolvedSyms...)
}
unresolved = append(unresolved, f.messageToSymbols(f.ir.Options())...)
for extend := range seq.Values(f.ir.AllExtends()) {
extendee := extend.Extendee()
span := extend.AST().AsExtend().Extendee.Span()
if extendee.IsZero() || span.IsZero() {
continue
}
unresolved = append(unresolved, &symbol{
file: f,
span: span,
kind: &reference{
def: extendee.AST(),
fullName: extendee.FullName(),
},
})
}
return resolved, unresolved
}

Expand Down
23 changes: 23 additions & 0 deletions private/buf/buflsp/testdata/definition/extendee.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto2";

package definition.v1;

import "google/protobuf/descriptor.proto";

message Base {
extensions 100 to 199;
}

extend google.protobuf.FileOptions {
optional bool file_flag = 50002;
}

extend Base {
optional int32 tag = 100;
}

message Holder {
extend Base {
optional int32 nested_tag = 101;
}
}
Loading