Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(lsp): clear stale syntax error markers by never publishing null diagnostics (#20901)

h3n4lfe6a4bbfbc45
diff --git a/backend/plugin/parser/base/diagnose_test.go b/backend/plugin/parser/base/diagnose_test.go
index 1d85b27167..e79b5b6233 100644
--- a/backend/plugin/parser/base/diagnose_test.go
+++ b/backend/plugin/parser/base/diagnose_test.go
@@ -1,6 +1,8 @@
package base
import (
+ "context"
+ "encoding/json"
"testing"
lsp "github.com/bytebase/lsp-protocol"
@@ -9,6 +11,32 @@ import (
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
)
+func TestDiagnoseNeverReturnsNil(t *testing.T) {
+ a := require.New(t)
+
+ // Engine diagnose funcs return a nil slice for valid statements.
+ RegisterDiagnoseFunc(storepb.Engine_MONGODB, func(_ context.Context, _ DiagnoseContext, _ string) ([]Diagnostic, error) {
+ return nil, nil
+ })
+
+ for _, engine := range []storepb.Engine{
+ storepb.Engine_MONGODB,
+ // Engine without a registered diagnose func.
+ storepb.Engine_ENGINE_UNSPECIFIED,
+ } {
+ diagnostics, err := Diagnose(context.Background(), DiagnoseContext{}, engine, "db.users.find()")
+ a.NoError(err)
+ a.NotNil(diagnostics)
+ a.Empty(diagnostics)
+
+ // The LSP spec requires publishing an empty array (not null) to clear
+ // previously published diagnostics on the client.
+ payload, err := json.Marshal(lsp.PublishDiagnosticsParams{Diagnostics: diagnostics})
+ a.NoError(err)
+ a.Contains(string(payload), `"diagnostics":[]`)
+ }
+}
+
func TestConvertPositionToUTF16Position(t *testing.T) {
testCases := []struct {
description string
diff --git a/backend/plugin/parser/base/interface.go b/backend/plugin/parser/base/interface.go
index 1a024f35d8..8d1519c5f4 100644
--- a/backend/plugin/parser/base/interface.go
+++ b/backend/plugin/parser/base/interface.go
@@ -167,12 +167,18 @@ func RegisterDiagnoseFunc(engine storepb.Engine, f DiagnoseFunc) {
}
// Diagnose returns the diagnostics for the statement. The diagnostics never be nil, and may not be empty although the error is not nil.
+// A non-nil result matters: publishing diagnostics as JSON null instead of an empty array
+// does not clear previously published diagnostics on the LSP client.
func Diagnose(ctx context.Context, dCtx DiagnoseContext, engine storepb.Engine, statement string) ([]Diagnostic, error) {
f, ok := diagnoseCollectors[engine]
if !ok {
return []Diagnostic{}, nil
}
- return f(ctx, dCtx, statement)
+ diagnostics, err := f(ctx, dCtx, statement)
+ if diagnostics == nil {
+ diagnostics = []Diagnostic{}
+ }
+ return diagnostics, err
}
func RegisterStatementRangesFunc(engine storepb.Engine, f StatementRangeFunc) {
diff --git a/backend/plugin/parser/mongodb/mongodb_test.go b/backend/plugin/parser/mongodb/mongodb_test.go
index 5c2d34c788..2a99ba6ce0 100644
--- a/backend/plugin/parser/mongodb/mongodb_test.go
+++ b/backend/plugin/parser/mongodb/mongodb_test.go
@@ -178,6 +178,16 @@ func TestDiagnose(t *testing.T) {
statement: `db.collection.find({})`,
wantHasDiagnostics: false,
},
+ {
+ description: "valid statement without arguments",
+ statement: `db.users.find()`,
+ wantHasDiagnostics: false,
+ },
+ {
+ description: "syntax error - mid-typing prefix without parentheses",
+ statement: `db.users.find`,
+ wantHasDiagnostics: true,
+ },
{
description: "syntax error - unclosed brace",
statement: `db.collection.find({`,