Skip to content

Commit cde22ab

Browse files
bors[bot]lnicola
andauthored
Merge #7371
7371: Change directory before running rustfmt to respect rustfmt.toml r=matklad a=lnicola Fixes #6973 Co-authored-by: Laurențiu Nicola <[email protected]>
2 parents 0045d7c + c067ca5 commit cde22ab

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

crates/rust-analyzer/src/handlers.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,23 @@ pub(crate) fn handle_formatting(
858858
RustfmtConfig::Rustfmt { extra_args } => {
859859
let mut cmd = process::Command::new(toolchain::rustfmt());
860860
cmd.args(extra_args);
861+
// try to chdir to the file so we can respect `rustfmt.toml`
862+
// FIXME: use `rustfmt --config-path` once
863+
// https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
864+
match params.text_document.uri.to_file_path() {
865+
Ok(mut path) => {
866+
// pop off file name
867+
if path.pop() && path.is_dir() {
868+
cmd.current_dir(path);
869+
}
870+
}
871+
Err(_) => {
872+
log::error!(
873+
"Unable to get file path for {}, rustfmt.toml might be ignored",
874+
params.text_document.uri
875+
);
876+
}
877+
}
861878
if let Some(&crate_id) = crate_ids.first() {
862879
// Assume all crates are in the same edition
863880
let edition = snap.analysis.crate_edition(crate_id)?;

0 commit comments

Comments
 (0)