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 f79707b commit 1003808
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 58 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
1 change: 0 additions & 1 deletion crates/project-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ doctest = false
[dependencies]
anyhow.workspace = true
cargo_metadata.workspace = true
crossbeam-channel.workspace = true
rustc-hash.workspace = true
semver.workspace = true
serde_json.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/project-model/src/project_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl ProjectJson {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProjectJsonData {
pub sysroot: Option<Utf8PathBuf>,
sysroot: Option<Utf8PathBuf>,
sysroot_src: Option<Utf8PathBuf>,
crates: Vec<CrateData>,
}
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
25 changes: 12 additions & 13 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,6 @@ config_data! {
/// The warnings will be indicated by a blue squiggly underline in code
/// and a blue icon in the `Problems Panel`.
diagnostics_warningsAsInfo: Vec<String> = vec![],
/// Enables automatic discovery of projects using the discoverCommand.
///
/// Setting this command will result in rust-analyzer starting indexing
/// only once a Rust file has been opened.
discoverCommand: Option<Vec<String>> = None,
/// These directories will be ignored by rust-analyzer. They are
/// relative to the workspace root, and globs are not supported. You may
/// also need to add the folders to Code's `files.watcherExclude`.
Expand Down Expand Up @@ -441,6 +436,12 @@ config_data! {
/// Whether to insert closing angle brackets when typing an opening angle bracket of a generic argument list.
typing_autoClosingAngleBrackets_enable: bool = false,

/// Enables automatic discovery of projects using the discoverCommand.
///
/// Setting this command will result in rust-analyzer starting indexing
/// only once a Rust file has been opened.
workspace_discoverCommand: Option<Vec<String>> = None,

/// Workspace symbol search kind.
workspace_symbol_search_kind: WorkspaceSymbolSearchKindDef = WorkspaceSymbolSearchKindDef::OnlyTypes,
/// Limits the number of items returned from a workspace symbol search (Defaults to 128).
Expand Down Expand Up @@ -656,7 +657,7 @@ config_data! {

#[derive(Debug, Clone)]
pub struct Config {
pub(crate) discovered_projects: Vec<ProjectManifest>,
discovered_projects: Vec<ProjectManifest>,
/// The workspace roots as registered by the LSP client
workspace_roots: Vec<AbsPathBuf>,
caps: lsp_types::ClientCapabilities,
Expand Down Expand Up @@ -933,12 +934,10 @@ impl Config {
pub fn add_linked_projects(&mut self, projects: Vec<ProjectJsonData>) {
let linked_projects = &mut self.client_config.global.linkedProjects;

let mut new_projects: Vec<_> =
projects.into_iter().map(ManifestOrProjectJson::ProjectJson).collect();

let new_projects = projects.into_iter().map(ManifestOrProjectJson::ProjectJson);
match linked_projects {
Some(projects) => projects.append(&mut new_projects),
None => *linked_projects = Some(new_projects),
Some(projects) => projects.append(&mut new_projects.collect::<Vec<_>>()),
None => *linked_projects = Some(new_projects.collect::<Vec<_>>()),
}
}

Expand Down Expand Up @@ -1322,7 +1321,7 @@ impl Config {
}

pub fn discover_command(&self) -> Option<Vec<String>> {
self.discoverCommand().clone()
self.workspace_discoverCommand().clone()
}

pub fn linked_or_discovered_projects(&self) -> Vec<LinkedProject> {
Expand Down Expand Up @@ -1589,7 +1588,7 @@ impl Config {
}

pub fn cargo_autoreload_config(&self) -> bool {
self.cargo_autoreload().to_owned() && self.discoverCommand().is_none()
self.cargo_autoreload().to_owned() && self.workspace_discoverCommand().is_none()
}

pub fn run_build_scripts(&self) -> bool {
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
16 changes: 8 additions & 8 deletions docs/user/generated_config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,6 @@ List of warnings that should be displayed with info severity.
The warnings will be indicated by a blue squiggly underline in code
and a blue icon in the `Problems Panel`.
--
[[rust-analyzer.discoverCommand]]rust-analyzer.discoverCommand (default: `null`)::
+
--
Enables automatic discovery of projects using the discoverCommand.

Setting this command will result in rust-analyzer starting indexing
only once a Rust file has been opened.
--
[[rust-analyzer.files.excludeDirs]]rust-analyzer.files.excludeDirs (default: `[]`)::
+
--
Expand Down Expand Up @@ -993,6 +985,14 @@ Show documentation.
--
Whether to insert closing angle brackets when typing an opening angle bracket of a generic argument list.
--
[[rust-analyzer.workspace.discoverCommand]]rust-analyzer.workspace.discoverCommand (default: `null`)::
+
--
Enables automatic discovery of projects using the discoverCommand.

Setting this command will result in rust-analyzer starting indexing
only once a Rust file has been opened.
--
[[rust-analyzer.workspace.symbol.search.kind]]rust-analyzer.workspace.symbol.search.kind (default: `"only_types"`)::
+
--
Expand Down
22 changes: 11 additions & 11 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -979,17 +979,6 @@
"type": "string"
}
},
"rust-analyzer.discoverCommand": {
"markdownDescription": "Enables automatic discovery of projects using the discoverCommand.\n\nSetting this command will result in rust-analyzer starting indexing\nonly once a Rust file has been opened.",
"default": null,
"type": [
"null",
"array"
],
"items": {
"type": "string"
}
},
"rust-analyzer.files.excludeDirs": {
"markdownDescription": "These directories will be ignored by rust-analyzer. They are\nrelative to the workspace root, and globs are not supported. You may\nalso need to add the folders to Code's `files.watcherExclude`.",
"default": [],
Expand Down Expand Up @@ -1711,6 +1700,17 @@
"default": false,
"type": "boolean"
},
"rust-analyzer.workspace.discoverCommand": {
"markdownDescription": "Enables automatic discovery of projects using the discoverCommand.\n\nSetting this command will result in rust-analyzer starting indexing\nonly once a Rust file has been opened.",
"default": null,
"type": [
"null",
"array"
],
"items": {
"type": "string"
}
},
"rust-analyzer.workspace.symbol.search.kind": {
"markdownDescription": "Workspace symbol search kind.",
"default": "only_types",
Expand Down

0 comments on commit 1003808

Please sign in to comment.