Skip to content

Commit

Permalink
Handle initialization failure according to LSP spec
Browse files Browse the repository at this point in the history
  • Loading branch information
netmute committed Dec 6, 2024
1 parent 54479fc commit f743442
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,11 @@ type Range struct {

// Server represents the language server
type Server struct {
tagEntries []TagEntry
rootPath string
cache FileCache
mu sync.Mutex
tagEntries []TagEntry
rootPath string
cache FileCache
initialized bool
mu sync.Mutex
}

// FileCache stores the content of opened files for quick access
Expand Down Expand Up @@ -377,8 +378,29 @@ Options:
`, os.Args[0])
}

// checkInitializedOrFail ensures that the server has been successfully initialized.
func checkInitializedOrFail(id json.RawMessage, server *Server, method string) bool {
// The following methods are allowed even if not initialized:
// - initialize (the first request)
// - shutdown and exit (for cleanup)
if method == "initialize" || method == "shutdown" || method == "exit" {
return true
}

if !server.initialized {
sendError(id, -32002, "Server not initialized", "Received request before successful initialization")
return false
}
return true
}

// handleRequest routes JSON-RPC requests to appropriate handlers
func handleRequest(server *Server, req RPCRequest) {
if !checkInitializedOrFail(req.ID, server, req.Method) {
// Server not initialized and request is not allowed.
return
}

switch req.Method {
case "initialize":
handleInitialize(server, req)
Expand Down Expand Up @@ -456,6 +478,7 @@ func handleInitialize(server *Server, req RPCRequest) {
}

sendResult(req.ID, result)
server.initialized = true
}

// handleInitialized processes the 'initialized' notification
Expand Down

0 comments on commit f743442

Please sign in to comment.