Skip to content

Commit

Permalink
internal: report errors on failure; clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbarsky committed May 22, 2024
1 parent f240858 commit e62932f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
8 changes: 4 additions & 4 deletions crates/flycheck/src/json_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ pub struct JsonWorkspaceHandle {
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum DiscoverProjectMessage {
Error { message: String },
Progress { message: String },
Finished { project_json: Vec<ProjectJsonData> },
}

impl ParseFromLine for DiscoverProjectMessage {
fn from_line(line: &str, _error: &mut String) -> Option<Self> {
let value = serde_json::from_str::<serde_json::Value>(&line).unwrap();

// let mut deserializer = serde_json::Deserializer::from_str(line);
// deserializer.disable_recursion_limit();
let Ok(value) = serde_json::from_str::<serde_json::Value>(line) else {
return Some(DiscoverProjectMessage::Error { message: line.to_owned() });
};

if let Ok(project) = serde_json::from_value::<ProjectJsonData>(value.clone()) {
return Some(DiscoverProjectMessage::Finished { project_json: vec![project] });
Expand Down
3 changes: 1 addition & 2 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
utf8_stdout, CargoConfig, CargoWorkspace, InvocationStrategy, ManifestPath, Package,
ProjectJson, ProjectManifest, Sysroot, TargetData, TargetKind, WorkspaceBuildScripts,
};
use tracing::{info, Level, error, debug};
use tracing::{debug, error, info, Level};

pub type FileLoader<'a> = &'a mut dyn for<'b> FnMut(&'b AbsPath) -> Option<FileId>;

Expand Down Expand Up @@ -357,7 +357,6 @@ impl ProjectWorkspace {
) -> ProjectWorkspace {
let sysroot = match (project_json.sysroot.clone(), project_json.sysroot_src.clone()) {
(Some(sysroot), Some(sysroot_src)) => {

Ok(Sysroot::load(sysroot, Some(Ok(sysroot_src)), false))
}
(Some(sysroot), None) => {
Expand Down
9 changes: 5 additions & 4 deletions crates/rust-analyzer/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,11 @@ fn run_server() -> anyhow::Result<()> {
return Err(e.into());
}

if config.discover_command().is_none() {
if !config.has_linked_projects() && config.detached_files().is_empty() {
config.rediscover_workspaces();
}
if config.discover_command().is_none()
&& !config.has_linked_projects()
&& config.detached_files().is_empty()
{
config.rediscover_workspaces();
}

// If the io_threads have an error, there's usually an error on the main
Expand Down
16 changes: 6 additions & 10 deletions crates/rust-analyzer/src/lsp/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,21 @@ impl GlobalState {
}

/// Sends a notification to the client containing the error `message`.
/// If `additional_info` is [`Some`], appends a note to the notification telling to check the logs.
/// This will always log `message` + `additional_info` to the server's error log.
/// If `additional_info` is [`Some`], the details will be included in the server's logs.
pub(crate) fn show_and_log_error(&mut self, message: String, additional_info: Option<String>) {
match additional_info {
Some(additional_info) => {
tracing::error!("{}:\n{}", &message, &additional_info);
self.show_message(
lsp_types::MessageType::ERROR,
message,
tracing::enabled!(tracing::Level::ERROR),
);
}
None => {
tracing::error!("{}", &message);
self.send_notification::<lsp_types::notification::ShowMessage>(
lsp_types::ShowMessageParams { typ: lsp_types::MessageType::ERROR, message },
);
}
}
self.show_message(
lsp_types::MessageType::ERROR,
message,
tracing::enabled!(tracing::Level::ERROR),
);
}

/// rust-analyzer is resilient -- if it fails, this doesn't usually affect
Expand Down
8 changes: 6 additions & 2 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl GlobalState {
}
Event::DiscoverProject(message) => {
self.handle_discover_msg(message);
// Coalesce many project discovery events into a single loop turn
// Coalesce many project discovery events into a single loop turn.
while let Ok(message) = self.discover_receiver.try_recv() {
self.handle_discover_msg(message);
}
Expand Down Expand Up @@ -752,7 +752,7 @@ impl GlobalState {
let id = from_proto::file_id(&snap, &uri).expect("unable to get FileId");
if let Ok(crates) = &snap.analysis.crates_for(id) {
if crates.is_empty() {
if let Some(_) = snap.config.discover_command() {
if snap.config.discover_command().is_some() {
sender.send(Task::DiscoverLinkedProjects(uri)).unwrap();
}
} else {
Expand Down Expand Up @@ -794,6 +794,10 @@ impl GlobalState {
flycheck::DiscoverProjectMessage::Progress { message } => {
self.report_progress("Buck", Progress::Report, Some(message), None, None)
}
flycheck::DiscoverProjectMessage::Error { message } => {
self.show_and_log_error(message.clone(), None);
self.report_progress("Buck", Progress::End, Some(message), None, None)
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ impl GlobalState {
);
}

if self.config.linked_or_discovered_projects() != old_config.linked_or_discovered_projects() {
if self.config.linked_or_discovered_projects() != old_config.linked_or_discovered_projects()
{
self.fetch_workspaces_queue.request_op("discovered projects changed".to_owned(), true)
} else if self.config.flycheck() != old_config.flycheck() {
self.reload_flycheck();
Expand Down

0 comments on commit e62932f

Please sign in to comment.