@@ -138,7 +138,7 @@ export class CppProperties {
138
138
private configFileWatcherFallbackTime : Date = new Date ( ) ; // Used when file watching fails.
139
139
private compileCommandsFile : vscode . Uri | undefined | null = undefined ;
140
140
private compileCommandsFileWatchers : fs . FSWatcher [ ] = [ ] ;
141
- private compileCommandsFileWatcherFallbackTime : Date = new Date ( ) ; // Used when file watching fails.
141
+ private compileCommandsFileWatcherFallbackTime : Map < string , Date > = new Map < string , Date > ( ) ; // Used when file watching fails.
142
142
private defaultCompilerPath : string | null = null ;
143
143
private knownCompilers ?: KnownCompiler [ ] ;
144
144
private defaultCStandard : string | null = null ;
@@ -1093,6 +1093,10 @@ export class CppProperties {
1093
1093
1094
1094
if ( configuration . compileCommands ) {
1095
1095
configuration . compileCommands = this . resolvePath ( configuration . compileCommands ) ;
1096
+ if ( ! this . compileCommandsFileWatcherFallbackTime . has ( configuration . compileCommands ) ) {
1097
+ // Start tracking the fallback time for a new path.
1098
+ this . compileCommandsFileWatcherFallbackTime . set ( configuration . compileCommands , new Date ( ) ) ;
1099
+ }
1096
1100
}
1097
1101
1098
1102
if ( configuration . forcedInclude ) {
@@ -1104,12 +1108,31 @@ export class CppProperties {
1104
1108
}
1105
1109
}
1106
1110
1111
+ this . clearStaleCompileCommandsFileWatcherFallbackTimes ( ) ;
1107
1112
this . updateCompileCommandsFileWatchers ( ) ;
1108
1113
if ( ! this . configurationIncomplete ) {
1109
1114
this . onConfigurationsChanged ( ) ;
1110
1115
}
1111
1116
}
1112
1117
1118
+ private clearStaleCompileCommandsFileWatcherFallbackTimes ( ) : void {
1119
+ // We need to keep track of relevant timestamps, so we cannot simply clear all entries.
1120
+ // Instead, we clear entries that are no longer relevant.
1121
+ const trackedCompileCommandsPaths : Set < string > = new Set ( ) ;
1122
+ this . configurationJson ?. configurations . forEach ( ( config : Configuration ) => {
1123
+ const path = this . resolvePath ( config . compileCommands ) ;
1124
+ if ( path . length > 0 ) {
1125
+ trackedCompileCommandsPaths . add ( path ) ;
1126
+ }
1127
+ } ) ;
1128
+
1129
+ for ( const path of this . compileCommandsFileWatcherFallbackTime . keys ( ) ) {
1130
+ if ( ! trackedCompileCommandsPaths . has ( path ) ) {
1131
+ this . compileCommandsFileWatcherFallbackTime . delete ( path ) ;
1132
+ }
1133
+ }
1134
+ }
1135
+
1113
1136
private compileCommandsFileWatcherTimer ?: NodeJS . Timeout ;
1114
1137
private compileCommandsFileWatcherFiles : Set < string > = new Set < string > ( ) ;
1115
1138
@@ -2310,14 +2333,18 @@ export class CppProperties {
2310
2333
fs . stat ( compileCommandsFile , ( err , stats ) => {
2311
2334
if ( err ) {
2312
2335
if ( err . code === "ENOENT" && this . compileCommandsFile ) {
2336
+ this . compileCommandsFileWatchers . forEach ( ( watcher : fs . FSWatcher ) => watcher . close ( ) ) ;
2313
2337
this . compileCommandsFileWatchers = [ ] ; // reset file watchers
2314
2338
this . onCompileCommandsChanged ( compileCommandsFile ) ;
2315
2339
this . compileCommandsFile = null ; // File deleted
2316
2340
}
2317
- } else if ( stats . mtime > this . compileCommandsFileWatcherFallbackTime ) {
2318
- this . compileCommandsFileWatcherFallbackTime = new Date ( ) ;
2319
- this . onCompileCommandsChanged ( compileCommandsFile ) ;
2320
- this . compileCommandsFile = vscode . Uri . file ( compileCommandsFile ) ; // File created.
2341
+ } else {
2342
+ const compileCommandsLastChanged : Date | undefined = this . compileCommandsFileWatcherFallbackTime . get ( compileCommandsFile ) ;
2343
+ if ( compileCommandsLastChanged !== undefined && stats . mtime > compileCommandsLastChanged ) {
2344
+ this . compileCommandsFileWatcherFallbackTime . set ( compileCommandsFile , new Date ( ) ) ;
2345
+ this . onCompileCommandsChanged ( compileCommandsFile ) ;
2346
+ this . compileCommandsFile = vscode . Uri . file ( compileCommandsFile ) ; // File created.
2347
+ }
2321
2348
}
2322
2349
} ) ;
2323
2350
}
0 commit comments