Skip to content

Commit 038f982

Browse files
bors[bot]oeed
authored and
Anatol Ulrich
committed
10093: fix: Remove incorrectly filtering VS Code cargo task execution resolution by scope r=oeed a=oeed This fixes rust-lang#9093 ​introduced by rust-lang#8995. A filter was present on the function that adds the execution definition to Cargo tasks. This would mean Cargo tasks defined in workspaces (i.e. `.code-workspace` files) would not be given an execution, leading to a `There is no task provider registered for tasks of type "cargo".` error as descibed in rust-lang#9093. I have made a minimum reproduction setup [here](https://github.com/oeed/ra-workspace). This PR essentially removes that check. The `if (scope) { ... }` is to handle the case where `task.scope === undefined` using a deprecated constructor. I'm not sure if that is ever likely to occur and can remove if not needed. There is some discussion about whether it's necessary to filter the tasks before building them. From my understanding, it shouldn't be needed as we should provide an execution for all `cargo` tasks; but I'm not overly familiar with VS Code internals so I could be wrong. For more info please see [the discussion](rust-lang#8995 (comment)) on rust-lang#8995 Co-authored-by: Oliver Cooper <[email protected]>
2 parents 6a80620 + d246a5f commit 038f982

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

crates/ide/src/runnables.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,24 @@ fn find_related_tests(
229229
for (file_id, refs) in refs.into_iter().flat_map(|refs| refs.references) {
230230
let file = sema.parse(file_id);
231231
let file = file.syntax();
232-
let functions = refs.iter().filter_map(|(range, _)| {
233-
let token = file.token_at_offset(range.start()).next()?;
234-
let token = sema.descend_into_macros(token);
235-
token
236-
.ancestors()
237-
.find_map(ast::Fn::cast)
238-
.map(|f| hir::InFile::new(sema.hir_file_for(f.syntax()), f))
232+
233+
// create flattened vec of tokens
234+
let tokens = refs.iter().flat_map(|(range, _)| {
235+
match file.token_at_offset(range.start()).next() {
236+
Some(token) => sema.descend_into_macros_many(token).into_vec(),
237+
None => {
238+
vec![]
239+
}
240+
}
239241
});
240242

243+
// find first suitable ancestor
244+
let functions = tokens
245+
.map(|token| token.ancestors().find_map(ast::Fn::cast))
246+
// remove instances where no ancestor could be found ( = find_map returns `None`)
247+
.filter_map(|f| f)
248+
.map(|f| hir::InFile::new(sema.hir_file_for(f.syntax()), f));
249+
241250
for fn_def in functions {
242251
// #[test/bench] expands to just the item causing us to lose the attribute, so recover them by going out of the attribute
243252
let InFile { value: fn_def, .. } = &fn_def.node_with_attributes(sema.db);

editors/code/src/tasks.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,15 @@ class CargoTaskProvider implements vscode.TaskProvider {
5858

5959
if (definition.type === TASK_TYPE && definition.command) {
6060
const args = [definition.command].concat(definition.args ?? []);
61-
if (isWorkspaceFolder(task.scope)) {
62-
return await buildCargoTask(task.scope, definition, task.name, args, this.config.cargoRunner);
63-
}
61+
return await buildCargoTask(task.scope, definition, task.name, args, this.config.cargoRunner);
6462
}
6563

6664
return undefined;
6765
}
6866
}
6967

70-
function isWorkspaceFolder(scope?: any): scope is vscode.WorkspaceFolder {
71-
return (scope as vscode.WorkspaceFolder).name !== undefined;
72-
}
73-
7468
export async function buildCargoTask(
75-
target: vscode.WorkspaceFolder,
69+
scope: vscode.WorkspaceFolder | vscode.TaskScope | undefined,
7670
definition: CargoTaskDefinition,
7771
name: string,
7872
args: string[],
@@ -117,7 +111,9 @@ export async function buildCargoTask(
117111

118112
return new vscode.Task(
119113
definition,
120-
target,
114+
// scope can sometimes be undefined. in these situations we default to the workspace taskscope as
115+
// recommended by the official docs: https://code.visualstudio.com/api/extension-guides/task-provider#task-provider)
116+
scope ?? vscode.TaskScope.Workspace,
121117
name,
122118
TASK_SOURCE,
123119
exec,

0 commit comments

Comments
 (0)