Skip to content

Commit b1f89a8

Browse files
committed
Auto merge of #15600 - davidbarsky:davidbarsky/broken-rustfmt-in-ra, r=Veykril
fix: ensure `rustfmt` runs when configured with `./` (Hopefully) resolves rust-lang/rust-analyzer#15595. This change kinda approaches canonicalization—which I am not a fan of—but only in service of making `./`-configured commands run correctly. Longer-term, I feel like this code should be removed once `rustfmt` supports recursive searches of configuration files or interpolation of values like `${workspace_folder}` lands in rust-analyzer. ## Testing I cloned `rustc`, setup rust-analyzer as suggested in the [`rustc` dev guide](https://rustc-dev-guide.rust-lang.org/building/suggested.html#configuring-rust-analyzer-for-rustc), saved and formatted files in `src/tools/miri` and `compiler`, and saw `rustfmt` (seemingly) correctly.
2 parents ef58843 + b3ebc9a commit b1f89a8

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

crates/rust-analyzer/src/handlers/request.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::{
55
fs,
66
io::Write as _,
7+
path::PathBuf,
78
process::{self, Stdio},
89
};
910

@@ -1995,14 +1996,34 @@ fn run_rustfmt(
19951996
cmd
19961997
}
19971998
RustfmtConfig::CustomCommand { command, args } => {
1998-
let mut cmd = process::Command::new(command);
1999+
let cmd = PathBuf::from(&command);
2000+
let workspace = CargoTargetSpec::for_file(&snap, file_id)?;
2001+
let mut cmd = match workspace {
2002+
Some(spec) => {
2003+
// approach: if the command name contains a path seperator, join it with the workspace root.
2004+
// however, if the path is absolute, joining will result in the absolute path being preserved.
2005+
// as a fallback, rely on $PATH-based discovery.
2006+
let cmd_path =
2007+
if cfg!(windows) && command.contains(&[std::path::MAIN_SEPARATOR, '/']) {
2008+
spec.workspace_root.join(cmd).into()
2009+
} else if command.contains(std::path::MAIN_SEPARATOR) {
2010+
spec.workspace_root.join(cmd).into()
2011+
} else {
2012+
cmd
2013+
};
2014+
process::Command::new(cmd_path)
2015+
}
2016+
None => process::Command::new(cmd),
2017+
};
19992018

20002019
cmd.envs(snap.config.extra_env());
20012020
cmd.args(args);
20022021
cmd
20032022
}
20042023
};
20052024

2025+
tracing::debug!(?command, "created format command");
2026+
20062027
// try to chdir to the file so we can respect `rustfmt.toml`
20072028
// FIXME: use `rustfmt --config-path` once
20082029
// https://github.com/rust-lang/rustfmt/issues/4660 gets fixed

0 commit comments

Comments
 (0)