Skip to content

Commit b7997e0

Browse files
authored
Merge pull request rust-lang#19017 from Veykril/push-uktrsknwmsvy
fix: Fix flycheck panicking with "once" invocation strategy
2 parents c3e88ae + dfd9490 commit b7997e0

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/tools/rust-analyzer/crates/rust-analyzer/src/flycheck.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ pub(crate) enum FlycheckConfig {
8888
},
8989
}
9090

91+
impl FlycheckConfig {
92+
pub(crate) fn invocation_strategy_once(&self) -> bool {
93+
match self {
94+
FlycheckConfig::CargoCommand { .. } => false,
95+
FlycheckConfig::CustomCommand { invocation_strategy, .. } => {
96+
*invocation_strategy == InvocationStrategy::Once
97+
}
98+
}
99+
}
100+
}
101+
91102
impl fmt::Display for FlycheckConfig {
92103
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93104
match self {

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/notification.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,15 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
291291
let file_id = state.vfs.read().0.file_id(&vfs_path);
292292
if let Some(file_id) = file_id {
293293
let world = state.snapshot();
294+
let invocation_strategy_once = state.config.flycheck(None).invocation_strategy_once();
294295
let may_flycheck_workspace = state.config.flycheck_workspace(None);
295296
let mut updated = false;
296297
let task = move || -> std::result::Result<(), ide::Cancelled> {
298+
if invocation_strategy_once {
299+
let saved_file = vfs_path.as_path().map(|p| p.to_owned());
300+
world.flycheck[0].restart_workspace(saved_file.clone());
301+
}
302+
297303
let target = TargetSpec::for_file(&world, file_id)?.and_then(|it| {
298304
let tgt_kind = it.target_kind();
299305
let (tgt_name, root, package) = match it {
@@ -320,16 +326,15 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
320326
// the user opted into package checks then
321327
let package_check_allowed = target.is_some() || !may_flycheck_workspace;
322328
if package_check_allowed {
323-
let workspace =
324-
world.workspaces.iter().enumerate().find(|(_, ws)| match &ws.kind {
325-
project_model::ProjectWorkspaceKind::Cargo { cargo, .. }
326-
| project_model::ProjectWorkspaceKind::DetachedFile {
327-
cargo: Some((cargo, _, _)),
328-
..
329-
} => *cargo.workspace_root() == root,
330-
_ => false,
331-
});
332-
if let Some((idx, _)) = workspace {
329+
let workspace = world.workspaces.iter().position(|ws| match &ws.kind {
330+
project_model::ProjectWorkspaceKind::Cargo { cargo, .. }
331+
| project_model::ProjectWorkspaceKind::DetachedFile {
332+
cargo: Some((cargo, _, _)),
333+
..
334+
} => *cargo.workspace_root() == root,
335+
_ => false,
336+
});
337+
if let Some(idx) = workspace {
333338
world.flycheck[idx].restart_for_package(package, target);
334339
}
335340
}

0 commit comments

Comments
 (0)