Skip to content

Commit

Permalink
Fix file watchers for includes (#4172)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcampbell-msft authored Nov 21, 2024
1 parent bc0f5ec commit 0b8ebcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 19 additions & 3 deletions src/presets/presetsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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<string, preset.PresetsFile | undefined> = new Map();
const referencedFiles: Map<string, preset.PresetsFile | undefined> =
new Map();

// Reset all changes due to expansion since parents could change
await this._presetsParser.resetPresetsFiles(
Expand All @@ -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() {
Expand Down Expand Up @@ -1620,10 +1621,25 @@ export class PresetsController implements vscode.Disposable {
*/
class FileWatcher implements vscode.Disposable {
private watchers: Map<string, chokidar.FSWatcher>;
// 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<string, () => void>, options?: chokidar.WatchOptions) {
this.watchers = new Map<string, chokidar.FSWatcher>();

// 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 });
Expand Down

0 comments on commit 0b8ebcf

Please sign in to comment.