@@ -105,11 +105,11 @@ export interface CompilerDefaults {
105105}
106106
107107export class CppProperties {
108- private rootUri : vscode . Uri ;
108+ private rootUri : vscode . Uri | undefined ;
109109 private propertiesFile : vscode . Uri | undefined | null = undefined ; // undefined and null values are handled differently
110110 private readonly configFolder : string ;
111111 private configurationJson ?: ConfigurationJson ;
112- private currentConfigurationIndex : PersistentFolderState < number > ;
112+ private currentConfigurationIndex : PersistentFolderState < number > | undefined ;
113113 private configFileWatcher : vscode . FileSystemWatcher | null = null ;
114114 private configFileWatcherFallbackTime : Date = new Date ( ) ; // Used when file watching fails.
115115 private compileCommandFileWatchers : fs . FSWatcher [ ] = [ ] ;
@@ -137,10 +137,12 @@ export class CppProperties {
137137 // we want to track when the default includes have been added to it.
138138 private configurationIncomplete : boolean = true ;
139139
140- constructor ( rootUri : vscode . Uri , workspaceFolder : vscode . WorkspaceFolder ) {
140+ constructor ( rootUri : vscode . Uri | undefined , workspaceFolder : vscode . WorkspaceFolder | undefined ) {
141141 this . rootUri = rootUri ;
142142 let rootPath : string = rootUri ? rootUri . fsPath : "" ;
143- this . currentConfigurationIndex = new PersistentFolderState < number > ( "CppProperties.currentConfigurationIndex" , - 1 , workspaceFolder ) ;
143+ if ( workspaceFolder ) {
144+ this . currentConfigurationIndex = new PersistentFolderState < number > ( "CppProperties.currentConfigurationIndex" , - 1 , workspaceFolder ) ;
145+ }
144146 this . configFolder = path . join ( rootPath , ".vscode" ) ;
145147 this . diagnosticCollection = vscode . languages . createDiagnosticCollection ( rootPath ) ;
146148 this . buildVcpkgIncludePath ( ) ;
@@ -151,7 +153,7 @@ export class CppProperties {
151153 public get SelectionChanged ( ) : vscode . Event < number > { return this . selectionChanged . event ; }
152154 public get CompileCommandsChanged ( ) : vscode . Event < string > { return this . compileCommandsChanged . event ; }
153155 public get Configurations ( ) : Configuration [ ] | undefined { return this . configurationJson ? this . configurationJson . configurations : undefined ; }
154- public get CurrentConfigurationIndex ( ) : number { return this . currentConfigurationIndex . Value ; }
156+ public get CurrentConfigurationIndex ( ) : number { return this . currentConfigurationIndex === undefined ? 0 : this . currentConfigurationIndex . Value ; }
155157 public get CurrentConfiguration ( ) : Configuration | undefined { return this . Configurations ? this . Configurations [ this . CurrentConfigurationIndex ] : undefined ; }
156158 public get KnownCompiler ( ) : KnownCompiler [ ] | undefined { return this . knownCompilers ; }
157159
@@ -263,10 +265,12 @@ export class CppProperties {
263265 if ( resetIndex || this . CurrentConfigurationIndex < 0 ||
264266 this . CurrentConfigurationIndex >= this . configurationJson . configurations . length ) {
265267 let index : number | undefined = this . getConfigIndexForPlatform ( this . configurationJson ) ;
266- if ( index === undefined ) {
267- this . currentConfigurationIndex . setDefault ( ) ;
268- } else {
269- this . currentConfigurationIndex . Value = index ;
268+ if ( this . currentConfigurationIndex !== undefined ) {
269+ if ( index === undefined ) {
270+ this . currentConfigurationIndex . setDefault ( ) ;
271+ } else {
272+ this . currentConfigurationIndex . Value = index ;
273+ }
270274 }
271275 }
272276 this . configurationIncomplete = true ;
@@ -493,7 +497,9 @@ export class CppProperties {
493497 }
494498 }
495499
496- this . currentConfigurationIndex . Value = index ;
500+ if ( this . currentConfigurationIndex !== undefined ) {
501+ this . currentConfigurationIndex . Value = index ;
502+ }
497503 this . onSelectionChanged ( ) ;
498504 }
499505
@@ -712,7 +718,7 @@ export class CppProperties {
712718 let configNames : string [ ] | undefined = this . ConfigurationNames ;
713719 if ( configNames && this . configurationJson ) {
714720 // Use the active configuration as the default selected configuration to load on UI editor
715- this . settingsPanel . selectedConfigIndex = this . currentConfigurationIndex . Value ;
721+ this . settingsPanel . selectedConfigIndex = this . CurrentConfigurationIndex ;
716722 this . settingsPanel . createOrShow ( configNames ,
717723 this . configurationJson . configurations [ this . settingsPanel . selectedConfigIndex ] ,
718724 this . getErrorsForConfigUI ( this . settingsPanel . selectedConfigIndex ) ) ;
@@ -740,7 +746,7 @@ export class CppProperties {
740746 // The settings UI became visible or active.
741747 // Ensure settingsPanel has copy of latest current configuration
742748 if ( this . settingsPanel . selectedConfigIndex >= this . configurationJson . configurations . length ) {
743- this . settingsPanel . selectedConfigIndex = this . currentConfigurationIndex . Value ;
749+ this . settingsPanel . selectedConfigIndex = this . CurrentConfigurationIndex ;
744750 }
745751 this . settingsPanel . updateConfigUI ( configNames ,
746752 this . configurationJson . configurations [ this . settingsPanel . selectedConfigIndex ] ,
@@ -809,10 +815,12 @@ export class CppProperties {
809815 this . CurrentConfigurationIndex >= this . configurationJson . configurations . length ) {
810816 // If the index is out of bounds (during initialization or due to removal of configs), fix it.
811817 let index : number | undefined = this . getConfigIndexForPlatform ( this . configurationJson ) ;
812- if ( ! index ) {
813- this . currentConfigurationIndex . setDefault ( ) ;
814- } else {
815- this . currentConfigurationIndex . Value = index ;
818+ if ( this . currentConfigurationIndex !== undefined ) {
819+ if ( ! index ) {
820+ this . currentConfigurationIndex . setDefault ( ) ;
821+ } else {
822+ this . currentConfigurationIndex . Value = index ;
823+ }
816824 }
817825 }
818826 }
@@ -882,18 +890,22 @@ export class CppProperties {
882890 this . CurrentConfigurationIndex >= 0 && this . CurrentConfigurationIndex < this . configurationJson . configurations . length ) {
883891 for ( let i : number = 0 ; i < newJson . configurations . length ; i ++ ) {
884892 if ( newJson . configurations [ i ] . name === this . configurationJson . configurations [ this . CurrentConfigurationIndex ] . name ) {
885- this . currentConfigurationIndex . Value = i ;
893+ if ( this . currentConfigurationIndex !== undefined ) {
894+ this . currentConfigurationIndex . Value = i ;
895+ }
886896 break ;
887897 }
888898 }
889899 }
890900 this . configurationJson = newJson ;
891901 if ( this . CurrentConfigurationIndex < 0 || this . CurrentConfigurationIndex >= newJson . configurations . length ) {
892902 let index : number | undefined = this . getConfigIndexForPlatform ( newJson ) ;
893- if ( index === undefined ) {
894- this . currentConfigurationIndex . setDefault ( ) ;
895- } else {
896- this . currentConfigurationIndex . Value = index ;
903+ if ( this . currentConfigurationIndex !== undefined ) {
904+ if ( index === undefined ) {
905+ this . currentConfigurationIndex . setDefault ( ) ;
906+ } else {
907+ this . currentConfigurationIndex . Value = index ;
908+ }
897909 }
898910 }
899911
@@ -975,11 +987,13 @@ export class CppProperties {
975987
976988 // first resolve variables
977989 result = util . resolveVariables ( path , this . ExtendedEnvironment ) ;
978- if ( result . includes ( "${workspaceFolder}" ) ) {
979- result = result . replace ( "${workspaceFolder}" , this . rootUri . fsPath ) ;
980- }
981- if ( result . includes ( "${workspaceRoot}" ) ) {
982- result = result . replace ( "${workspaceRoot}" , this . rootUri . fsPath ) ;
990+ if ( this . rootUri ) {
991+ if ( result . includes ( "${workspaceFolder}" ) ) {
992+ result = result . replace ( "${workspaceFolder}" , this . rootUri . fsPath ) ;
993+ }
994+ if ( result . includes ( "${workspaceRoot}" ) ) {
995+ result = result . replace ( "${workspaceRoot}" , this . rootUri . fsPath ) ;
996+ }
983997 }
984998 if ( result . includes ( "${vcpkgRoot}" ) && util . getVcpkgRoot ( ) ) {
985999 result = result . replace ( "${vcpkgRoot}" , util . getVcpkgRoot ( ) ) ;
@@ -1040,6 +1054,8 @@ export class CppProperties {
10401054 if ( ! fs . existsSync ( resolvedCompilerPath ) ) {
10411055 if ( existsWithExeAdded ( resolvedCompilerPath ) ) {
10421056 resolvedCompilerPath += ".exe" ;
1057+ } else if ( ! this . rootUri ) {
1058+ pathExists = false ;
10431059 } else {
10441060 // Check again for a relative path.
10451061 const relativePath : string = this . rootUri . fsPath + path . sep + resolvedCompilerPath ;
@@ -1118,12 +1134,16 @@ export class CppProperties {
11181134
11191135 // Check if resolved path exists
11201136 if ( ! fs . existsSync ( resolvedPath ) ) {
1121- // Check for relative path if resolved path does not exists
1122- const relativePath : string = this . rootUri . fsPath + path . sep + resolvedPath ;
1123- if ( ! fs . existsSync ( relativePath ) ) {
1137+ if ( ! this . rootUri ) {
11241138 pathExists = false ;
11251139 } else {
1126- resolvedPath = relativePath ;
1140+ // Check for relative path if resolved path does not exists
1141+ const relativePath : string = this . rootUri . fsPath + path . sep + resolvedPath ;
1142+ if ( ! fs . existsSync ( relativePath ) ) {
1143+ pathExists = false ;
1144+ } else {
1145+ resolvedPath = relativePath ;
1146+ }
11271147 }
11281148 }
11291149
@@ -1307,6 +1327,8 @@ export class CppProperties {
13071327 if ( ! fs . existsSync ( resolvedPath ) ) {
13081328 if ( existsWithExeAdded ( resolvedPath ) ) {
13091329 resolvedPath += ".exe" ;
1330+ } else if ( ! this . rootUri ) {
1331+ pathExists = false ;
13101332 } else {
13111333 // Check again for a relative path.
13121334 const relativePath : string = this . rootUri . fsPath + path . sep + resolvedPath ;
0 commit comments