Skip to content

Commit 39f32a3

Browse files
committed
Fix build task bugs. (#6539)
* Fix build task bugs. * Use VS Code's variable resolution.
1 parent 16f3521 commit 39f32a3

3 files changed

Lines changed: 17 additions & 29 deletions

File tree

Extension/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cpptools",
33
"displayName": "C/C++",
44
"description": "C/C++ IntelliSense, debugging, and code browsing.",
5-
"version": "1.1.2",
5+
"version": "1.1.3",
66
"publisher": "ms-vscode",
77
"icon": "LanguageCCPP_color_128x.png",
88
"readme": "README.md",

Extension/src/Debugger/configurationProvider.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,16 @@ class CppConfigurationProvider implements vscode.DebugConfigurationProvider {
150150
}
151151
// Filter out build tasks that don't match the currently selected debug configuration type.
152152
buildTasks = buildTasks.filter((task: CppBuildTask) => {
153+
const command: string = task.definition.command as string;
154+
if (!command) {
155+
return false;
156+
}
153157
if (defaultConfig.name.startsWith("(Windows) ")) {
154-
if ((task.definition.command as string).includes("cl.exe")) {
158+
if (command.includes("cl.exe")) {
155159
return true;
156160
}
157161
} else {
158-
if (!(task.definition.command as string).includes("cl.exe")) {
162+
if (!command.includes("cl.exe")) {
159163
return true;
160164
}
161165
}

Extension/src/LanguageServer/cppBuildTaskProvider.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* ------------------------------------------------------------------------------------------ */
55
import * as path from 'path';
66
import {
7-
TaskDefinition, Task, TaskGroup, WorkspaceFolder, ShellExecution, Uri, workspace,
7+
TaskDefinition, Task, TaskGroup, ShellExecution, Uri, workspace,
88
TaskProvider, TaskScope, CustomExecution, ProcessExecution, TextEditor, Pseudoterminal, EventEmitter, Event, TerminalDimensions, window
99
} from 'vscode';
1010
import * as os from 'os';
@@ -185,9 +185,9 @@ export class CppBuildTaskProvider implements TaskProvider {
185185

186186
const scope: TaskScope = TaskScope.Workspace;
187187
const task: CppBuildTask = new Task(definition, scope, definition.label, CppBuildTaskProvider.CppBuildSourceStr,
188-
new CustomExecution(async (): Promise<Pseudoterminal> =>
188+
new CustomExecution(async (resolvedDefinition: TaskDefinition): Promise<Pseudoterminal> =>
189189
// When the task is executed, this callback will run. Here, we setup for running the task.
190-
new CustomBuildTaskTerminal(resolvedcompilerPath, definition ? definition.args : [], definition ? definition.options : undefined)
190+
new CustomBuildTaskTerminal(resolvedcompilerPath, resolvedDefinition.args, resolvedDefinition.options)
191191
), isCl ? '$msCompile' : '$gcc');
192192

193193
task.group = TaskGroup.Build;
@@ -200,6 +200,9 @@ export class CppBuildTaskProvider implements TaskProvider {
200200
const rawJson: any = await this.getRawTasksJson();
201201
const rawTasksJson: any = (!rawJson.tasks) ? new Array() : rawJson.tasks;
202202
const buildTasksJson: CppBuildTask[] = rawTasksJson.map((task: any) => {
203+
if (!task.label) {
204+
return null;
205+
}
203206
const definition: CppBuildTaskDefinition = {
204207
type: task.type,
205208
label: task.label,
@@ -211,7 +214,7 @@ export class CppBuildTaskProvider implements TaskProvider {
211214
cppBuildTask.detail = task.detail;
212215
return cppBuildTask;
213216
});
214-
return buildTasksJson;
217+
return buildTasksJson.filter((task: CppBuildTask) => task !== null);
215218
}
216219

217220
public async ensureBuildTaskExists(taskLabel: string): Promise<void> {
@@ -343,16 +346,16 @@ class CustomBuildTaskTerminal implements Pseudoterminal {
343346

344347
private async doBuild(): Promise<any> {
345348
// Do build.
346-
let activeCommand: string = util.resolveVariables(this.command, this.AdditionalEnvironment);
349+
let activeCommand: string = util.resolveVariables(this.command);
347350
this.args.forEach(value => {
348-
let temp: string = util.resolveVariables(value, this.AdditionalEnvironment);
351+
let temp: string = util.resolveVariables(value);
349352
if (temp && temp.includes(" ")) {
350353
temp = "\"" + temp + "\"";
351354
}
352355
activeCommand = activeCommand + " " + temp;
353356
});
354357
if (this.options?.cwd) {
355-
this.options.cwd = util.resolveVariables(this.options.cwd, this.AdditionalEnvironment);
358+
this.options.cwd = util.resolveVariables(this.options.cwd);
356359
}
357360

358361
const splitWriteEmitter = (lines: string | Buffer) => {
@@ -382,23 +385,4 @@ class CustomBuildTaskTerminal implements Pseudoterminal {
382385
this.closeEmitter.fire(-1);
383386
}
384387
}
385-
386-
private get AdditionalEnvironment(): { [key: string]: string | string[] } | undefined {
387-
const editor: TextEditor | undefined = window.activeTextEditor;
388-
if (!editor) {
389-
return undefined;
390-
}
391-
const fileDir: WorkspaceFolder | undefined = workspace.getWorkspaceFolder(editor.document.uri);
392-
if (!fileDir) {
393-
window.showErrorMessage('This command is not yet available for single-file mode.');
394-
return undefined;
395-
}
396-
const file: string = editor.document.fileName;
397-
return {
398-
"file": file,
399-
"fileDirname": path.parse(file).dir,
400-
"fileBasenameNoExtension": path.parse(file).name,
401-
"workspaceFolder": fileDir.uri.fsPath
402-
};
403-
}
404388
}

0 commit comments

Comments
 (0)