-
Notifications
You must be signed in to change notification settings - Fork 945
fix(watcher): use Watchman when available and reduce FSEvents usage #10115
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
Open
davidfirst
wants to merge
10
commits into
master
Choose a base branch
from
fix/fsevents-watchman-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+62
−14
Open
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
67230b2
fix(watcher): use Watchman when available and reduce FSEvents usage
davidfirst e95c668
Merge branch 'master' into fix/fsevents-watchman-support
davidfirst 23ad66c
fix(watcher): cache Watchman availability and add timeout
davidfirst a5fadac
fix: use correct Options type from @parcel/watcher
davidfirst a32c71c
fix: add shell:false to execSync and fix comment
davidfirst 3689733
fix: remove shell:false as it expects string, not boolean
davidfirst d603845
fix: use spawnSync instead of execSync for shell-free execution
davidfirst 93175e4
Merge branch 'master' into fix/fsevents-watchman-support
davidfirst 75a2864
fix: add error handling for spawnSync in Watchman check
davidfirst 0ed6a80
remove .only
davidfirst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,8 +26,9 @@ import type { CheckTypes } from './check-types'; | |||||||||||||||||||||
| import type { WatcherMain } from './watcher.main.runtime'; | ||||||||||||||||||||||
| import { WatchQueue } from './watch-queue'; | ||||||||||||||||||||||
| import type { Logger } from '@teambit/logger'; | ||||||||||||||||||||||
| import type { Event } from '@parcel/watcher'; | ||||||||||||||||||||||
| import type { Event, Options as ParcelWatcherOptions } from '@parcel/watcher'; | ||||||||||||||||||||||
| import ParcelWatcher from '@parcel/watcher'; | ||||||||||||||||||||||
| import { execSync } from 'child_process'; | ||||||||||||||||||||||
| import { sendEventsToClients } from '@teambit/harmony.modules.send-server-sent-events'; | ||||||||||||||||||||||
| import { WatcherDaemon, WatcherClient, getOrCreateWatcherConnection, type WatcherError } from './watcher-daemon'; | ||||||||||||||||||||||
| import { formatFSEventsErrorMessage } from './fsevents-error'; | ||||||||||||||||||||||
|
|
@@ -98,6 +99,8 @@ export class Watcher { | |||||||||||||||||||||
| private parcelSubscription: { unsubscribe: () => Promise<void> } | null = null; | ||||||||||||||||||||||
| // Signal handlers for cleanup (to avoid accumulation) | ||||||||||||||||||||||
| private signalCleanupHandler: (() => void) | null = null; | ||||||||||||||||||||||
| // Cached Watchman availability (checked once per process lifetime) | ||||||||||||||||||||||
| private watchmanAvailable: boolean | null = null; | ||||||||||||||||||||||
| constructor( | ||||||||||||||||||||||
| private workspace: Workspace, | ||||||||||||||||||||||
| private pubsub: PubsubMain, | ||||||||||||||||||||||
|
|
@@ -130,6 +133,46 @@ export class Watcher { | |||||||||||||||||||||
| ]; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Get Parcel watcher options, preferring Watchman on macOS when available. | ||||||||||||||||||||||
| * On macOS, FSEvents is the default but has a system-wide limit of ~500 streams. | ||||||||||||||||||||||
| * Watchman is a single-daemon solution that avoids this limit. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| private getParcelWatcherOptions(): ParcelWatcherOptions { | ||||||||||||||||||||||
| const options: ParcelWatcherOptions = { | ||||||||||||||||||||||
| ignore: this.getParcelIgnorePatterns(), | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // On macOS, prefer Watchman if available to avoid FSEvents stream limit | ||||||||||||||||||||||
| if (process.platform === 'darwin') { | ||||||||||||||||||||||
| if (this.isWatchmanAvailable()) { | ||||||||||||||||||||||
| options.backend = 'watchman'; | ||||||||||||||||||||||
| this.logger.debug('Using Watchman backend for file watching'); | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| this.logger.debug('Using FSEvents backend for file watching (Watchman not available)'); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return options; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Check if Watchman is installed. | ||||||||||||||||||||||
| * Result is cached to avoid repeated shell executions. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| private isWatchmanAvailable(): boolean { | ||||||||||||||||||||||
| if (this.watchmanAvailable !== null) { | ||||||||||||||||||||||
| return this.watchmanAvailable; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| try { | ||||||||||||||||||||||
| execSync('watchman version', { stdio: 'ignore', timeout: 5000 }); | ||||||||||||||||||||||
| this.watchmanAvailable = true; | ||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||
|
||||||||||||||||||||||
| } catch { | |
| if (!this.watchmanAvailable) { | |
| if (result.error) { | |
| this.logger.debug(`Watchman detection failed: ${result.error.message}`); | |
| } else { | |
| this.logger.debug(`Watchman detection failed: exited with status ${result.status}`); | |
| } | |
| } | |
| } catch (err: any) { | |
| this.logger.debug(`Watchman detection threw exception: ${err?.message ?? err}`); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
execSynccall should include theshell: falseoption for security. While the command is hardcoded (not using user input), explicitly disabling shell interpretation is a security best practice to prevent command injection vulnerabilities.Suggested fix: