From 63be83e0cf7a0186ff6b46fb2f8596e0c633f9d6 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 23 Sep 2026 22:34:33 +1000 Subject: [PATCH] refactor(playbooks): serve on go.lsp.dev/protocol instead of hand-written LSP types Replaces sourcegraph/jsonrpc2 with the go.lsp.dev stack (protocol, jsonrpc2, uri). The exit notification now ends Serve; closing the connection from inside the exit handler deadlocked. --- go.mod | 5 +- go.sum | 12 +- internal/app/archieplaybooks/serve.go | 153 +++++++++------------ internal/app/archieplaybooks/serve_test.go | 105 ++++++++------ 4 files changed, 138 insertions(+), 137 deletions(-) diff --git a/go.mod b/go.mod index 3ecc78c22..1e6641388 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,7 @@ require ( github.com/ganigeorgiev/fexpr v0.6.0 // indirect github.com/go-fed/httpsig v1.1.0 // indirect github.com/go-git/gcfg/v2 v2.0.2 // indirect + github.com/go-json-experiment/json v0.0.0-20260623181947-01eb4420fa68 // indirect github.com/go-logr/logr v1.4.4 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/golang-jwt/jwt/v5 v5.3.1 // indirect @@ -95,8 +96,10 @@ require ( github.com/pocketbase/dbx v1.12.0 github.com/pocketbase/pocketbase v0.40.4 github.com/samcharles93/ai-sdk v0.1.39 - github.com/sourcegraph/jsonrpc2 v0.2.3 github.com/traefik/yaegi v0.16.1 + go.lsp.dev/jsonrpc2 v1.0.1 + go.lsp.dev/protocol v1.0.1 + go.lsp.dev/uri v1.0.1 golang.org/x/net v0.59.0 golang.org/x/oauth2 v0.37.0 golang.org/x/term v0.46.0 diff --git a/go.sum b/go.sum index c052e3af8..68954390e 100644 --- a/go.sum +++ b/go.sum @@ -78,6 +78,8 @@ github.com/go-git/go-git/v6 v6.0.0-alpha.5 h1:sE+OlkHgYWNMVmN1s9sR7uyFgsWLtxcNWs github.com/go-git/go-git/v6 v6.0.0-alpha.5/go.mod h1:3IjhiZnM+uBmUrOGSeqrJpsmi4Vd0H2NZO/uK2a7d0s= github.com/go-jose/go-jose/v4 v4.1.5 h1:RjgjO2LOtWOJKUC5wpwY9LR3B3vwVAz6JS2YHfYU6eA= github.com/go-jose/go-jose/v4 v4.1.5/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08= +github.com/go-json-experiment/json v0.0.0-20260623181947-01eb4420fa68 h1:KZaTBSyshWX3MP5jukJcNSuXDQTO+rNpt0J564dX/eg= +github.com/go-json-experiment/json v0.0.0-20260623181947-01eb4420fa68/go.mod h1:tphK2c80bpPhMOI4v6bIc2xWywPfbqi1Z06+RcrMkDg= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8= github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= @@ -105,8 +107,6 @@ github.com/google/pprof v0.0.0-20260902005441-ca85771921e4 h1:/6mPXfWmhv8eKck12I github.com/google/pprof v0.0.0-20260902005441-ca85771921e4/go.mod h1:jl5iWTm0/hd5PjEYEOuwAJ57L/CibdZfrqZ5XA5GrCk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= -github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hashicorp/go-version v1.9.0 h1:CeOIz6k+LoN3qX9Z0tyQrPtiB1DFYRPfCIBtaXPSCnA= github.com/hashicorp/go-version v1.9.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= @@ -174,8 +174,6 @@ github.com/samcharles93/ai-sdk v0.1.39 h1:gbNPYeA27MifReCruBiqMgb4fFSjCDi1Zv57a9 github.com/samcharles93/ai-sdk v0.1.39/go.mod h1:u4zrQ9/Q58L5mNEut4bNQ1ks+RCKq71LN5EDz2N/0Zk= github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw= github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= -github.com/sourcegraph/jsonrpc2 v0.2.3 h1:0VYp5WZ2irQvRK8OKKxZAbbplj6Pmda07MXm+Mdz3GQ= -github.com/sourcegraph/jsonrpc2 v0.2.3/go.mod h1:0jBvyko0tdLpe6ntNSF/mvFalb/RPRXcmogoV47cEWc= github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY= github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo= github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= @@ -189,6 +187,12 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= github.com/traefik/yaegi v0.16.1 h1:f1De3DVJqIDKmnasUF6MwmWv1dSEEat0wcpXhD2On3E= github.com/traefik/yaegi v0.16.1/go.mod h1:4eVhbPb3LnD2VigQjhYbEJ69vDRFdT2HQNrXx8eEwUY= +go.lsp.dev/jsonrpc2 v1.0.1 h1:JZAchpm6RcOuwEOnZ7kF3kA1EpEjdJZ/6rMSfOF3+F4= +go.lsp.dev/jsonrpc2 v1.0.1/go.mod h1:WN+TesqrPumBmUdsuWyn9DHq3zl4i85L/qG0NLdj8qg= +go.lsp.dev/protocol v1.0.1 h1:kLWUIk1v/deXxo1D0VO4avjZnfeuZ82mouJgJd/NbdU= +go.lsp.dev/protocol v1.0.1/go.mod h1:e//i41aEqc9ml32xiNuneUwbjOjuvTA4+uv+AkssJgk= +go.lsp.dev/uri v1.0.1 h1:ywxJN1UXoQ+v0I9lkoQNMmsm7lub5G9ZG7KXT8YTx30= +go.lsp.dev/uri v1.0.1/go.mod h1:06a0ghafs4PuqkLsJGUmlgPHw6+kH79edRXjOT/rA8s= go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0 h1:3g7B90UzBltIDKq1/5mrTGxTnOFDV0ICOhLoxiZ8jlg= diff --git a/internal/app/archieplaybooks/serve.go b/internal/app/archieplaybooks/serve.go index 83169dfde..aabdcf8cb 100644 --- a/internal/app/archieplaybooks/serve.go +++ b/internal/app/archieplaybooks/serve.go @@ -3,16 +3,17 @@ package archieplaybooks import ( "bytes" "context" - "encoding/json" "io" - "net/url" "os" "path/filepath" "regexp" "strconv" "strings" + "sync" - "github.com/sourcegraph/jsonrpc2" + "go.lsp.dev/jsonrpc2" + "go.lsp.dev/protocol" + "go.lsp.dev/uri" "gopkg.in/yaml.v3" ) @@ -22,93 +23,72 @@ import ( // it: the whole directory, because a collision is a cross-file finding. // Unsaved edits are not validated. func Serve(ctx context.Context, rwc io.ReadWriteCloser) error { - conn := jsonrpc2.NewConn(ctx, jsonrpc2.NewBufferedStream(rwc, jsonrpc2.VSCodeObjectCodec{}), jsonrpc2.HandlerWithError(handle)) + s := &server{exited: make(chan struct{})} + _, conn, client := protocol.NewServer(ctx, s, jsonrpc2.NewStream(rwc)) + s.client = client select { - case <-conn.DisconnectNotify(): + case <-conn.Done(): + return nil + case <-s.exited: case <-ctx.Done(): - _ = conn.Close() } + return conn.Close() +} + +type server struct { + protocol.UnimplementedServer + client protocol.Client + // exited is closed by the exit notification. Serve closes the connection: + // closing it from inside the handler would wait on that handler. + exited chan struct{} + exitOnce sync.Once +} + +func (s *server) Initialize(context.Context, *protocol.InitializeParams) (*protocol.InitializeResult, error) { + // The server reads files from disk, so it needs opens, closes and saves, + // not content changes. + openClose, change := true, protocol.TextDocumentSyncKindNone + return &protocol.InitializeResult{ + Capabilities: protocol.ServerCapabilities{ + TextDocumentSync: &protocol.TextDocumentSyncOptions{ + OpenClose: &openClose, + Change: &change, + Save: protocol.Boolean(true), + }, + }, + ServerInfo: protocol.ServerInfo{Name: "archie-playbooks"}, + }, nil +} + +func (s *server) Shutdown(context.Context) error { return nil } + +func (s *server) Exit(context.Context) error { + s.exitOnce.Do(func() { close(s.exited) }) return nil } -type position struct { - Line int `json:"line"` - Character int `json:"character"` +func (s *server) DidOpen(ctx context.Context, p *protocol.DidOpenTextDocumentParams) error { + return s.client.PublishDiagnostics(ctx, diagnose(p.TextDocument.URI)) } -type lspRange struct { - Start position `json:"start"` - End position `json:"end"` +func (s *server) DidSave(ctx context.Context, p *protocol.DidSaveTextDocumentParams) error { + return s.client.PublishDiagnostics(ctx, diagnose(p.TextDocument.URI)) } -type diagnostic struct { - Range lspRange `json:"range"` - Severity int `json:"severity"` - Source string `json:"source"` - Message string `json:"message"` +func (s *server) DidClose(ctx context.Context, p *protocol.DidCloseTextDocumentParams) error { + return s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{URI: p.TextDocument.URI, Diagnostics: []protocol.Diagnostic{}}) } -type publishDiagnosticsParams struct { - URI string `json:"uri"` - Diagnostics []diagnostic `json:"diagnostics"` -} - -type textDocumentParams struct { - TextDocument struct { - URI string `json:"uri"` - } `json:"textDocument"` -} - -const severityError = 1 - -func handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (any, error) { - switch req.Method { - case "initialize": - // Full sync with save notifications: the server reads files from - // disk, so it needs to hear about opens and saves only. - return map[string]any{"capabilities": map[string]any{ - "textDocumentSync": map[string]any{"openClose": true, "change": 0, "save": true}, - }}, nil - case "shutdown": - return nil, nil - case "exit": - return nil, conn.Close() - case "textDocument/didOpen", "textDocument/didSave": - if uri, ok := documentURI(req); ok { - return nil, conn.Notify(ctx, "textDocument/publishDiagnostics", diagnose(uri)) - } - case "textDocument/didClose": - if uri, ok := documentURI(req); ok { - return nil, conn.Notify(ctx, "textDocument/publishDiagnostics", publishDiagnosticsParams{URI: uri, Diagnostics: []diagnostic{}}) - } - } - if req.Notif { - return nil, nil - } - return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeMethodNotFound, Message: "method not supported: " + req.Method} -} - -// documentURI reads a text-document notification's URI. A notification has -// no response to carry an error, so malformed params are ignored. -func documentURI(req *jsonrpc2.Request) (string, bool) { - var p textDocumentParams - if req.Params == nil || json.Unmarshal(*req.Params, &p) != nil || p.TextDocument.URI == "" { - return "", false - } - return p.TextDocument.URI, true -} - -// diagnose lints the directory holding uri's file and returns the findings -// that belong to that file: a finding naming another file of the directory -// belongs there, and one naming this file without a line (an EDA load error) -// goes on its first line. -func diagnose(uri string) publishDiagnosticsParams { - out := publishDiagnosticsParams{URI: uri, Diagnostics: []diagnostic{}} - u, err := url.Parse(uri) - if err != nil || u.Scheme != "file" { +// diagnose lints the directory holding u's file and returns the findings that +// belong to that file: a finding naming another file of the directory belongs +// there, and one naming this file without a line (an EDA load error) goes on +// its first line. +func diagnose(u uri.URI) *protocol.PublishDiagnosticsParams { + out := &protocol.PublishDiagnosticsParams{URI: u, Diagnostics: []protocol.Diagnostic{}} + if !u.IsFile() { return out } - path := u.Path + path := u.FsPath() var result Result if isEDAPlaybook(path) { result = LintEDA(filepath.Dir(path), io.Discard) @@ -120,11 +100,11 @@ func diagnose(uri string) publishDiagnosticsParams { if !ok { continue } - out.Diagnostics = append(out.Diagnostics, diagnostic{ - Range: lspRange{Start: position{Line: line}, End: position{Line: line, Character: 1 << 16}}, - Severity: severityError, - Source: "archie-playbooks", - Message: finding, + out.Diagnostics = append(out.Diagnostics, protocol.Diagnostic{ + Range: protocol.Range{Start: protocol.Position{Line: line}, End: protocol.Position{Line: line + 1}}, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("archie-playbooks"), + Message: protocol.String(finding), }) } return out @@ -148,13 +128,10 @@ func isEDAPlaybook(path string) bool { // findingLine places a finding on path. A compiler-style `path:line:` prefix // gives the 0-based line; a finding naming path without a line goes on line // 0; a finding naming another file is not path's. -func findingLine(finding, path string) (int, bool) { +func findingLine(finding, path string) (uint32, bool) { if m := regexp.MustCompile(regexp.QuoteMeta(path) + `:(\d+):`).FindStringSubmatch(finding); m != nil { - n, _ := strconv.Atoi(m[1]) - return max(n-1, 0), true + n, _ := strconv.ParseUint(m[1], 10, 32) + return uint32(max(n, 1) - 1), true } - if strings.Contains(finding, path) { - return 0, true - } - return 0, false + return 0, strings.Contains(finding, path) } diff --git a/internal/app/archieplaybooks/serve_test.go b/internal/app/archieplaybooks/serve_test.go index 4813b053c..1df76484e 100644 --- a/internal/app/archieplaybooks/serve_test.go +++ b/internal/app/archieplaybooks/serve_test.go @@ -2,68 +2,57 @@ package archieplaybooks import ( "context" - "encoding/json" "net" - "net/url" "path/filepath" "testing" "time" - "github.com/sourcegraph/jsonrpc2" + "go.lsp.dev/jsonrpc2" + "go.lsp.dev/protocol" + "go.lsp.dev/uri" ) -// lspClient drives Serve over an in-memory pipe and collects every -// publishDiagnostics notification it receives. -type lspClient struct { - conn *jsonrpc2.Conn - diags chan publishDiagnosticsParams +// diagnosticsClient is the editor side: it collects every +// publishDiagnostics notification the server sends. +type diagnosticsClient struct { + protocol.UnimplementedClient + diags chan *protocol.PublishDiagnosticsParams } -func startServe(t *testing.T) *lspClient { +func (c *diagnosticsClient) PublishDiagnostics(_ context.Context, p *protocol.PublishDiagnosticsParams) error { + c.diags <- p + return nil +} + +// openInServe starts Serve over an in-memory pipe, initializes it, opens +// path, and returns the diagnostics published for it. +func openInServe(t *testing.T, path string) *protocol.PublishDiagnosticsParams { t.Helper() - ctx, cancel := context.WithCancel(t.Context()) + ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second) t.Cleanup(cancel) - server, client := net.Pipe() - go func() { _ = Serve(ctx, server) }() - c := &lspClient{diags: make(chan publishDiagnosticsParams, 8)} - c.conn = jsonrpc2.NewConn(ctx, jsonrpc2.NewBufferedStream(client, jsonrpc2.VSCodeObjectCodec{}), - jsonrpc2.HandlerWithError(func(_ context.Context, _ *jsonrpc2.Conn, r *jsonrpc2.Request) (any, error) { - if r.Method == "textDocument/publishDiagnostics" && r.Params != nil { - var p publishDiagnosticsParams - if err := json.Unmarshal(*r.Params, &p); err == nil { - c.diags <- p - } - } - return nil, nil - })) - t.Cleanup(func() { _ = c.conn.Close() }) - var init map[string]any - callCtx, stop := context.WithTimeout(ctx, 5*time.Second) - defer stop() - if err := c.conn.Call(callCtx, "initialize", map[string]any{}, &init); err != nil { + serverEnd, clientEnd := net.Pipe() + go func() { _ = Serve(ctx, serverEnd) }() + client := &diagnosticsClient{diags: make(chan *protocol.PublishDiagnosticsParams, 8)} + _, conn, server := protocol.NewClient(ctx, client, jsonrpc2.NewStream(clientEnd)) + t.Cleanup(func() { _ = conn.Close() }) + + if _, err := server.Initialize(ctx, &protocol.InitializeParams{}); err != nil { t.Fatalf("initialize: %v", err) } - return c -} - -func (c *lspClient) open(t *testing.T, path string) publishDiagnosticsParams { - t.Helper() - uri := (&url.URL{Scheme: "file", Path: path}).String() - if err := c.conn.Notify(t.Context(), "textDocument/didOpen", map[string]any{ - "textDocument": map[string]any{"uri": uri, "languageId": "yaml", "version": 1, "text": ""}, - }); err != nil { + u := uri.File(path) + if err := server.DidOpen(ctx, &protocol.DidOpenTextDocumentParams{TextDocument: protocol.TextDocumentItem{URI: u, LanguageID: "yaml", Version: 1}}); err != nil { t.Fatal(err) } select { - case p := <-c.diags: - if p.URI != uri { - t.Fatalf("diagnostics for %q, want %q", p.URI, uri) + case p := <-client.diags: + if p.URI != u { + t.Fatalf("diagnostics for %q, want %q", p.URI, u) } return p - case <-time.After(5 * time.Second): + case <-ctx.Done(): t.Fatal("no publishDiagnostics received") } - return publishDiagnosticsParams{} + return nil } func TestServePublishesDiagnostics(t *testing.T) { @@ -104,16 +93,44 @@ func TestServePublishesDiagnostics(t *testing.T) { for name, content := range tt.files { writeFile(t, filepath.Join(dir, name), content) } - p := startServe(t).open(t, filepath.Join(dir, tt.open)) + p := openInServe(t, filepath.Join(dir, tt.open)) if tt.wantLine < 0 { if len(p.Diagnostics) != 0 { t.Fatalf("diagnostics = %+v, want none", p.Diagnostics) } return } - if len(p.Diagnostics) != 1 || p.Diagnostics[0].Range.Start.Line != tt.wantLine { + if len(p.Diagnostics) != 1 || int(p.Diagnostics[0].Range.Start.Line) != tt.wantLine { t.Fatalf("diagnostics = %+v, want one on line %d", p.Diagnostics, tt.wantLine) } }) } } + +// The exit notification ends Serve. Closing the connection from inside the +// exit handler waited on that handler, so an editor quitting left the server +// running. +func TestServeReturnsAfterExit(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second) + defer cancel() + serverEnd, clientEnd := net.Pipe() + done := make(chan error, 1) + go func() { done <- Serve(ctx, serverEnd) }() + _, conn, server := protocol.NewClient(ctx, &diagnosticsClient{}, jsonrpc2.NewStream(clientEnd)) + defer conn.Close() + + if _, err := server.Initialize(ctx, &protocol.InitializeParams{}); err != nil { + t.Fatalf("initialize: %v", err) + } + if err := server.Shutdown(ctx); err != nil { + t.Fatalf("shutdown: %v", err) + } + if err := server.Exit(ctx); err != nil { + t.Fatalf("exit: %v", err) + } + select { + case <-done: + case <-ctx.Done(): + t.Fatal("Serve still running after exit") + } +}