@@ -120,7 +120,9 @@ export class CppProperties {
120120 private currentConfigurationIndex : PersistentFolderState < number > | undefined ;
121121 private configFileWatcher : vscode . FileSystemWatcher | null = null ;
122122 private configFileWatcherFallbackTime : Date = new Date ( ) ; // Used when file watching fails.
123- private compileCommandFileWatchers : fs . FSWatcher [ ] = [ ] ;
123+ private compileCommandsFile : vscode . Uri | undefined | null = undefined ;
124+ private compileCommandsFileWatchers : fs . FSWatcher [ ] = [ ] ;
125+ private compileCommandsFileWatcherFallbackTime : Date = new Date ( ) ; // Used when file watching fails.
124126 private defaultCompilerPath : string | null = null ;
125127 private knownCompilers ?: KnownCompiler [ ] ;
126128 private defaultCStandard : string | null = null ;
@@ -744,8 +746,8 @@ export class CppProperties {
744746 // paths are expected to have variables resolved already
745747 public updateCompileCommandsFileWatchers ( ) : void {
746748 if ( this . configurationJson ) {
747- this . compileCommandFileWatchers . forEach ( ( watcher : fs . FSWatcher ) => watcher . close ( ) ) ;
748- this . compileCommandFileWatchers = [ ] ; // reset it
749+ this . compileCommandsFileWatchers . forEach ( ( watcher : fs . FSWatcher ) => watcher . close ( ) ) ;
750+ this . compileCommandsFileWatchers = [ ] ; // reset it
749751 const filePaths : Set < string > = new Set < string > ( ) ;
750752 this . configurationJson . configurations . forEach ( c => {
751753 if ( c . compileCommands ) {
@@ -757,7 +759,7 @@ export class CppProperties {
757759 } ) ;
758760 try {
759761 filePaths . forEach ( ( path : string ) => {
760- this . compileCommandFileWatchers . push ( fs . watch ( path , ( event : string , filename : string ) => {
762+ this . compileCommandsFileWatchers . push ( fs . watch ( path , ( event : string , filename : string ) => {
761763 // Wait 1 second after a change to allow time for the write to finish.
762764 if ( this . compileCommandsFileWatcherTimer ) {
763765 clearInterval ( this . compileCommandsFileWatcherTimer ) ;
@@ -1669,12 +1671,33 @@ export class CppProperties {
16691671 } ) ;
16701672 }
16711673
1674+ public checkCompileCommands ( ) : void {
1675+ // Check for changes in case of file watcher failure.
1676+ const compileCommandsFile : string | undefined = this . CurrentConfiguration ?. compileCommands ;
1677+ if ( ! compileCommandsFile ) {
1678+ return ;
1679+ }
1680+ fs . stat ( compileCommandsFile , ( err , stats ) => {
1681+ if ( err ) {
1682+ if ( err . code === "ENOENT" && this . compileCommandsFile ) {
1683+ this . compileCommandsFileWatchers = [ ] ; // reset file watchers
1684+ this . onCompileCommandsChanged ( compileCommandsFile ) ;
1685+ this . compileCommandsFile = null ; // File deleted
1686+ }
1687+ } else if ( stats . mtime > this . compileCommandsFileWatcherFallbackTime ) {
1688+ this . compileCommandsFileWatcherFallbackTime = new Date ( ) ;
1689+ this . onCompileCommandsChanged ( compileCommandsFile ) ;
1690+ this . compileCommandsFile = vscode . Uri . file ( compileCommandsFile ) ; // File created.
1691+ }
1692+ } ) ;
1693+ }
1694+
16721695 dispose ( ) : void {
16731696 this . disposables . forEach ( ( d ) => d . dispose ( ) ) ;
16741697 this . disposables = [ ] ;
16751698
1676- this . compileCommandFileWatchers . forEach ( ( watcher : fs . FSWatcher ) => watcher . close ( ) ) ;
1677- this . compileCommandFileWatchers = [ ] ; // reset it
1699+ this . compileCommandsFileWatchers . forEach ( ( watcher : fs . FSWatcher ) => watcher . close ( ) ) ;
1700+ this . compileCommandsFileWatchers = [ ] ; // reset it
16781701
16791702 this . diagnosticCollection . dispose ( ) ;
16801703 }
0 commit comments