Skip to content

terminal suggest - adopt vscode.fs watcher - #276477

Merged
Benjamin Pasero (bpasero) merged 3 commits into
mainfrom
ben/managing-ferret
Nov 10, 2025
Merged

Benjamin Pasero (bpasero) merged 3 commits into
mainfrom
ben/managing-ferret

Conversation

@bpasero

@bpasero Benjamin Pasero (bpasero) commented Nov 10, 2025 •

Copy link
Copy Markdown
Contributor

Megan Rogge (@meganrogge) do you recall why you were using fs for file watching and not our vscode API?

//cc Henning Dieterichs (@hediet)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request refactors the file system watching mechanism in the terminal-suggest extension to use VS Code's createFileSystemWatcher API instead of Node.js's native fs.watch. This improves consistency with VS Code's extension patterns and ensures proper lifecycle management through the extension context.

Key Changes

  • Migrated from Node.js fs.watch to VS Code's createFileSystemWatcher API
  • Improved watcher registration by adding to context.subscriptions for automatic cleanup
  • Optimized directory checking by moving the activeWatchers.has(dir) check earlier in the loop

Comment thread extensions/terminal-suggest/src/env/pathExecutableCache.ts Outdated
Comment on lines +234 to +262
if (activeWatchers.has(dir)) {
// Skip if already watching or directory doesn't exist
continue;
}

try {
const stat = await fs.stat(dir);
if (!stat.isDirectory()) {
continue;
}
} catch {
// File not found
continue;
}

const watcher = filesystem.watch(dir, { persistent: false }, () => {
if (pathExecutableCache) {
// Refresh cache when directory contents change
pathExecutableCache.refresh(dir);
}
});
const handleChange = () => {
// Refresh cache when directory contents change
pathExecutableCache?.refresh(dir);
};

activeWatchers.add(dir);
const watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(vscode.Uri.file(dir), '*'));
context.subscriptions.push(
watcher,
watcher.onDidCreate(handleChange),
watcher.onDidChange(handleChange),
watcher.onDidDelete(handleChange)
);

context.subscriptions.push(new vscode.Disposable(() => {
try {
watcher.close();
activeWatchers.delete(dir);
} catch { } { }
}));
} catch { }
activeWatchers.add(dir);

Copilot AI Nov 10, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The activeWatchers set is scoped locally to this function invocation, which means it won't persist across multiple calls to watchPathDirectories. If this function could be called multiple times (e.g., when the PATH environment variable changes), it would create duplicate watchers for the same directories without the ability to track or clean them up. Consider moving activeWatchers to module scope or implementing a more robust tracking mechanism that persists across function calls.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

@meganrogge Megan Rogge (meganrogge) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was not aware of this API. Thank you!

@bpasero
Benjamin Pasero (bpasero) merged commit 5e5ba2f into main Nov 10, 2025
28 checks passed
@bpasero
Benjamin Pasero (bpasero) deleted the ben/managing-ferret branch November 10, 2025 17:02
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Dec 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants