Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #21534 from Veykril/push-polxrwlzrzrx
fix: Do not panic if rust-analyzer fails to spawn the discover command
Lukas Wirth 3 months ago
parent 137eee2 · parent b28aeeb · commit 1055f29
-rw-r--r--crates/rust-analyzer/src/command.rs8
-rw-r--r--crates/rust-analyzer/src/discover.rs4
-rw-r--r--crates/rust-analyzer/src/main_loop.rs34
-rw-r--r--crates/rust-analyzer/src/test_runner.rs2
4 files changed, 24 insertions, 24 deletions
diff --git a/crates/rust-analyzer/src/command.rs b/crates/rust-analyzer/src/command.rs
index 2f052618cd..49ce6db4ea 100644
--- a/crates/rust-analyzer/src/command.rs
+++ b/crates/rust-analyzer/src/command.rs
@@ -10,6 +10,7 @@ use std::{
process::{ChildStderr, ChildStdout, Command, Stdio},
};
+use anyhow::Context;
use crossbeam_channel::Sender;
use paths::Utf8PathBuf;
use process_wrap::std::{StdChildWrapper, StdCommandWrap};
@@ -156,7 +157,7 @@ impl<T: Sized + Send + 'static> CommandHandle<T> {
parser: impl JsonLinesParser<T>,
sender: Sender<T>,
out_file: Option<Utf8PathBuf>,
- ) -> std::io::Result<Self> {
+ ) -> anyhow::Result<Self> {
command.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::null());
let program = command.get_program().into();
@@ -168,7 +169,10 @@ impl<T: Sized + Send + 'static> CommandHandle<T> {
child.wrap(process_wrap::std::ProcessSession);
#[cfg(windows)]
child.wrap(process_wrap::std::JobObject);
- let mut child = child.spawn().map(JodGroupChild)?;
+ let mut child = child
+ .spawn()
+ .map(JodGroupChild)
+ .with_context(|| "Failed to spawn command: {child:?}")?;
let stdout = child.0.stdout().take().unwrap();
let stderr = child.0.stderr().take().unwrap();
diff --git a/crates/rust-analyzer/src/discover.rs b/crates/rust-analyzer/src/discover.rs
index f129f156a0..098b6a4d98 100644
--- a/crates/rust-analyzer/src/discover.rs
+++ b/crates/rust-analyzer/src/discover.rs
@@ -1,6 +1,6 @@
//! Infrastructure for lazy project discovery. Currently only support rust-project.json discovery
//! via a custom discover command.
-use std::{io, path::Path};
+use std::path::Path;
use crossbeam_channel::Sender;
use ide_db::FxHashMap;
@@ -47,7 +47,7 @@ impl DiscoverCommand {
&self,
discover_arg: DiscoverArgument,
current_dir: &Path,
- ) -> io::Result<DiscoverHandle> {
+ ) -> anyhow::Result<DiscoverHandle> {
let command = &self.command[0];
let args = &self.command[1..];
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index 71c4b2accc..f5cead5d8f 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -825,33 +825,29 @@ impl GlobalState {
}
Task::DiscoverLinkedProjects(arg) => {
if let Some(cfg) = self.config.discover_workspace_config() {
- // the clone is unfortunately necessary to avoid a borrowck error when
- // `self.report_progress` is called later
- let title = &cfg.progress_label.clone();
let command = cfg.command.clone();
let discover = DiscoverCommand::new(self.discover_sender.clone(), command);
- if self.discover_jobs_active == 0 {
- self.report_progress(title, Progress::Begin, None, None, None);
- }
- self.discover_jobs_active += 1;
-
let arg = match arg {
DiscoverProjectParam::Buildfile(it) => DiscoverArgument::Buildfile(it),
DiscoverProjectParam::Path(it) => DiscoverArgument::Path(it),
};
- let handle = discover
- .spawn(
- arg,
- &std::env::current_dir()
- .expect("Failed to get cwd during project discovery"),
- )
- .unwrap_or_else(|e| {
- panic!("Failed to spawn project discovery command: {e}")
- });
-
- self.discover_handles.push(handle);
+ match discover.spawn(arg, self.config.root_path().as_ref()) {
+ Ok(handle) => {
+ if self.discover_jobs_active == 0 {
+ let title = &cfg.progress_label.clone();
+ self.report_progress(title, Progress::Begin, None, None, None);
+ }
+ self.discover_jobs_active += 1;
+ self.discover_handles.push(handle)
+ }
+ Err(e) => self.show_message(
+ lsp_types::MessageType::ERROR,
+ format!("Failed to spawn project discovery command: {e:#}"),
+ false,
+ ),
+ }
}
}
Task::FetchBuildData(progress) => {
diff --git a/crates/rust-analyzer/src/test_runner.rs b/crates/rust-analyzer/src/test_runner.rs
index f0020f9088..0d9c8310d8 100644
--- a/crates/rust-analyzer/src/test_runner.rs
+++ b/crates/rust-analyzer/src/test_runner.rs
@@ -101,7 +101,7 @@ impl CargoTestHandle {
ws_target_dir: Option<&Utf8Path>,
test_target: TestTarget,
sender: Sender<CargoTestMessage>,
- ) -> std::io::Result<Self> {
+ ) -> anyhow::Result<Self> {
let mut cmd = toolchain::command(Tool::Cargo.path(), root, &options.extra_env);
cmd.env("RUSTC_BOOTSTRAP", "1");
cmd.arg("--color=always");