Skip to content

Commit

Permalink
Auto merge of rust-lang#16153 - Veykril:err-msg, r=Veykril
Browse files Browse the repository at this point in the history
fix: Notify user that linkedProjects is set when failing to discover projects

Fixes rust-lang/rust-analyzer#15171
  • Loading branch information
bors committed Jan 2, 2024
2 parents df94a87 + 8136e73 commit 1a0b772
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
29 changes: 18 additions & 11 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
//! configure the server itself, feature flags are passed into analysis, and
//! tweak things like automatic insertion of `()` in completions.
use std::{fmt, iter, ops::Not, path::PathBuf};
use std::{
fmt, iter,
ops::Not,
path::{Path, PathBuf},
};

use cfg::{CfgAtom, CfgDiff};
use flycheck::FlycheckConfig;
Expand Down Expand Up @@ -920,7 +924,19 @@ impl Config {
pub fn has_linked_projects(&self) -> bool {
!self.data.linkedProjects.is_empty()
}
pub fn linked_projects(&self) -> Vec<LinkedProject> {
pub fn linked_manifests(&self) -> impl Iterator<Item = &Path> + '_ {
self.data.linkedProjects.iter().filter_map(|it| match it {
ManifestOrProjectJson::Manifest(p) => Some(&**p),
ManifestOrProjectJson::ProjectJson(_) => None,
})
}
pub fn has_linked_project_jsons(&self) -> bool {
self.data
.linkedProjects
.iter()
.any(|it| matches!(it, ManifestOrProjectJson::ProjectJson(_)))
}
pub fn linked_or_discovered_projects(&self) -> Vec<LinkedProject> {
match self.data.linkedProjects.as_slice() {
[] => {
let exclude_dirs: Vec<_> =
Expand Down Expand Up @@ -955,15 +971,6 @@ impl Config {
}
}

pub fn add_linked_projects(&mut self, linked_projects: Vec<ProjectJsonData>) {
let mut linked_projects = linked_projects
.into_iter()
.map(ManifestOrProjectJson::ProjectJson)
.collect::<Vec<ManifestOrProjectJson>>();

self.data.linkedProjects.append(&mut linked_projects);
}

pub fn did_save_text_document_dynamic_registration(&self) -> bool {
let caps = try_or_def!(self.caps.text_document.as_ref()?.synchronization.clone()?);
caps.did_save == Some(true) && caps.dynamic_registration == Some(true)
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ pub(crate) fn handle_runnables(
}
}
None => {
if !snap.config.linked_projects().is_empty() {
if !snap.config.linked_or_discovered_projects().is_empty() {
res.push(lsp_ext::Runnable {
label: "cargo check --workspace".to_string(),
location: None,
Expand Down
23 changes: 19 additions & 4 deletions crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ impl GlobalState {
&self.config.lru_query_capacities().cloned().unwrap_or_default(),
);
}
if self.config.linked_projects() != old_config.linked_projects() {
if self.config.linked_or_discovered_projects() != old_config.linked_or_discovered_projects()
{
self.fetch_workspaces_queue.request_op("linked projects changed".to_string(), false)
} else if self.config.flycheck() != old_config.flycheck() {
self.reload_flycheck();
Expand Down Expand Up @@ -128,7 +129,7 @@ impl GlobalState {
status.health = lsp_ext::Health::Warning;
message.push_str("Auto-reloading is disabled and the workspace has changed, a manual workspace reload is required.\n\n");
}
if self.config.linked_projects().is_empty()
if self.config.linked_or_discovered_projects().is_empty()
&& self.config.detached_files().is_empty()
&& self.config.notifications().cargo_toml_not_found
{
Expand Down Expand Up @@ -174,7 +175,21 @@ impl GlobalState {

if let Err(_) = self.fetch_workspace_error() {
status.health = lsp_ext::Health::Error;
message.push_str("Failed to load workspaces.\n\n");
message.push_str("Failed to load workspaces.");

if self.config.has_linked_projects() {
message.push_str(
"`rust-analyzer.linkedProjects` have been specified, which may be incorrect. Specified project paths:\n",
);
message.push_str(&format!(
" {}",
self.config.linked_manifests().map(|it| it.display()).format("\n ")
));
if self.config.has_linked_project_jsons() {
message.push_str("\nAdditionally, one or more project jsons are specified")
}
}
message.push_str("\n\n");
}

if !message.is_empty() {
Expand All @@ -187,7 +202,7 @@ impl GlobalState {
tracing::info!(%cause, "will fetch workspaces");

self.task_pool.handle.spawn_with_sender(ThreadIntent::Worker, {
let linked_projects = self.config.linked_projects();
let linked_projects = self.config.linked_or_discovered_projects();
let detached_files = self.config.detached_files().to_vec();
let cargo_config = self.config.cargo();

Expand Down

0 comments on commit 1a0b772

Please sign in to comment.