Skip to content

Commit 6d99657

Browse files
authored
Don't block running a task if it doesn't use the active file (#8608)
1 parent e6003fe commit 6d99657

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

Extension/src/LanguageServer/cppBuildTaskProvider.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,12 @@ export class CppBuildTaskProvider implements TaskProvider {
202202
}
203203
}
204204

205+
const taskUsesActiveFile: boolean = definition.args.some(arg => arg.indexOf('${file}') >= 0); // Need to check this before ${file} is resolved
205206
const scope: WorkspaceFolder | TaskScope = folder ? folder : TaskScope.Workspace;
206207
const task: CppBuildTask = new Task(definition, scope, definition.label, CppBuildTaskProvider.CppBuildSourceStr,
207208
new CustomExecution(async (resolvedDefinition: TaskDefinition): Promise<Pseudoterminal> =>
208209
// When the task is executed, this callback will run. Here, we setup for running the task.
209-
new CustomBuildTaskTerminal(resolvedcompilerPath, resolvedDefinition.args, resolvedDefinition.options)
210+
new CustomBuildTaskTerminal(resolvedcompilerPath, resolvedDefinition.args, resolvedDefinition.options, taskUsesActiveFile)
210211
), isCl ? '$msCompile' : '$gcc');
211212

212213
task.group = TaskGroup.Build;
@@ -349,15 +350,14 @@ class CustomBuildTaskTerminal implements Pseudoterminal {
349350
public get onDidClose(): Event<number> { return this.closeEmitter.event; }
350351
private endOfLine: string = "\r\n";
351352

352-
constructor(private command: string, private args: string[], private options: cp.ExecOptions | cp.SpawnOptions | undefined) {
353+
constructor(private command: string, private args: string[], private options: cp.ExecOptions | cp.SpawnOptions | undefined, private taskUsesActiveFile: boolean) {
353354
}
354355

355356
async open(_initialDimensions: TerminalDimensions | undefined): Promise<void> {
356-
const editor: TextEditor | undefined = window.activeTextEditor;
357-
if (editor && !util.fileIsCOrCppSource(editor.document.fileName)) {
357+
if (this.taskUsesActiveFile && !util.fileIsCOrCppSource(window.activeTextEditor?.document.fileName)) {
358358
this.writeEmitter.fire(localize("cannot.build.non.cpp", 'Cannot build and debug because the active file is not a C or C++ source file.') + this.endOfLine);
359359
this.closeEmitter.fire(-1);
360-
return Promise.resolve();
360+
return;
361361
}
362362
telemetry.logLanguageServerEvent("cppBuildTaskStarted");
363363
// At this point we can start using the terminal.

Extension/src/common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ export async function getRawJson(path: string | undefined): Promise<any> {
9595
return rawElement;
9696
}
9797

98-
export function fileIsCOrCppSource(file: string): boolean {
98+
export function fileIsCOrCppSource(file?: string): boolean {
99+
if (file === undefined) {
100+
return false;
101+
}
99102
const fileExtLower: string = path.extname(file).toLowerCase();
100103
return [".cu", ".c", ".cpp", ".cc", ".cxx", ".c++", ".cp", ".tcc", ".mm", ".ino", ".ipp", ".inl"].some(ext => fileExtLower === ext);
101104
}

0 commit comments

Comments
 (0)