Skip to content

Commit afc367b

Browse files
authored
Merge pull request #19571 from duncanawoods/19464-Regression-vs-code-test-explorer-skips-unit-tests-since-v0.3.2353
Fix missing test update notifications when there are hyphens in the target name and exclude dependencies from `Run All`
2 parents 96925d5 + 06770e0 commit afc367b

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

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

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -195,45 +195,35 @@ pub(crate) fn handle_view_item_tree(
195195
}
196196

197197
// cargo test requires:
198+
// - the package is a member of the workspace
199+
// - the target in the package is not a build script (custom-build)
198200
// - the package name - the root of the test identifier supplied to this handler can be
199201
// a package or a target inside a package.
200202
// - the target name - if the test identifier is a target, it's needed in addition to the
201203
// package name to run the right test
202204
// - real names - the test identifier uses the namespace form where hyphens are replaced with
203205
// underscores. cargo test requires the real name.
204206
// - the target kind e.g. bin or lib
205-
fn find_test_target(namespace_root: &str, cargo: &CargoWorkspace) -> Option<TestTarget> {
206-
cargo.packages().find_map(|p| {
207-
let package_name = &cargo[p].name;
208-
for target in cargo[p].targets.iter() {
209-
let target_name = &cargo[*target].name;
210-
if target_name.replace('-', "_") == namespace_root {
211-
return Some(TestTarget {
212-
package: package_name.clone(),
213-
target: target_name.clone(),
214-
kind: cargo[*target].kind,
215-
});
207+
fn all_test_targets(cargo: &CargoWorkspace) -> impl Iterator<Item = TestTarget> {
208+
cargo.packages().filter(|p| cargo[*p].is_member).flat_map(|p| {
209+
let package = &cargo[p];
210+
package.targets.iter().filter_map(|t| {
211+
let target = &cargo[*t];
212+
if target.kind == TargetKind::BuildScript {
213+
None
214+
} else {
215+
Some(TestTarget {
216+
package: package.name.clone(),
217+
target: target.name.clone(),
218+
kind: target.kind,
219+
})
216220
}
217-
}
218-
None
221+
})
219222
})
220223
}
221224

222-
fn get_all_targets(cargo: &CargoWorkspace) -> Vec<TestTarget> {
223-
cargo
224-
.packages()
225-
.flat_map(|p| {
226-
let package_name = &cargo[p].name;
227-
cargo[p].targets.iter().map(|target| {
228-
let target_name = &cargo[*target].name;
229-
TestTarget {
230-
package: package_name.clone(),
231-
target: target_name.clone(),
232-
kind: cargo[*target].kind,
233-
}
234-
})
235-
})
236-
.collect()
225+
fn find_test_target(namespace_root: &str, cargo: &CargoWorkspace) -> Option<TestTarget> {
226+
all_test_targets(cargo).find(|t| namespace_root == t.target.replace('-', "_"))
237227
}
238228

239229
pub(crate) fn handle_run_test(
@@ -265,7 +255,7 @@ pub(crate) fn handle_run_test(
265255
}
266256
})
267257
.collect_vec(),
268-
None => get_all_targets(cargo).into_iter().map(|target| (target, None)).collect(),
258+
None => all_test_targets(cargo).map(|target| (target, None)).collect(),
269259
};
270260

271261
for (target, path) in tests {

crates/rust-analyzer/src/main_loop.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,9 @@ impl GlobalState {
969969
TestState::Ok => lsp_ext::TestState::Passed,
970970
TestState::Failed { stdout } => lsp_ext::TestState::Failed { message: stdout },
971971
};
972-
let test_id = format!("{}::{name}", message.target.target);
972+
973+
// The notification requires the namespace form (with underscores) of the target
974+
let test_id = format!("{}::{name}", message.target.target.replace('-', "_"));
973975

974976
self.send_notification::<lsp_ext::ChangeTestState>(
975977
lsp_ext::ChangeTestStateParams { test_id, state },

0 commit comments

Comments
 (0)