Skip to content

Commit d35c014

Browse files
authored
fix: don't report error when rslint config not found (#252)
1 parent 94c7b64 commit d35c014

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

internal/lsp/server.go

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func ptrTo[T any](v T) *T {
3838
// LSP Server implementation
3939
type LSPServer struct {
4040
conn *jsonrpc2.Conn
41-
rootURI string
4241
documents map[lsproto.DocumentUri]string // URI -> content
4342
diagnostics map[lsproto.DocumentUri][]rule.RuleDiagnostic // URI -> diagnostics
4443
// align with https://github.com/microsoft/typescript-go/blob/5cdf239b02006783231dd4da8ca125cef398cd27/internal/lsp/server.go#L147
@@ -93,21 +92,35 @@ func (s *LSPServer) Handle(requestCtx context.Context, conn *jsonrpc2.Conn, req
9392
case "initialize":
9493
return s.handleInitialize(ctx, req)
9594
case "initialized":
96-
// Client finished initialization
97-
return nil, nil
95+
return s.handleInitialized(ctx, req)
9896
case "textDocument/didOpen":
97+
if s.rslintConfig == nil {
98+
return nil, nil
99+
}
99100
s.handleDidOpen(ctx, req)
100101
return nil, nil
101102
case "textDocument/didChange":
103+
if s.rslintConfig == nil {
104+
return nil, nil
105+
}
102106
s.handleDidChange(ctx, req)
103107
return nil, nil
104108
case "textDocument/didSave":
109+
if s.rslintConfig == nil {
110+
return nil, nil
111+
}
105112
s.handleDidSave(ctx, req)
106113
return nil, nil
107114
case "textDocument/diagnostic":
115+
if s.rslintConfig == nil {
116+
return nil, nil
117+
}
108118
s.handleDidSave(ctx, req)
109119
return nil, nil
110120
case "textDocument/codeAction":
121+
if s.rslintConfig == nil {
122+
return nil, nil
123+
}
111124
return s.handleCodeAction(ctx, req)
112125
case "shutdown":
113126
return s.handleShutdown(ctx, req)
@@ -133,15 +146,34 @@ func (s *LSPServer) handleInitialize(ctx context.Context, req *jsonrpc2.Request)
133146
Message: "Initialize params cannot be nil",
134147
}
135148
}
149+
result := &lsproto.InitializeResult{
150+
Capabilities: &lsproto.ServerCapabilities{
151+
TextDocumentSync: &lsproto.TextDocumentSyncOptionsOrKind{
152+
Kind: ptrTo(lsproto.TextDocumentSyncKindFull),
153+
},
154+
CodeActionProvider: &lsproto.BooleanOrCodeActionOptions{
155+
Boolean: ptrTo(true),
156+
},
157+
},
158+
}
159+
160+
return result, nil
161+
}
162+
func (s *LSPServer) handleInitialized(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) {
163+
// Check if params is nil
164+
if req.Params == nil {
165+
return nil, &jsonrpc2.Error{
166+
Code: jsonrpc2.CodeInvalidParams,
167+
Message: "Initialized params cannot be nil",
168+
}
169+
}
136170

137-
// Parse initialize params
138-
var params lsproto.InitializeParams
171+
// Parse initialized params
172+
var params lsproto.InitializedParams
139173
if err := json.Unmarshal(*req.Params, &params); err != nil {
140-
s.rootURI = "."
141-
} else {
142-
//nolint
143-
if params.RootUri.DocumentUri != nil {
144-
s.rootURI = uriToPath(*params.RootUri.DocumentUri)
174+
return nil, &jsonrpc2.Error{
175+
Code: jsonrpc2.CodeInvalidParams,
176+
Message: "Invalid initialized params",
145177
}
146178
}
147179

@@ -157,7 +189,7 @@ func (s *LSPServer) handleInitialize(ctx context.Context, req *jsonrpc2.Request)
157189
rslintConfigPath, configFound = findRslintConfig(s.fs, s.cwd)
158190

159191
if !configFound {
160-
return nil, errors.New("config file not found")
192+
return nil, nil
161193
}
162194

163195
// Load rslint configuration and extract tsconfig paths
@@ -176,22 +208,8 @@ func (s *LSPServer) handleInitialize(ctx context.Context, req *jsonrpc2.Request)
176208
return nil, errors.New("no TypeScript configurations found in rslint config")
177209
}
178210

179-
// Do not pre-create configured projects here. The service will create
180-
// configured or inferred projects on demand when files are opened.
181-
result := &lsproto.InitializeResult{
182-
Capabilities: &lsproto.ServerCapabilities{
183-
TextDocumentSync: &lsproto.TextDocumentSyncOptionsOrKind{
184-
Kind: ptrTo(lsproto.TextDocumentSyncKindFull),
185-
},
186-
CodeActionProvider: &lsproto.BooleanOrCodeActionOptions{
187-
Boolean: ptrTo(true),
188-
},
189-
},
190-
}
191-
192-
return result, nil
211+
return nil, nil
193212
}
194-
195213
func (s *LSPServer) handleDidOpen(ctx context.Context, req *jsonrpc2.Request) {
196214
log.Printf("Handling didOpen: %+v,%+v", req, ctx)
197215
var params lsproto.DidOpenTextDocumentParams

0 commit comments

Comments
 (0)