From 0b8ebcfdb1077e59b1037be913f37be7e719254b Mon Sep 17 00:00:00 2001 From: Garrett Campbell <86264750+gcampbell-msft@users.noreply.github.com> Date: Thu, 21 Nov 2024 12:17:36 -0800 Subject: [PATCH] Fix file watchers for includes (#4172) --- CHANGELOG.md | 1 + src/presets/presetsController.ts | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82106dd93..cac98e039 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ Bug Fixes: - Ensure that we're sanitizing paths for `cmake.copyCompileCommands`. [#3874](https://github.com/microsoft/vscode-cmake-tools/issues/3874) - Ensure that tests are updated after a build. [#4148](https://github.com/microsoft/vscode-cmake-tools/pull/4148) - Fix various GCC compiler errors and GCC linker errors not showing up in Problems View [#2864](https://github.com/microsoft/vscode-cmake-tools/issues/2864) +- Fix reloading presets when included files are changed or renamed and updated. [#3963](https://github.com/microsoft/vscode-cmake-tools/issues/3963) ## 1.19.52 diff --git a/src/presets/presetsController.ts b/src/presets/presetsController.ts index 80aa3445c..066f596d2 100644 --- a/src/presets/presetsController.ts +++ b/src/presets/presetsController.ts @@ -69,8 +69,6 @@ export class PresetsController implements vscode.Disposable { // there might be timing issues, since listeners are invoked async. await presetsController.reapplyPresets(); - await presetsController.watchPresetsChange(); - project.workspaceContext.config.onChange('allowCommentsInPresetsFile', async () => { await presetsController.reapplyPresets(); vscode.workspace.textDocuments.forEach(doc => { @@ -140,7 +138,8 @@ export class PresetsController implements vscode.Disposable { // Need to reapply presets every time presets changed since the binary dir or cmake path could change // (need to clean or reload driver) async reapplyPresets() { - const referencedFiles: Map = new Map(); + const referencedFiles: Map = + new Map(); // Reset all changes due to expansion since parents could change await this._presetsParser.resetPresetsFiles( @@ -158,6 +157,8 @@ export class PresetsController implements vscode.Disposable { await this.setConfigurePreset(this.project.configurePreset.name); } // Don't need to set build/test presets here since they are reapplied in setConfigurePreset + + await this.watchPresetsChange(); } private showNameInputBox() { @@ -1620,10 +1621,25 @@ export class PresetsController implements vscode.Disposable { */ class FileWatcher implements vscode.Disposable { private watchers: Map; + // Debounce the change handler to avoid multiple changes being triggered by a single file change. Two change events are coming in rapid succession without this. + private canRunChangeHandler = true; public constructor(paths: string | string[], eventHandlers: Map void>, options?: chokidar.WatchOptions) { this.watchers = new Map(); + // We debounce the change event to avoid multiple changes being triggered by a single file change. + const onChange = eventHandlers.get('change'); + if (onChange) { + const debouncedOnChange = () => { + if (this.canRunChangeHandler) { + onChange(); + this.canRunChangeHandler = false; + setTimeout(() => (this.canRunChangeHandler = true), 500); + } + }; + eventHandlers.set("change", debouncedOnChange); + } + for (const path of Array.isArray(paths) ? paths : [paths]) { try { const watcher = chokidar.watch(path, { ...options });