Skip to content

Commit

Permalink
fix(vscode): handle lsp crashes (#1934)
Browse files Browse the repository at this point in the history
Fixes #1890 

- Detect when FTL stops via crash and shows an error on the status bar.
- When restarting, start spinning again.
- Tested manually with both examples in #1890 and also killing the
process directly.
  • Loading branch information
gak authored Jul 2, 2024
1 parent 15a6ebf commit 2fd15ac
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 39 deletions.
52 changes: 20 additions & 32 deletions extensions/vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "https://github.com/TBD54566975/ftl-vscode"
},
"engines": {
"vscode": "^1.87.0"
"vscode": "^1.90.2"
},
"categories": [
"Other"
Expand Down Expand Up @@ -101,6 +101,6 @@
"dependencies": {
"lookpath": "^1.2.2",
"semver": "^7.6.0",
"vscode-languageclient": "^9.0.1"
"vscode-languageclient": "^10.0.0-next.8"
}
}
38 changes: 33 additions & 5 deletions extensions/vscode/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
State,
} from 'vscode-languageclient/node'
import { FTLStatus } from './status'

Expand All @@ -14,6 +15,7 @@ export class FTLClient {
private outputChannel: vscode.OutputChannel
private client: LanguageClient | undefined
private isClientStarting = false
private isExpectingStop = false

constructor(statusBar: vscode.StatusBarItem, output: vscode.OutputChannel) {
this.statusBarItem = statusBar
Expand Down Expand Up @@ -83,15 +85,42 @@ export class FTLClient {
this.outputChannel.appendLine('Starting lsp client')
try {
await this.client.start()
this.outputChannel.appendLine('Client started')
console.log(`${this.clientName} started`)
FTLStatus.buildOK(this.statusBarItem)
} catch (error) {
console.error(`Error starting ${this.clientName}: ${error}`)
FTLStatus.ftlError(this.statusBarItem, `Error starting ${this.clientName}: ${error}`)
this.outputChannel.appendLine(`Error starting ${this.clientName}: ${error}`)
}

this.client.onDidChangeState((event) => {
switch (event.newState) {
case State.Starting:
FTLStatus.ftlStarting(this.statusBarItem)
this.isClientStarting = true
break
case State.Running:
FTLStatus.buildRunning(this.statusBarItem)
this.isClientStarting = false
break
case State.Stopped:
if (this.isExpectingStop) {
FTLStatus.ftlStopped(this.statusBarItem)
} else {
FTLStatus.ftlError(this.statusBarItem, 'Client stopped unexpectedly')
this.client = undefined
}
this.isExpectingStop = false
this.isClientStarting = false
break
case State.StartFailed:
// TODO: This is new in vscode-languageclient v10.0.0 but is not being set for some reason.
// https://github.com/microsoft/vscode-languageserver-node/blob/539330136d84891d005e2d0d539932a05644ba54/client/src/common/client.ts#L1618
FTLStatus.ftlError(this.statusBarItem, 'Client start failed')
this.client = undefined
this.isClientStarting = false
break
}
})

this.isClientStarting = false
}

Expand All @@ -115,6 +144,7 @@ export class FTLClient {

console.log('Stopping client')
const serverProcess = this.client!['_serverProcess']
this.isExpectingStop = true

try {
await this.client!.stop()
Expand Down Expand Up @@ -150,7 +180,5 @@ export class FTLClient {
} else if (serverProcess && serverProcess.killed) {
console.log('Server process was already killed')
}

FTLStatus.ftlStopped(this.statusBarItem)
}
}

0 comments on commit 2fd15ac

Please sign in to comment.