Skip to content

Commit c4f9a5e

Browse files
Add tags to diagnostics
Also removes the diagnostic.Data from the new diagnostics, which AFAICS is unused; we can save ourselves the json marshaling. As a part of this, added a first pass at adding tests for diagnostics; we don't currently have any. These are a bit different because we haven't implemented [PullDiagnostics][1] (we can add this, but we'd need a custom implementation, or wait until [go.lsp.dev upgrades to 3.17][2]). Instead, we intercept the notifications from the server. Lastly, since we don't actually want the tests to wait around, adds synctest and only runs the tests on Go 1.25 for now (1.26 should be released next month and we'll be able to upgrade to 1.25 as our minimum). This is in preparation to eventually landing relatedInformation (other spans) in diagnostics, from bufbuild/protocompile#659. Also pulls in the latest protocompile@main to bring in the new deprecated tag. [1]: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_pullDiagnostics [2]: go-language-server/protocol#52
1 parent 08df2b3 commit c4f9a5e

File tree

11 files changed

+435
-50
lines changed

11 files changed

+435
-50
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.9.0
19-
github.com/bufbuild/protocompile v0.14.2-0.20260121154354-91940cd2bca3
19+
github.com/bufbuild/protocompile v0.14.2-0.20260130195850-5c64bed4577e
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYW
4040
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
4141
github.com/bmatcuk/doublestar/v4 v4.9.2 h1:b0mc6WyRSYLjzofB2v/0cuDUZ+MqoGyH3r0dVij35GI=
4242
github.com/bmatcuk/doublestar/v4 v4.9.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
43+
github.com/bmatcuk/doublestar/v4 v4.10.0 h1:zU9WiOla1YA122oLM6i4EXvGW62DvKZVxIe6TYWexEs=
4344
github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4=
4445
github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs=
4546
github.com/bufbuild/protocompile v0.14.2-0.20260121154354-91940cd2bca3 h1:7wRdWMBC4bFiS66NRJtMWRimxYik53CKtcU8KPP1EMg=
4647
github.com/bufbuild/protocompile v0.14.2-0.20260121154354-91940cd2bca3/go.mod h1:H51HpPHpeLddOIg/OZAuZsPmlKvP8pyXKXAkOUin9ZM=
48+
github.com/bufbuild/protocompile v0.14.2-0.20260130195850-5c64bed4577e h1:emH16Bf1w4C0cJ3ge4QtBAl4sIYJe23EfpWH0SpA9co=
49+
github.com/bufbuild/protocompile v0.14.2-0.20260130195850-5c64bed4577e/go.mod h1:cxhE8h+14t0Yxq2H9MV/UggzQ1L0gh0t2tJobITWsBE=
4750
github.com/bufbuild/protoplugin v0.0.0-20250218205857-750e09ce93e1 h1:V1xulAoqLqVg44rY97xOR+mQpD2N+GzhMHVwJ030WEU=
4851
github.com/bufbuild/protoplugin v0.0.0-20250218205857-750e09ce93e1/go.mod h1:c5D8gWRIZ2HLWO3gXYTtUfw/hbJyD8xikv2ooPxnklQ=
4952
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=

private/buf/buflsp/buflsp_test.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,20 @@ func setupLSPServer(
121121

122122
stream := jsonrpc2.NewStream(serverConn)
123123

124-
go func() {
125-
conn, err := buflsp.Serve(
126-
ctx,
127-
"test",
128-
wktBucket,
129-
appextContainer,
130-
controller,
131-
wasmRuntime,
132-
stream,
133-
queryExecutor,
134-
)
135-
if err != nil {
136-
t.Errorf("Failed to start server: %v", err)
137-
return
138-
}
139-
t.Cleanup(func() {
140-
require.NoError(t, conn.Close())
141-
})
142-
<-ctx.Done()
143-
}()
124+
conn, err := buflsp.Serve(
125+
ctx,
126+
"test",
127+
wktBucket,
128+
appextContainer,
129+
controller,
130+
wasmRuntime,
131+
stream,
132+
queryExecutor,
133+
)
134+
require.NoError(t, err)
135+
t.Cleanup(func() {
136+
require.NoError(t, conn.Close())
137+
})
144138

145139
clientStream := jsonrpc2.NewStream(clientConn)
146140
clientJSONConn := jsonrpc2.NewConn(clientStream)

private/buf/buflsp/diagnostic.go

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
package buflsp
1818

1919
import (
20-
"encoding/json"
21-
"strings"
22-
2320
"github.com/bufbuild/protocompile/experimental/report"
21+
"github.com/bufbuild/protocompile/experimental/report/tags"
2422
"github.com/bufbuild/protocompile/experimental/source/length"
2523
"go.lsp.dev/protocol"
2624
)
@@ -30,14 +28,6 @@ import (
3028
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position
3129
const positionalEncoding = length.UTF16
3230

33-
// diagnosticData is a structure to hold the [report.Diagnostic] notes, help, and debug
34-
// messages, to marshal into JSON for the [protocol.Diagnostic].Data field.
35-
type diagnosticData struct {
36-
Notes string `json:"notes,omitempty"`
37-
Help string `json:"help,omitempty"`
38-
Debug string `json:"debug,omitempty"`
39-
}
40-
4131
// reportLevelToDiagnosticSeverity is a mapping of [report.Level] to [protocol.DiagnosticSeverity].
4232
var reportLevelToDiagnosticSeverity = map[report.Level]protocol.DiagnosticSeverity{
4333
report.ICE: protocol.DiagnosticSeverityError,
@@ -70,20 +60,15 @@ func reportDiagnosticToProtocolDiagnostic(
7060
},
7161
}
7262
}
73-
data := diagnosticData{
74-
Notes: strings.Join(reportDiagnostic.Notes(), "\n"),
75-
Help: strings.Join(reportDiagnostic.Help(), "\n"),
76-
Debug: strings.Join(reportDiagnostic.Debug(), "\n"),
77-
}
78-
bytes, err := json.Marshal(data)
79-
if err != nil {
80-
return protocol.Diagnostic{}, err
81-
}
82-
if bytes != nil {
83-
// We serialize the bytes into a string before providing the structure to diagnostic.Data
84-
// because diagnostic.Data is an interface{}, which is treated as a JSON "any", which
85-
// will not cleanly deserialize.
86-
diagnostic.Data = string(bytes)
63+
switch reportDiagnostic.Tag() {
64+
case tags.UnusedImport:
65+
diagnostic.Tags = []protocol.DiagnosticTag{
66+
protocol.DiagnosticTagUnnecessary,
67+
}
68+
case tags.Deprecated:
69+
diagnostic.Tags = []protocol.DiagnosticTag{
70+
protocol.DiagnosticTagDeprecated,
71+
}
8772
}
8873
return diagnostic, nil
8974
}

0 commit comments

Comments
 (0)