Skip to content

Commit

Permalink
Improve VSCode Extension so that it doesn't crash and pop open the ou…
Browse files Browse the repository at this point in the history
…tput panel (#1103)

<!-- ELLIPSIS_HIDDEN -->



> [!IMPORTANT]
> Improves error handling and logging in the VSCode extension to prevent
crashes and unnecessary output panel openings.
> 
>   - **Error Handling**:
> - `getProjectById` in `baml_project_manager.ts` now returns
`undefined` instead of throwing an error, logging the error instead.
> - Added checks in `server.ts` to handle cases where `getProjectById`
returns `undefined`, preventing further operations.
>   - **Logging**:
> - Commented out `console.log` and `console.error` bindings in
`server.ts` to prevent the output panel from opening unnecessarily.
>   - **Misc**:
> - Changed `client.outputChannel.show()` to
`client.outputChannel.show(true)` in `index.ts` to control output panel
behavior.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup>
for 2fdd7b0. It will automatically
update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
hellovai authored Oct 25, 2024
1 parent 7ca8136 commit cb5a266
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,13 @@ class BamlProjectManager {
})
}

getProjectById(id: URI): Project {
return this.get_project(uriToRootPath(id))
getProjectById(id: URI): Project | undefined {
try {
return this.get_project(uriToRootPath(id))
} catch (e) {
console.error(`Error getting project by id: ${e}`)
return undefined
}
}
}

Expand Down
16 changes: 13 additions & 3 deletions typescript/vscode-ext/packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ export function startServer(options?: LSOptions): void {
}
})

console.log = connection.console.log.bind(connection.console)
console.error = connection.console.error.bind(connection.console)
// hellovai 🤷‍♂️: I have no idea why remove this prevents Output Panel
// from not showing up. But it does still keep logs, so I guess this works.
// Adding the snippets back in, will make the Output Panel will show up in
// a very annoying way whenever we have syntax errors in the BAML files.
//
// console.log = connection.console.log.bind(connection.console)
// console.error = connection.console.error.bind(connection.console)

console.log('Starting Baml Language Server...')

Expand Down Expand Up @@ -343,7 +348,6 @@ export function startServer(options?: LSOptions): void {

documents.onDidChangeContent(async (change: { document: TextDocument }) => {
const textDocument = change.document

await bamlProjectManager.upsert_file(URI.parse(textDocument.uri), textDocument.getText())
})

Expand All @@ -360,6 +364,9 @@ export function startServer(options?: LSOptions): void {
}

const proj = bamlProjectManager.getProjectById(documentUri)
if (!proj) {
return
}

try {
const error = proj.checkVersionOnSave()
Expand Down Expand Up @@ -447,6 +454,9 @@ export function startServer(options?: LSOptions): void {
const splitWord = completionWord.split('{{')
completionWord = splitWord[splitWord.length - 1]
const proj = bamlProjectManager.getProjectById(URI.parse(doc.uri))
if (!proj) {
return undefined
}
const res = proj.verifyCompletionRequest(doc, params.position)
if (res) {
if (completionWord === '_.') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const activateClient = (
// need to append line for the show to work for some reason.
// dont delete this.
client.outputChannel.appendLine('\n')
client.outputChannel.show()
client.outputChannel.show(true)
})
client.onNotification('baml/message', (message: BAMLMessage) => {
client.outputChannel.appendLine('baml/message' + JSON.stringify(message, null, 2))
Expand Down

0 comments on commit cb5a266

Please sign in to comment.