Skip to content

Commit 2974416

Browse files
committed
fix: ensure rustfmt runs when configured with ./
1 parent cc6c820 commit 2974416

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

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

Lines changed: 40 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,52 @@ 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 mut components = cmd.components();
2001+
2002+
// to support rustc's suggested, default configuration
2003+
let mut cmd = match components.next() {
2004+
Some(std::path::Component::CurDir) => {
2005+
let rest = components.as_path();
2006+
2007+
let roots = snap
2008+
.workspaces
2009+
.iter()
2010+
.flat_map(|ws| ws.workspace_definition_path())
2011+
.collect::<Vec<&AbsPath>>();
2012+
2013+
let abs: Option<AbsPathBuf> = roots.into_iter().find_map(|base| {
2014+
let abs = base.join(rest);
2015+
std::fs::metadata(&abs).ok().map(|_| abs)
2016+
});
2017+
2018+
let command = match abs {
2019+
Some(cmd) => cmd,
2020+
None => {
2021+
tracing::error!(
2022+
rustfmt = ?command,
2023+
"Unable to make the format command an absolute path"
2024+
);
2025+
anyhow::bail!(
2026+
"Unable to make the format command an absolute path: {}",
2027+
command
2028+
);
2029+
}
2030+
};
2031+
2032+
process::Command::new(&command.as_os_str())
2033+
}
2034+
_ => process::Command::new(command),
2035+
};
19992036

20002037
cmd.envs(snap.config.extra_env());
20012038
cmd.args(args);
20022039
cmd
20032040
}
20042041
};
20052042

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

0 commit comments

Comments
 (0)