Skip to content

Commit defc911

Browse files
committed
fix(lsp): project loading
1 parent 0ac9adb commit defc911

File tree

5 files changed

+182
-52
lines changed

5 files changed

+182
-52
lines changed

.changeset/rich-beds-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [](). The Biome Language Server had a regression where opening an editor with a file already open wouldn't load the project settings correctly.

crates/biome_configuration/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ pub struct ConfigurationPayload {
605605
pub external_resolution_base_path: Utf8PathBuf,
606606
}
607607

608-
#[derive(Debug, Default, PartialEq, Clone)]
608+
#[derive(Debug, Default, PartialEq, Clone, Eq, Hash)]
609609
pub enum ConfigurationPathHint {
610610
/// The default mode, not having a configuration file is not an error.
611611
/// The path will be filled with the working directory if it is not filled at the time of usage.

crates/biome_lsp/src/handlers/text_document.rs

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
use std::sync::Arc;
2-
31
use crate::diagnostics::LspError;
2+
use crate::session::ConfigurationStatus;
43
use crate::utils::apply_document_changes;
54
use crate::{documents::Document, session::Session};
5+
use biome_configuration::ConfigurationPathHint;
66
use biome_service::workspace::{
77
ChangeFileParams, CloseFileParams, DocumentFileSource, FeaturesBuilder, FileContent,
88
GetFileContentParams, IgnoreKind, OpenFileParams, PathIsIgnoredParams,
99
};
10-
use tower_lsp_server::lsp_types;
11-
use tower_lsp_server::lsp_types::MessageType;
12-
use tracing::{debug, error, field};
10+
use camino::Utf8PathBuf;
11+
use std::sync::Arc;
12+
use tower_lsp_server::{UriExt, lsp_types};
13+
use tracing::{debug, error, field, info};
1314

1415
/// Handler for `textDocument/didOpen` LSP notification
1516
#[tracing::instrument(
@@ -31,22 +32,76 @@ pub(crate) async fn did_open(
3132

3233
let path = session.file_path(&url)?;
3334

34-
let status = session.configuration_status();
35+
eprintln!("Session id {:?}", session.key);
36+
let project_key = match session.project_for_path(&path) {
37+
Some(project_key) => project_key,
38+
None => {
39+
info!("No open project for path: {path:?}. Opening new project.");
40+
41+
let project_path = path
42+
.parent()
43+
.map(|parent| parent.to_path_buf())
44+
.unwrap_or_default();
45+
46+
// We first check if the current file belongs to one of the folders that we registered.
47+
// Ifo so, we return the project folder, otherwise we use the folder provided by the function did_open
48+
let project_path = if let Some(workspace_folders) = session.get_workspace_folders() {
49+
workspace_folders
50+
.iter()
51+
.find_map(|folder| {
52+
folder
53+
.uri
54+
.to_file_path()
55+
.map(|p| {
56+
Utf8PathBuf::from_path_buf(p.to_path_buf())
57+
.expect("To have a valid UTF-8 path")
58+
})
59+
.map(|folder| {
60+
if project_path.starts_with(&folder) {
61+
folder
62+
} else {
63+
project_path.clone()
64+
}
65+
})
66+
})
67+
.unwrap_or(project_path.clone())
68+
} else if let Some(base_path) = session.base_path() {
69+
if project_path.starts_with(&base_path) {
70+
base_path
71+
} else {
72+
project_path.clone()
73+
}
74+
} else {
75+
project_path
76+
};
77+
78+
session.set_configuration_status(ConfigurationStatus::Loading);
79+
eprintln!(
80+
"Loading configuration from text_document {:?}",
81+
&project_path
82+
);
83+
let status = session
84+
.load_biome_configuration_file(ConfigurationPathHint::FromLsp(project_path), false)
85+
.await;
86+
87+
session.set_configuration_status(status);
88+
89+
if status.is_loaded() {
90+
match session.project_for_path(&path) {
91+
Some(project_key) => project_key,
92+
93+
None => {
94+
error!("Could not find project for {path}");
95+
96+
return Ok(());
97+
}
98+
}
99+
} else {
100+
error!("Configuration could not be loaded for {path}");
35101

36-
let project_key = if status.is_loaded() {
37-
match session.project_for_path(&path) {
38-
Some(project_key) => project_key,
39-
None => {
40-
error!("Could not find project for {path}");
41102
return Ok(());
42103
}
43104
}
44-
} else {
45-
if status.is_plugin_error() {
46-
session.client.show_message(MessageType::WARNING, "The plugin loading has failed. Biome will report only parsing errors until the file is fixed or its usage is disabled.").await;
47-
}
48-
error!("Configuration could not be loaded for {path}");
49-
return Ok(());
50105
};
51106

52107
let is_ignored = session

crates/biome_lsp/src/server.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::session::{
66
};
77
use crate::utils::{into_lsp_error, panic_to_lsp_error};
88
use crate::{handlers, requests};
9-
use biome_configuration::ConfigurationPathHint;
109
use biome_console::markup;
1110
use biome_diagnostics::panic::PanicError;
1211
use biome_fs::{ConfigName, MemoryFileSystem, OsFileSystem};
@@ -310,13 +309,10 @@ impl LanguageServer for LSPServer {
310309
}
311310

312311
#[tracing::instrument(level = "debug", skip_all)]
313-
async fn initialized(&self, params: InitializedParams) {
314-
let _ = params;
315-
312+
async fn initialized(&self, _params: InitializedParams) {
316313
info!("Attempting to load the configuration from 'biome.json' file");
317-
318314
self.session.load_extension_settings().await;
319-
self.session.load_workspace_settings().await;
315+
self.session.load_workspace_settings(false).await;
320316

321317
let msg = format!("Server initialized with PID: {}", std::process::id());
322318
self.session
@@ -360,7 +356,7 @@ impl LanguageServer for LSPServer {
360356
|| watched_file.ends_with(".editorconfig"))
361357
{
362358
self.session.load_extension_settings().await;
363-
self.session.load_workspace_settings().await;
359+
self.session.load_workspace_settings(true).await;
364360
self.setup_capabilities().await;
365361
self.session.update_all_diagnostics().await;
366362
// for now we are only interested to the configuration file,
@@ -411,18 +407,8 @@ impl LanguageServer for LSPServer {
411407
}
412408
}
413409

414-
for added in &params.event.added {
415-
if let Ok(project_path) = self.session.file_path(&added.uri) {
416-
let status = self
417-
.session
418-
.load_biome_configuration_file(ConfigurationPathHint::FromWorkspace(
419-
project_path.to_path_buf(),
420-
))
421-
.await;
422-
debug!("Configuration status: {status:?}");
423-
self.session.set_configuration_status(status);
424-
}
425-
}
410+
self.session.update_workspace_folders(params.event.added);
411+
self.session.load_workspace_settings(true).await;
426412
}
427413

428414
async fn code_action(&self, params: CodeActionParams) -> LspResult<Option<CodeActionResponse>> {

0 commit comments

Comments
 (0)