Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): don't create a new project when the configuration changes #3597

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

### Editors

#### Bug fixes

- Fix [#3577](https://github.com/biomejs/biome/issues/3577), where the update of the configuration file was resulting in the creation of a new internal project. Contributed by @ematipico

### Formatter

#### Enhancements
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ fn resolve_manifest(cli_session: &CliSession) -> Result<(), WorkspaceError> {
content: result.content,
version: 0,
})?;
workspace.update_current_project(UpdateProjectParams { path: biome_path })?;
workspace.update_current_manifest(UpdateProjectParams { path: biome_path })?;
}

Ok(())
Expand Down
14 changes: 14 additions & 0 deletions crates/biome_lsp/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::utils::into_lsp_error;
use anyhow::Error;
use biome_diagnostics::print_diagnostic_to_string;
use biome_service::WorkspaceError;
use std::fmt::{Display, Formatter};
use tower_lsp::lsp_types::MessageType;
Expand All @@ -8,6 +9,7 @@ use tower_lsp::lsp_types::MessageType;
pub enum LspError {
WorkspaceError(WorkspaceError),
Anyhow(anyhow::Error),
Error(biome_diagnostics::Error),
}

impl From<WorkspaceError> for LspError {
Expand All @@ -16,6 +18,12 @@ impl From<WorkspaceError> for LspError {
}
}

impl From<biome_diagnostics::Error> for LspError {
fn from(value: biome_diagnostics::Error) -> Self {
Self::Error(value)
}
}

impl From<anyhow::Error> for LspError {
fn from(value: Error) -> Self {
Self::Anyhow(value)
Expand All @@ -31,6 +39,7 @@ impl Display for LspError {
LspError::Anyhow(err) => {
write!(f, "{err}")
}
LspError::Error(err) => err.description(f),
}
}
}
Expand Down Expand Up @@ -60,5 +69,10 @@ pub(crate) async fn handle_lsp_error<T>(
}
},
LspError::Anyhow(err) => Err(into_lsp_error(err)),
LspError::Error(err) => {
let message = print_diagnostic_to_string(&err);
client.log_message(MessageType::ERROR, message).await;
Ok(None)
}
}
}
3 changes: 2 additions & 1 deletion crates/biome_lsp/src/handlers/analysis.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::converters::from_proto;
use crate::converters::line_index::LineIndex;
use crate::diagnostics::LspError;
use crate::session::Session;
use crate::utils;
use anyhow::{Context, Result};
Expand Down Expand Up @@ -37,7 +38,7 @@ fn fix_all_kind() -> CodeActionKind {
pub(crate) fn code_actions(
session: &Session,
params: CodeActionParams,
) -> Result<Option<CodeActionResponse>> {
) -> Result<Option<CodeActionResponse>, LspError> {
let url = params.text_document.uri.clone();
let biome_path = session.file_path(&url)?;

Expand Down
6 changes: 5 additions & 1 deletion crates/biome_lsp/src/handlers/rename.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::collections::HashMap;

use crate::converters::from_proto;
use crate::diagnostics::LspError;
use crate::{session::Session, utils};
use anyhow::{Context, Result};
use tower_lsp::lsp_types::{RenameParams, WorkspaceEdit};
use tracing::trace;

#[tracing::instrument(level = "debug", skip(session), err)]
pub(crate) fn rename(session: &Session, params: RenameParams) -> Result<Option<WorkspaceEdit>> {
pub(crate) fn rename(
session: &Session,
params: RenameParams,
) -> Result<Option<WorkspaceEdit>, LspError> {
let url = params.text_document_position.text_document.uri;
let biome_path = session.file_path(&url)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/biome_lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ impl ServerFactory {
workspace_method!(builder, unregister_project_folder);
workspace_method!(builder, open_file);
workspace_method!(builder, open_project);
workspace_method!(builder, update_current_project);
workspace_method!(builder, update_current_manifest);
workspace_method!(builder, get_syntax_tree);
workspace_method!(builder, get_control_flow_graph);
workspace_method!(builder, get_formatter_ir);
Expand Down
54 changes: 31 additions & 23 deletions crates/biome_lsp/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::converters::{negotiated_encoding, PositionEncoding, WideEncoding};
use crate::diagnostics::LspError;
use crate::documents::Document;
use crate::extension_settings::ExtensionSettings;
use crate::extension_settings::CONFIGURATION_SECTION;
Expand All @@ -7,7 +8,7 @@ use anyhow::Result;
use biome_analyze::RuleCategoriesBuilder;
use biome_configuration::ConfigurationPathHint;
use biome_console::markup;
use biome_diagnostics::PrintDescription;
use biome_diagnostics::{DiagnosticExt, Error, PrintDescription};
use biome_fs::{BiomePath, FileSystem};
use biome_service::configuration::{
load_configuration, LoadedConfiguration, PartialConfigurationExt,
Expand Down Expand Up @@ -260,13 +261,13 @@ impl Session {
/// Get a [`Document`] matching the provided [`lsp_types::Url`]
///
/// If document does not exist, result is [WorkspaceError::NotFound]
pub(crate) fn document(&self, url: &lsp_types::Url) -> Result<Document, WorkspaceError> {
pub(crate) fn document(&self, url: &lsp_types::Url) -> Result<Document, Error> {
self.documents
.read()
.unwrap()
.get(url)
.cloned()
.ok_or_else(WorkspaceError::not_found)
.ok_or_else(|| WorkspaceError::not_found().with_file_path(url.to_string()))
}

/// Set the [`Document`] for the provided [`lsp_types::Url`]
Expand Down Expand Up @@ -298,7 +299,7 @@ impl Session {
/// them to the client. Called from [`handlers::text_document`] when a file's
/// contents changes.
#[tracing::instrument(level = "trace", skip_all, fields(url = display(&url), diagnostic_count), err)]
pub(crate) async fn update_diagnostics(&self, url: lsp_types::Url) -> Result<()> {
pub(crate) async fn update_diagnostics(&self, url: lsp_types::Url) -> Result<(), LspError> {
let biome_path = self.file_path(&url)?;
let doc = self.document(&url)?;
if self.configuration_status().is_error() && !self.notified_broken_configuration() {
Expand Down Expand Up @@ -491,31 +492,38 @@ impl Session {
directory_path: configuration_path,
..
} = loaded_configuration;
info!("Loaded workspace setting");
info!("Configuration loaded successfully from disk.");
info!("Update workspace settings.");
let fs = &self.fs;

let result =
configuration.retrieve_gitignore_matches(fs, configuration_path.as_deref());

match result {
Ok((vcs_base_path, gitignore_matches)) => {
if let ConfigurationPathHint::FromWorkspace(path) = &base_path {
// We don't need the key
let _ = self.workspace.register_project_folder(
RegisterProjectFolderParams {
path: Some(path.clone()),
// This is naive, but we don't know if the user has a file already open or not, so we register every project as the current one.
// The correct one is actually set when the LSP calls `textDocument/didOpen`
set_as_current_workspace: true,
},
);
} else {
let _ = self.workspace.register_project_folder(
RegisterProjectFolderParams {
path: fs.working_directory(),
set_as_current_workspace: true,
},
);
let register_result =
if let ConfigurationPathHint::FromWorkspace(path) = &base_path {
// We don't need the key
self.workspace
.register_project_folder(RegisterProjectFolderParams {
path: Some(path.clone()),
// This is naive, but we don't know if the user has a file already open or not, so we register every project as the current one.
// The correct one is actually set when the LSP calls `textDocument/didOpen`
set_as_current_workspace: true,
})
.err()
} else {
self.workspace
.register_project_folder(RegisterProjectFolderParams {
path: fs.working_directory(),
set_as_current_workspace: true,
})
.err()
};
if let Some(error) = register_result {
error!("Failed to register the project folder: {}", error);
self.client.log_message(MessageType::ERROR, &error).await;
return ConfigurationStatus::Error;
}
let result = self.workspace.update_settings(UpdateSettingsParams {
workspace_directory: fs.working_directory(),
Expand Down Expand Up @@ -572,7 +580,7 @@ impl Session {
}
let result = self
.workspace
.update_current_project(UpdateProjectParams { path: biome_path });
.update_current_manifest(UpdateProjectParams { path: biome_path });
if let Err(err) = result {
error!("{}", err);
}
Expand Down
4 changes: 4 additions & 0 deletions crates/biome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ pub struct WorkspaceSettings {
}

impl WorkspaceSettings {
pub fn get_current_project_key(&self) -> ProjectKey {
self.current_project
}

/// Retrieves the settings of the current workspace folder
pub fn get_current_settings(&self) -> Option<&Settings> {
trace!("Current key {:?}", self.current_project);
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_service/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ pub trait Workspace: Send + Sync + RefUnwindSafe {
) -> Result<(), WorkspaceError>;

/// Sets the current project path
fn update_current_project(&self, params: UpdateProjectParams) -> Result<(), WorkspaceError>;
fn update_current_manifest(&self, params: UpdateProjectParams) -> Result<(), WorkspaceError>;

// Return a textual, debug representation of the syntax tree for a given document
fn get_syntax_tree(
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_service/src/workspace/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ where
self.request("biome/unregister_project_folder", params)
}

fn update_current_project(&self, params: UpdateProjectParams) -> Result<(), WorkspaceError> {
self.request("biome/update_current_project", params)
fn update_current_manifest(&self, params: UpdateProjectParams) -> Result<(), WorkspaceError> {
self.request("biome/update_current_manifest", params)
}

fn get_syntax_tree(
Expand Down
Loading