Skip to content

Commit 9af1a22

Browse files
authored
Merge pull request #21084 from Veykril/push-skkpkokzvyyz
fix: Fix formatting request blocking on `crate_def_map` query
2 parents 509b181 + 6cada7a commit 9af1a22

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

crates/rust-analyzer/src/global_state.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,14 @@ impl GlobalStateSnapshot {
781781

782782
pub(crate) fn target_spec_for_crate(&self, crate_id: Crate) -> Option<TargetSpec> {
783783
let file_id = self.analysis.crate_root(crate_id).ok()?;
784+
self.target_spec_for_file(file_id, crate_id)
785+
}
786+
787+
pub(crate) fn target_spec_for_file(
788+
&self,
789+
file_id: FileId,
790+
crate_id: Crate,
791+
) -> Option<TargetSpec> {
784792
let path = self.vfs_read().file_path(file_id).clone();
785793
let path = path.as_path()?;
786794

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,21 +2348,9 @@ fn run_rustfmt(
23482348
let file_id = try_default!(from_proto::file_id(snap, &text_document.uri)?);
23492349
let file = snap.analysis.file_text(file_id)?;
23502350

2351-
// Determine the edition of the crate the file belongs to (if there's multiple, we pick the
2352-
// highest edition).
2353-
let Ok(editions) = snap
2354-
.analysis
2355-
.relevant_crates_for(file_id)?
2356-
.into_iter()
2357-
.map(|crate_id| snap.analysis.crate_edition(crate_id))
2358-
.collect::<Result<Vec<_>, _>>()
2359-
else {
2360-
return Ok(None);
2361-
};
2362-
let edition = editions.iter().copied().max();
2363-
23642351
let line_index = snap.file_line_index(file_id)?;
23652352
let source_root_id = snap.analysis.source_root_id(file_id).ok();
2353+
let crates = snap.analysis.relevant_crates_for(file_id)?;
23662354

23672355
// try to chdir to the file so we can respect `rustfmt.toml`
23682356
// FIXME: use `rustfmt --config-path` once
@@ -2383,6 +2371,17 @@ fn run_rustfmt(
23832371

23842372
let mut command = match snap.config.rustfmt(source_root_id) {
23852373
RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => {
2374+
// Determine the edition of the crate the file belongs to (if there's multiple, we pick the
2375+
// highest edition).
2376+
let Ok(editions) = crates
2377+
.iter()
2378+
.map(|&crate_id| snap.analysis.crate_edition(crate_id))
2379+
.collect::<Result<Vec<_>, _>>()
2380+
else {
2381+
return Ok(None);
2382+
};
2383+
let edition = editions.iter().copied().max();
2384+
23862385
// FIXME: Set RUSTUP_TOOLCHAIN
23872386
let mut cmd = toolchain::command(
23882387
toolchain::Tool::Rustfmt.path(),
@@ -2429,7 +2428,8 @@ fn run_rustfmt(
24292428
}
24302429
RustfmtConfig::CustomCommand { command, args } => {
24312430
let cmd = Utf8PathBuf::from(&command);
2432-
let target_spec = TargetSpec::for_file(snap, file_id)?;
2431+
let target_spec =
2432+
crates.first().and_then(|&crate_id| snap.target_spec_for_file(file_id, crate_id));
24332433
let extra_env = snap.config.extra_env(source_root_id);
24342434
let mut cmd = match target_spec {
24352435
Some(TargetSpec::Cargo(_)) => {

0 commit comments

Comments
 (0)