Skip to content

Commit a2b59b1

Browse files
committed
Report config errors via status
1 parent 79fe11c commit a2b59b1

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

crates/rust-analyzer/src/config.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -720,11 +720,11 @@ pub struct ClientCommandsConfig {
720720
}
721721

722722
#[derive(Debug)]
723-
pub struct ConfigUpdateError {
723+
pub struct ConfigError {
724724
errors: Vec<(String, serde_json::Error)>,
725725
}
726726

727-
impl fmt::Display for ConfigUpdateError {
727+
impl fmt::Display for ConfigError {
728728
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
729729
let errors = self.errors.iter().format_with("\n", |(key, e), f| {
730730
f(key)?;
@@ -733,7 +733,7 @@ impl fmt::Display for ConfigUpdateError {
733733
});
734734
write!(
735735
f,
736-
"rust-analyzer found {} invalid config value{}:\n{}",
736+
"invalid config value{}:\n{}",
737737
self.errors.len(),
738738
if self.errors.len() == 1 { "" } else { "s" },
739739
errors
@@ -777,7 +777,7 @@ impl Config {
777777
self.workspace_roots.extend(paths);
778778
}
779779

780-
pub fn update(&mut self, mut json: serde_json::Value) -> Result<(), ConfigUpdateError> {
780+
pub fn update(&mut self, mut json: serde_json::Value) -> Result<(), ConfigError> {
781781
tracing::info!("updating config from JSON: {:#}", json);
782782
if json.is_null() || json.as_object().map_or(false, |it| it.is_empty()) {
783783
return Ok(());
@@ -824,7 +824,7 @@ impl Config {
824824
if errors.is_empty() {
825825
Ok(())
826826
} else {
827-
Err(ConfigUpdateError { errors })
827+
Err(ConfigError { errors })
828828
}
829829
}
830830

crates/rust-analyzer/src/global_state.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use triomphe::Arc;
1919
use vfs::AnchoredPathBuf;
2020

2121
use crate::{
22-
config::Config,
22+
config::{Config, ConfigError},
2323
diagnostics::{CheckFixes, DiagnosticCollection},
2424
from_proto,
2525
line_index::{LineEndings, LineIndex},
@@ -56,6 +56,7 @@ pub(crate) struct GlobalState {
5656
pub(crate) task_pool: Handle<TaskPool<Task>, Receiver<Task>>,
5757

5858
pub(crate) config: Arc<Config>,
59+
pub(crate) config_errors: Option<ConfigError>,
5960
pub(crate) analysis_host: AnalysisHost,
6061
pub(crate) diagnostics: DiagnosticCollection,
6162
pub(crate) mem_docs: MemDocs,
@@ -168,6 +169,7 @@ impl GlobalState {
168169
shutdown_requested: false,
169170
last_reported_status: None,
170171
source_root_config: SourceRootConfig::default(),
172+
config_errors: Default::default(),
171173

172174
proc_macro_changed: false,
173175
// FIXME: use `Arc::from_iter` when it becomes available

crates/rust-analyzer/src/handlers/notification.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,7 @@ pub(crate) fn handle_did_change_configuration(
169169
// Note that json can be null according to the spec if the client can't
170170
// provide a configuration. This is handled in Config::update below.
171171
let mut config = Config::clone(&*this.config);
172-
if let Err(error) = config.update(json.take()) {
173-
this.show_message(
174-
lsp_types::MessageType::WARNING,
175-
error.to_string(),
176-
false,
177-
);
178-
}
172+
config.update(json.take());
179173
this.update_configuration(config);
180174
}
181175
}

crates/rust-analyzer/src/main_loop.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,11 @@ impl GlobalState {
419419

420420
if self.config.server_status_notification() {
421421
self.send_notification::<lsp_ext::ServerStatusNotification>(status);
422-
} else if let (health, Some(message)) = (status.health, &status.message) {
422+
} else if let (
423+
health @ (lsp_ext::Health::Warning | lsp_ext::Health::Error),
424+
Some(message),
425+
) = (status.health, &status.message)
426+
{
423427
let open_log_button = tracing::enabled!(tracing::Level::ERROR)
424428
&& (self.fetch_build_data_error().is_err()
425429
|| self.fetch_workspace_error().is_err());

crates/rust-analyzer/src/reload.rs

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use ide_db::{
2727
use itertools::Itertools;
2828
use proc_macro_api::{MacroDylib, ProcMacroServer};
2929
use project_model::{PackageRoot, ProjectWorkspace, WorkspaceBuildScripts};
30+
use stdx::format_to;
3031
use syntax::SmolStr;
3132
use triomphe::Arc;
3233
use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
@@ -134,6 +135,10 @@ impl GlobalState {
134135
message.push_str("Failed to discover workspace.\n");
135136
message.push_str("Consider adding the `Cargo.toml` of the workspace to the [`linkedProjects`](https://rust-analyzer.github.io/manual.html#rust-analyzer.linkedProjects) setting.\n\n");
136137
}
138+
if let Some(err) = &self.config_errors {
139+
status.health = lsp_ext::Health::Warning;
140+
format_to!(message, "{err}\n");
141+
}
137142

138143
for ws in self.workspaces.iter() {
139144
let (ProjectWorkspace::Cargo { sysroot, .. }

0 commit comments

Comments
 (0)