Skip to content

Commit e9303ae

Browse files
authored
Fix bug where LSP crashed when a baml file not in a baml_src was opened (#2019)
<!-- ELLIPSIS_HIDDEN --> > [!IMPORTANT] > Fix LSP crash by ensuring operations only on files within `baml_src` across various handlers. > > - **Behavior**: > - Added checks in `api.rs`, `did_change.rs`, `did_change_watched_files.rs`, `did_close.rs`, `did_open.rs`, `did_save_text_document.rs`, `code_lens.rs`, `completion.rs`, `diagnostic.rs`, `format.rs`, `go_to_definition.rs`, `hover.rs`, and `rename.rs` to ensure operations are only performed on files within `baml_src`. > - If a file is not in `baml_src`, the functions return early without processing. > - **Notifications**: > - `DidChangeTextDocumentHandler`, `DidChangeWatchedFiles`, `DidCloseTextDocumentHandler`, `DidOpenTextDocumentHandler`, and `DidSaveTextDocument` now check for `baml_src` in file paths. > - **Requests**: > - `CodeLens`, `Completion`, `DocumentDiagnosticRequestHandler`, `DocumentFormatting`, `GotoDefinition`, `Hover`, and `Rename` handlers now include checks for `baml_src` in file paths. > - **Misc**: > - Added an empty `test.baml` file in `integ-tests/` for integration testing purposes. > > <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 a6035c2. You can [customize](https://app.ellipsis.dev/BoundaryML/settings/summaries) this summary. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent 505a9e9 commit e9303ae

14 files changed

Lines changed: 66 additions & 2 deletions

File tree

engine/language_server/src/server/api.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ pub(super) fn request<'a>(req: lsp_server::Request) -> Task<'a> {
134134
.map_err(|e| anyhow::anyhow!("Failed to parse JSON: {e}"))?;
135135
let url = Url::parse(&params.project_id)
136136
.map_err(|e| anyhow::anyhow!("Failed to parse URL: {e}"))?;
137+
if !url.to_string().contains("baml_src") {
138+
return Ok(());
139+
}
140+
137141
let project = session
138142
.get_or_create_project(&url.to_file_path().unwrap())
139143
.expect("Already checked for project's existence");

engine/language_server/src/server/api/notifications/did_change.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ impl SyncNotificationHandler for DidChangeTextDocumentHandler {
2828
let start_time_total = Instant::now();
2929

3030
let url = params.text_document.uri;
31+
if !url.to_string().contains("baml_src") {
32+
return Ok(());
33+
}
34+
3135
let path = url
3236
.to_file_path()
3337
.internal_error_msg("Could not convert URL to path")?;

engine/language_server/src/server/api/notifications/did_change_watched_files.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ impl super::SyncNotificationHandler for DidChangeWatchedFiles {
2121
params: types::DidChangeWatchedFilesParams,
2222
) -> Result<()> {
2323
tracing::info!("#### DidChangeWatchedFiles {:?}", params.changes);
24+
if !params
25+
.changes
26+
.iter()
27+
.any(|change| change.uri.to_string().contains("baml_src"))
28+
{
29+
return Ok(());
30+
}
31+
2432
// Filter out CHANGED events - only process CREATED and DELETED
2533
let filtered_changes: Vec<_> = params
2634
.changes

engine/language_server/src/server/api/notifications/did_close.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ impl SyncNotificationHandler for DidCloseTextDocumentHandler {
2727
_requester: &mut Requester,
2828
params: DidCloseTextDocumentParams,
2929
) -> Result<()> {
30-
tracing::info!("------------ DidCloseTextDocumentHandler");
3130
let url = params.text_document.uri;
31+
if !url.to_string().contains("baml_src") {
32+
return Ok(());
33+
}
34+
3235
let path = url
3336
.to_file_path()
3437
.internal_error_msg("Could not convert URL to path")?;

engine/language_server/src/server/api/notifications/did_open.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ impl SyncNotificationHandler for DidOpenTextDocumentHandler {
2727
tracing::info!("DidOpenTextDocumentHandler");
2828

2929
let url = params.text_document.uri;
30+
if !url.to_string().contains("baml_src") {
31+
return Ok(());
32+
}
3033

3134
// TODO: do this when server initializes instead of every time a file is opened
3235
// note this just schedules the task. It will run after the current task is done.

engine/language_server/src/server/api/notifications/did_save_text_document.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ impl super::SyncNotificationHandler for DidSaveTextDocument {
2323
) -> Result<()> {
2424
tracing::info!("Did save text document---------");
2525
let url = params.text_document.uri;
26+
if !url.to_string().contains("baml_src") {
27+
return Ok(());
28+
}
29+
2630
let path = url
2731
.to_file_path()
2832
.internal_error_msg("Could not convert URL to path")?;

engine/language_server/src/server/api/requests/code_lens.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ impl SyncRequestHandler for CodeLens {
2626
) -> Result<Option<Vec<lsp_types::CodeLens>>> {
2727
tracing::info!("CodeLens request");
2828
let url = params.text_document.uri.clone();
29+
if !url.to_string().contains("baml_src") {
30+
return Ok(None);
31+
}
32+
2933
let path = url
3034
.to_file_path()
3135
.internal_error_msg("Could not convert URL to path")?;

engine/language_server/src/server/api/requests/completion.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ impl SyncRequestHandler for Completion {
2020
_requester: &mut Requester,
2121
params: CompletionParams,
2222
) -> Result<Option<lsp_types::CompletionResponse>> {
23+
let url = params.text_document_position.text_document.uri;
24+
if !url.to_string().contains("baml_src") {
25+
return Ok(None);
26+
}
27+
2328
// TODO: Enable this only if you
2429
// 1. test on windows, with chinese characters
2530
// 2. Modify position_utils.rs to use byte offsets to account for chinese/multibyte characters

engine/language_server/src/server/api/requests/diagnostic.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::sync::{Arc, Mutex};
44
use lsp_types::request::DocumentDiagnosticRequest;
55
use lsp_types::{
66
DocumentDiagnosticParams, DocumentDiagnosticReport, DocumentDiagnosticReportResult,
7-
FullDocumentDiagnosticReport, RelatedFullDocumentDiagnosticReport, Url,
7+
FullDocumentDiagnosticReport, RelatedFullDocumentDiagnosticReport,
8+
RelatedUnchangedDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport, Url,
89
};
910

1011
use crate::baml_project::Project;
@@ -48,6 +49,17 @@ impl SyncRequestHandler for DocumentDiagnosticRequestHandler {
4849
params: DocumentDiagnosticParams,
4950
) -> Result<DocumentDiagnosticReportResult> {
5051
let url = params.text_document.uri.clone();
52+
if !url.to_string().contains("baml_src") {
53+
return Ok(DocumentDiagnosticReportResult::Report(
54+
DocumentDiagnosticReport::Unchanged(RelatedUnchangedDocumentDiagnosticReport {
55+
related_documents: None,
56+
unchanged_document_diagnostic_report: UnchangedDocumentDiagnosticReport {
57+
result_id: "".to_string(),
58+
},
59+
}),
60+
));
61+
}
62+
5163
let path = url
5264
.to_file_path()
5365
.internal_error_msg("Could not convert URL to path")?;

engine/language_server/src/server/api/requests/format.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ impl SyncRequestHandler for DocumentFormatting {
2020
_requester: &mut Requester,
2121
params: DocumentFormattingParams,
2222
) -> Result<Option<Vec<lsp_types::TextEdit>>> {
23+
let url = params.text_document.uri;
24+
if !url.to_string().contains("baml_src") {
25+
return Ok(None);
26+
}
27+
2328
// let url = &params.text_document.uri;
2429
// let path = url
2530
// .to_file_path()

0 commit comments

Comments
 (0)