@@ -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.
0 commit comments