Skip to content

Commit b39875b

Browse files
committed
Auto merge of #15600 - davidbarsky:davidbarsky/broken-rustfmt-in-ra, r=Veykril
fix: ensure `rustfmt` runs when configured with `./` (Hopefully) resolves #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 + 0973d78 commit b39875b

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
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,33 @@ 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 = if cfg!(windows) && command.contains(&[std::path::MAIN_SEPARATOR_STR, '/']) {
2007+
spec.workspace_root.join(cmd).into()
2008+
} else if command.contains(std::path::MAIN_SEPARATOR_STR) {
2009+
spec.workspace_root.join(cmd).into()
2010+
} else {
2011+
cmd
2012+
};
2013+
process::Command::new(cmd_path)
2014+
}
2015+
None => process::Command::new(cmd),
2016+
};
19992017

20002018
cmd.envs(snap.config.extra_env());
20012019
cmd.args(args);
20022020
cmd
20032021
}
20042022
};
20052023

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

0 commit comments

Comments
 (0)