Log prettier errors on failures (#19951)

Closes https://github.com/zed-industries/zed/issues/11987

Release Notes:

- Fixed prettier not reporting failures in the status panel on
formatting and installation errors
This commit is contained in:
Kirill Bulatov 2024-10-30 14:49:47 +02:00 committed by GitHub
parent 0ba40bdfb8
commit d49cd0019f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 146 additions and 85 deletions

View file

@ -29,6 +29,7 @@ use gpui::{
Task, WeakModel,
};
use http_client::HttpClient;
use itertools::Itertools as _;
use language::{
language_settings::{
language_settings, FormatOnSave, Formatter, LanguageSettings, SelectedFormatter,
@ -144,7 +145,6 @@ pub struct LocalLspStore {
HashMap<LanguageServerId, (LanguageServerName, Arc<LanguageServer>)>,
prettier_store: Model<PrettierStore>,
current_lsp_settings: HashMap<LanguageServerName, LspSettings>,
last_formatting_failure: Option<String>,
_subscription: gpui::Subscription,
}
@ -563,9 +563,7 @@ impl LocalLspStore {
})?;
prettier_store::format_with_prettier(&prettier, &buffer.handle, cx)
.await
.transpose()
.ok()
.flatten()
.transpose()?
}
Formatter::External { command, arguments } => {
Self::format_via_external_command(buffer, command, arguments.as_deref(), cx)
@ -705,6 +703,7 @@ impl LspStoreMode {
pub struct LspStore {
mode: LspStoreMode,
last_formatting_failure: Option<String>,
downstream_client: Option<(AnyProtoClient, u64)>,
nonce: u128,
buffer_store: Model<BufferStore>,
@ -907,7 +906,6 @@ impl LspStore {
language_server_watcher_registrations: Default::default(),
current_lsp_settings: ProjectSettings::get_global(cx).lsp.clone(),
buffers_being_formatted: Default::default(),
last_formatting_failure: None,
prettier_store,
environment,
http_client,
@ -917,6 +915,7 @@ impl LspStore {
this.as_local_mut().unwrap().shutdown_language_servers(cx)
}),
}),
last_formatting_failure: None,
downstream_client: None,
buffer_store,
worktree_store,
@ -977,6 +976,7 @@ impl LspStore {
upstream_project_id: project_id,
}),
downstream_client: None,
last_formatting_failure: None,
buffer_store,
worktree_store,
languages: languages.clone(),
@ -5265,9 +5265,9 @@ impl LspStore {
.map(language::proto::serialize_transaction),
})
}
pub fn last_formatting_failure(&self) -> Option<&str> {
self.as_local()
.and_then(|local| local.last_formatting_failure.as_deref())
self.last_formatting_failure.as_deref()
}
pub fn environment_for_buffer(
@ -5338,23 +5338,16 @@ impl LspStore {
cx.clone(),
)
.await;
lsp_store.update(&mut cx, |lsp_store, _| {
let local = lsp_store.as_local_mut().unwrap();
match &result {
Ok(_) => local.last_formatting_failure = None,
Err(error) => {
local.last_formatting_failure.replace(error.to_string());
}
}
lsp_store.update_last_formatting_failure(&result);
})?;
result
})
} else if let Some((client, project_id)) = self.upstream_client() {
let buffer_store = self.buffer_store();
cx.spawn(move |_, mut cx| async move {
let response = client
cx.spawn(move |lsp_store, mut cx| async move {
let result = client
.request(proto::FormatBuffers {
project_id,
trigger: trigger as i32,
@ -5365,13 +5358,21 @@ impl LspStore {
})
.collect::<Result<_>>()?,
})
.await?
.transaction
.ok_or_else(|| anyhow!("missing transaction"))?;
.await
.and_then(|result| result.transaction.context("missing transaction"));
lsp_store.update(&mut cx, |lsp_store, _| {
lsp_store.update_last_formatting_failure(&result);
})?;
let transaction_response = result?;
buffer_store
.update(&mut cx, |buffer_store, cx| {
buffer_store.deserialize_project_transaction(response, push_to_history, cx)
buffer_store.deserialize_project_transaction(
transaction_response,
push_to_history,
cx,
)
})?
.await
})
@ -7366,6 +7367,18 @@ impl LspStore {
lsp_action,
})
}
fn update_last_formatting_failure<T>(&mut self, formatting_result: &anyhow::Result<T>) {
match &formatting_result {
Ok(_) => self.last_formatting_failure = None,
Err(error) => {
let error_string = format!("{error:#}");
log::error!("Formatting failed: {error_string}");
self.last_formatting_failure
.replace(error_string.lines().join(" "));
}
}
}
}
impl EventEmitter<LspStoreEvent> for LspStore {}