Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand targets names while searching for matching build task #3009

Merged
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Bug Fixes:
- CMake kits fails when parsing exported functions after running environmentSetupScript. [#2676](https://github.com/microsoft/vscode-cmake-tools/issues/2686)
- Implement cmake.parseBuildDiagnostics. [#1932](https://github.com/microsoft/vscode-cmake-tools/issues/1932)
- CMake tools not fully loaded when opening multi-project folders. [#3000](https://github.com/microsoft/vscode-cmake-tools/issues/3000)
- Expand variables in task's targets while searching matching taks. [#2970](https://github.com/microsoft/vscode-cmake-tools/issues/2970) [@piomis](https://github.com/piomis)
- Fix typo in `cmake.skipConfigureWhenCachePresent`. [#3040](https://github.com/microsoft/vscode-cmake-tools/issues/3040) [@Mlekow](https://github.com/Mlekow)

## 1.13.45
Expand Down Expand Up @@ -109,7 +110,7 @@ Bug Fixes:
- Revert back to the previous CMake language server extension dependency. [PR #2599](https://github.com/microsoft/vscode-cmake-tools/pull/2599)

Bug Fixes:
- Ninja is used as a default generator. [#2598](https://github.com/microsoft/vscode-cmake-tools/issues/2598)
- Ninja is used as a default generator. [#2598](https://github.com/microsoft/vscode-cmake-tools/issues/2598)

## 1.11.25
Improvements:
Expand Down
33 changes: 24 additions & 9 deletions src/cmakeTaskProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as preset from '@cmt/preset';
import { UseCMakePresets } from './config';
import * as telemetry from '@cmt/telemetry';
import * as util from '@cmt/util';
import * as expand from '@cmt/expand';

nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
Expand Down Expand Up @@ -186,18 +187,30 @@ export class CMakeTaskProvider implements vscode.TaskProvider {
return task;
}

public static async findBuildTask(presetName?: string, targets?: string[]): Promise<CMakeTask | undefined> {
public static async findBuildTask(presetName?: string, targets?: string[], expansionOptions?: expand.ExpansionOptions): Promise<CMakeTask | undefined> {
// Fetch all CMake task from `tasks.json` files.
const allTasks: vscode.Task[] = await vscode.tasks.fetchTasks({ type: CMakeTaskProvider.CMakeScriptType });
const tasks: (CMakeTask | undefined)[] = allTasks.map((task: any) => {

const tasks: (CMakeTask | undefined)[] = await Promise.all(allTasks.map(async (task: any) => {
if (!task.definition.label || !task.group || (task.group && task.group.id !== vscode.TaskGroup.Build.id)) {
return undefined;
}

let taskTargets: string[];
if (expansionOptions) {
taskTargets = await expand.expandStrings(task.definition.targets, expansionOptions);
if (task.definition.options?.cwd){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added cwd to be expanded too

task.definition.options.cwd = await expand.expandString(task.definition.options.cwd, expansionOptions);
}
} else {
taskTargets = task.definition.targets;
}

const definition: CMakeTaskDefinition = {
type: task.definition.type,
label: task.definition.label,
command: task.definition.command,
targets: task.definition.targets || targets,
targets: taskTargets || targets,
preset: task.definition.preset,
options: task.definition.options
};
Expand All @@ -208,7 +221,7 @@ export class CMakeTaskProvider implements vscode.TaskProvider {
buildTask.isDefault = true;
}
return buildTask;
});
}));

const buildTasks: CMakeTask[] = tasks.filter((task) => task !== undefined) as CMakeTask[];

Expand Down Expand Up @@ -237,13 +250,15 @@ export class CMakeTaskProvider implements vscode.TaskProvider {
} else {
// Search for the matching default task.
const defaultTask: CMakeTask[] = matchingTargetTasks.filter(task => task.isDefault);
if (defaultTask.length === 1) {
if (defaultTask.length >= 1) {
return defaultTask[0];
} else {
// Search for the matching existing task.
const existingTask: CMakeTask[] = matchingTargetTasks.filter(task => !task.isTemplate);
if (existingTask.length === 1) {
return existingTask[0];
// If there is no default task, matchingTargetTasks is a mixture of template and defined tasks.
// If there is only one task, that task is a template, so return the template.
// If there are only two tasks, the first one is always a template, and the second one is the defined task that we are searching for.
// But if there are more than two tasks, it means that there are multiple defiend tasks and none are set as default. So ask the user to choose one later.
if (matchingTargetTasks.length == 1 || matchingTargetTasks.length == 2) {
return matchingTargetTasks[matchingTargetTasks.length];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/cmakeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,7 @@ export abstract class CMakeDriver implements vscode.Disposable {
}
const useBuildTask: boolean = this.config.buildTask && isBuildCommand === true;
if (useBuildTask) {
const task: CMakeTask | undefined = await CMakeTaskProvider.findBuildTask(this._buildPreset?.name, targets);
const task: CMakeTask | undefined = await CMakeTaskProvider.findBuildTask(this._buildPreset?.name, targets, this.expansionOptions);
if (task) {
const resolvedTask: CMakeTask | undefined = await CMakeTaskProvider.resolveInternalTask(task);
if (resolvedTask) {
Expand Down