Unnamed repository; edit this file 'description' to name the repository.
fix: ensure `rustfmt` runs when configured with `./`
David Barsky 2023-09-13
parent cc6c820 · commit 2974416
-rw-r--r--crates/rust-analyzer/src/handlers/request.rs41
1 files changed, 40 insertions, 1 deletions
diff --git a/crates/rust-analyzer/src/handlers/request.rs b/crates/rust-analyzer/src/handlers/request.rs
index b8a1a39be1..ad6319586c 100644
--- a/crates/rust-analyzer/src/handlers/request.rs
+++ b/crates/rust-analyzer/src/handlers/request.rs
@@ -4,6 +4,7 @@
use std::{
fs,
io::Write as _,
+ path::PathBuf,
process::{self, Stdio},
};
@@ -1995,7 +1996,43 @@ fn run_rustfmt(
cmd
}
RustfmtConfig::CustomCommand { command, args } => {
- let mut cmd = process::Command::new(command);
+ let cmd = PathBuf::from(&command);
+ let mut components = cmd.components();
+
+ // to support rustc's suggested, default configuration
+ let mut cmd = match components.next() {
+ Some(std::path::Component::CurDir) => {
+ let rest = components.as_path();
+
+ let roots = snap
+ .workspaces
+ .iter()
+ .flat_map(|ws| ws.workspace_definition_path())
+ .collect::<Vec<&AbsPath>>();
+
+ let abs: Option<AbsPathBuf> = roots.into_iter().find_map(|base| {
+ let abs = base.join(rest);
+ std::fs::metadata(&abs).ok().map(|_| abs)
+ });
+
+ let command = match abs {
+ Some(cmd) => cmd,
+ None => {
+ tracing::error!(
+ rustfmt = ?command,
+ "Unable to make the format command an absolute path"
+ );
+ anyhow::bail!(
+ "Unable to make the format command an absolute path: {}",
+ command
+ );
+ }
+ };
+
+ process::Command::new(&command.as_os_str())
+ }
+ _ => process::Command::new(command),
+ };
cmd.envs(snap.config.extra_env());
cmd.args(args);
@@ -2003,6 +2040,8 @@ fn run_rustfmt(
}
};
+ tracing::debug!(?command, "created format command");
+
// try to chdir to the file so we can respect `rustfmt.toml`
// FIXME: use `rustfmt --config-path` once
// https://github.com/rust-lang/rustfmt/issues/4660 gets fixed