Skip to content

Commit dc776e9

Browse files
author
Charlie Egan
committed
lsp: Fix server signature race in test
E.g. https://github.com/StyraInc/regal/actions/runs/16962142183/job/48077281888?pr=1653 Signed-off-by: Charlie Egan <charlie@styra.com>
1 parent ef44b9e commit dc776e9

2 files changed

Lines changed: 20 additions & 20 deletions

File tree

internal/lsp/server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func (l *LanguageServer) Handle(ctx context.Context, _ *jsonrpc2.Conn, req *json
260260
case "textDocument/completion":
261261
return handler.WithContextAndParams(ctx, req, l.handleTextDocumentCompletion)
262262
case "textDocument/signatureHelp":
263-
return handler.WithParams(req, l.handleTextDocumentSignatureHelp)
263+
return handler.WithContextAndParams(ctx, req, l.handleTextDocumentSignatureHelp)
264264
case "workspace/didChangeWatchedFiles":
265265
return handler.WithParams(req, l.handleWorkspaceDidChangeWatchedFiles)
266266
case "workspace/diagnostic":
@@ -1449,7 +1449,10 @@ func (l *LanguageServer) handleTextDocumentHover(params types.TextDocumentHoverP
14491449
return nil, nil
14501450
}
14511451

1452-
func (l *LanguageServer) handleTextDocumentSignatureHelp(params types.SignatureHelpParams) (any, error) {
1452+
func (l *LanguageServer) handleTextDocumentSignatureHelp(
1453+
ctx context.Context,
1454+
params types.SignatureHelpParams,
1455+
) (any, error) {
14531456
if l.ignoreURI(params.TextDocument.URI) {
14541457
return nil, nil
14551458
}
@@ -1465,9 +1468,6 @@ func (l *LanguageServer) handleTextDocumentSignatureHelp(params types.SignatureH
14651468
}
14661469
input := ast.MustInterfaceToValue(inputData)
14671470

1468-
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
1469-
defer cancel()
1470-
14711471
var queryResult map[string]any
14721472

14731473
if err := rego.CachedQueryEval(ctx, `data.regal.lsp["signature-help"]`, input, &queryResult); err != nil {

internal/lsp/server_signature_help_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@ func TestTextDocumentSignatureHelp(t *testing.T) {
1616
t.Parallel()
1717

1818
ctx, cancel := context.WithCancel(t.Context())
19-
defer cancel()
19+
t.Cleanup(cancel)
2020

2121
tempDir := t.TempDir()
2222

23-
ls := NewLanguageServer(ctx, &LanguageServerOptions{
24-
LogWriter: newTestLogger(t),
25-
LogLevel: log.LevelDebug,
26-
})
27-
2823
mainRegoURI := fileURIScheme + filepath.Join(tempDir, "main.rego")
2924

3025
content := `package example
@@ -33,20 +28,12 @@ allow if regex.match(` + "`foo`" + `, "bar")
3328
allow if count([1,2,3]) == 2
3429
allow if concat(",", "a", "b") == "b,a"`
3530

36-
ls.cache.SetFileContents(mainRegoURI, content)
37-
3831
builtins := map[string]*ast.Builtin{
3932
"count": ast.Count,
4033
"concat": ast.Concat,
4134
"regex.match": ast.RegexMatch,
4235
}
4336

44-
// this is needed to prepare the server for signature help queries
45-
err := ls.storeBuiltinsAndCacheSignatureHelp(ctx, builtins)
46-
if err != nil {
47-
t.Fatalf("failed to store builtins and cache signature help: %s", err)
48-
}
49-
5037
testCases := map[string]struct {
5138
position types.Position
5239
expectedLabel string
@@ -77,14 +64,27 @@ allow if concat(",", "a", "b") == "b,a"`
7764
t.Run(name, func(t *testing.T) {
7865
t.Parallel()
7966

67+
ls := NewLanguageServer(ctx, &LanguageServerOptions{
68+
LogWriter: newTestLogger(t),
69+
LogLevel: log.LevelDebug,
70+
})
71+
72+
ls.cache.SetFileContents(mainRegoURI, content)
73+
74+
// this is needed to prepare the server for signature help queries
75+
err := ls.storeBuiltinsAndCacheSignatureHelp(ctx, builtins)
76+
if err != nil {
77+
t.Fatalf("failed to store builtins and cache signature help: %s", err)
78+
}
79+
8080
params := types.SignatureHelpParams{
8181
TextDocumentPositionParams: types.TextDocumentPositionParams{
8282
TextDocument: types.TextDocumentIdentifier{URI: mainRegoURI},
8383
Position: tc.position,
8484
},
8585
}
8686

87-
res, err := ls.handleTextDocumentSignatureHelp(params)
87+
res, err := ls.handleTextDocumentSignatureHelp(ctx, params)
8888
if err != nil {
8989
t.Fatalf("signature help should work, got error: %s", err)
9090
}

0 commit comments

Comments
 (0)