Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file watchers for includes #4172

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading