feat(playbooks): archie-playbooks serve, a language server publishing lint findings as diagnostics

This commit is contained in:
sam committed 2026-09-23 22:22:56 +10:00
1 parent 61dd33edce
commit fda2c51f6d
7 files changed
+336 -59

No files matched your search

+160
View File
@@ -0,0 +1,160 @@
package archieplaybooks
import (
"bytes"
"context"
"encoding/json"
"io"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/sourcegraph/jsonrpc2"
"gopkg.in/yaml.v3"
)
// Serve runs the language server over rwc until the client disconnects or
// ctx ends. It publishes diagnostics when a playbook file is opened or saved,
// by linting the file's saved directory with the loader the daemon runs for
// 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))
select {
case <-conn.DisconnectNotify():
case <-ctx.Done():
_ = conn.Close()
}
return nil
}
type position struct {
Line int `json:"line"`
Character int `json:"character"`
}
type lspRange struct {
Start position `json:"start"`
End position `json:"end"`
}
type diagnostic struct {
Range lspRange `json:"range"`
Severity int `json:"severity"`
Source string `json:"source"`
Message string `json:"message"`
}
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" {
return out
}
path := u.Path
var result Result
if isEDAPlaybook(path) {
result = LintEDA(filepath.Dir(path), io.Discard)
} else {
result = Lint([]string{filepath.Dir(path)}, io.Discard)
}
for _, finding := range result.Findings {
line, ok := findingLine(finding, path)
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,
})
}
return out
}
// isEDAPlaybook reports whether the file is an EDA playbook document (it has
// a top-level `trigger` key) rather than a routing binding file.
func isEDAPlaybook(path string) bool {
data, err := os.ReadFile(path)
if err != nil {
return false
}
var doc map[string]yaml.Node
if yaml.NewDecoder(bytes.NewReader(data)).Decode(&doc) != nil {
return false
}
_, ok := doc["trigger"]
return ok
}
// 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) {
if m := regexp.MustCompile(regexp.QuoteMeta(path) + `:(\d+):`).FindStringSubmatch(finding); m != nil {
n, _ := strconv.Atoi(m[1])
return max(n-1, 0), true
}
if strings.Contains(finding, path) {
return 0, true
}
return 0, false
}
+119
View File
@@ -0,0 +1,119 @@
package archieplaybooks
import (
"context"
"encoding/json"
"net"
"net/url"
"path/filepath"
"testing"
"time"
"github.com/sourcegraph/jsonrpc2"
)
// lspClient drives Serve over an in-memory pipe and collects every
// publishDiagnostics notification it receives.
type lspClient struct {
conn *jsonrpc2.Conn
diags chan publishDiagnosticsParams
}
func startServe(t *testing.T) *lspClient {
t.Helper()
ctx, cancel := context.WithCancel(t.Context())
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 {
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 {
t.Fatal(err)
}
select {
case p := <-c.diags:
if p.URI != uri {
t.Fatalf("diagnostics for %q, want %q", p.URI, uri)
}
return p
case <-time.After(5 * time.Second):
t.Fatal("no publishDiagnostics received")
}
return publishDiagnosticsParams{}
}
func TestServePublishesDiagnostics(t *testing.T) {
tests := []struct {
name string
files map[string]string
open string
wantLine int // 0-based; -1 means no diagnostics
}{
{
name: "clean routing file",
files: map[string]string{"routes.yaml": "bug: tdd\n"},
open: "routes.yaml",
wantLine: -1,
},
{
name: "routing finding on its key line",
files: map[string]string{"routes.yaml": "security: review\n\ndocs: \"\"\n"},
open: "routes.yaml",
wantLine: 2,
},
{
name: "cross-file collision reported on the opened file",
files: map[string]string{"a.yaml": "security: review\n", "b.yaml": "\nsecurity: other\n"},
open: "b.yaml",
wantLine: 1,
},
{
name: "eda playbook finding without a line",
files: map[string]string{"pb.yaml": "trigger:\n kind: bug\nactions:\n - position: module\n kind: nope\n"},
open: "pb.yaml",
wantLine: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
for name, content := range tt.files {
writeFile(t, filepath.Join(dir, name), content)
}
p := startServe(t).open(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 {
t.Fatalf("diagnostics = %+v, want one on line %d", p.Diagnostics, tt.wantLine)
}
})
}
}