-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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 fs watcher EMFILE error. #8258
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dc588b4
Fix fs watcher EMFILE error.
scotthovestadt 92e0cd0
Improve types.
scotthovestadt 007d9b1
Resolve eslint error.
scotthovestadt 60a23dc
Resolve eslint error.
scotthovestadt 4f3a205
Update CHANGELOG.md
scotthovestadt 9e7be4b
Eslint on CI.
scotthovestadt c7831a9
Update CHANGELOG.md
scotthovestadt f4af601
Copyright header.
scotthovestadt 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
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -11,22 +11,29 @@ | |
"types": "build/index.d.ts", | ||
"dependencies": { | ||
"@jest/types": "^24.6.0", | ||
"anymatch": "^2.0.0", | ||
"fb-watchman": "^2.0.0", | ||
"graceful-fs": "^4.1.15", | ||
"invariant": "^2.2.4", | ||
"jest-serializer": "^24.4.0", | ||
"jest-util": "^24.6.0", | ||
"jest-worker": "^24.6.0", | ||
"micromatch": "^3.1.10", | ||
"sane": "^4.0.3" | ||
"sane": "^4.0.3", | ||
"walker": "^1.0.7" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uses same dependency as already required by |
||
}, | ||
"devDependencies": { | ||
"@types/anymatch": "^1.3.1", | ||
"@types/fb-watchman": "^2.0.0", | ||
"@types/fsevents": "^1.1.0", | ||
"@types/graceful-fs": "^4.1.2", | ||
"@types/invariant": "^2.2.29", | ||
"@types/micromatch": "^3.1.0", | ||
"@types/sane": "^2.0.0" | ||
}, | ||
"optionalDependencies": { | ||
"fsevents": "^1.2.7" | ||
}, | ||
"engines": { | ||
"node": ">= 6" | ||
}, | ||
|
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,181 @@ | ||
'use strict'; | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
import {EventEmitter} from 'events'; | ||
import anymatch from 'anymatch'; | ||
import micromatch from 'micromatch'; | ||
import {Watcher} from 'fsevents'; | ||
// @ts-ignore no types | ||
import walker from 'walker'; | ||
|
||
let fsevents: (path: string) => Watcher; | ||
try { | ||
// eslint-disable-next-line import/no-unresolved | ||
fsevents = require('fsevents'); | ||
} catch (e) { | ||
// Optional dependency, only supported on Darwin. | ||
} | ||
|
||
const CHANGE_EVENT = 'change'; | ||
const DELETE_EVENT = 'delete'; | ||
const ADD_EVENT = 'add'; | ||
const ALL_EVENT = 'all'; | ||
|
||
type FsEventsWatcherEvent = | ||
| typeof CHANGE_EVENT | ||
| typeof DELETE_EVENT | ||
| typeof ADD_EVENT | ||
| typeof ALL_EVENT; | ||
|
||
/** | ||
* Export `FSEventsWatcher` class. | ||
* Watches `dir`. | ||
*/ | ||
class FSEventsWatcher extends EventEmitter { | ||
public readonly root: string; | ||
public readonly ignored?: anymatch.Matcher; | ||
public readonly glob: Array<string>; | ||
public readonly dot: boolean; | ||
public readonly hasIgnore: boolean; | ||
public readonly doIgnore: (path: string) => boolean; | ||
public readonly watcher: Watcher; | ||
private _tracked: Set<string>; | ||
|
||
static isSupported() { | ||
return fsevents !== undefined; | ||
} | ||
|
||
private static normalizeProxy( | ||
callback: (normalizedPath: string, stats: fs.Stats) => void, | ||
) { | ||
return (filepath: string, stats: fs.Stats) => | ||
callback(path.normalize(filepath), stats); | ||
} | ||
|
||
private static recReaddir( | ||
dir: string, | ||
dirCallback: (normalizedPath: string, stats: fs.Stats) => void, | ||
fileCallback: (normalizedPath: string, stats: fs.Stats) => void, | ||
endCallback: Function, | ||
errorCallback: Function, | ||
ignored?: anymatch.Matcher, | ||
) { | ||
walker(dir) | ||
.filterDir( | ||
(currentDir: string) => !ignored || !anymatch(ignored, currentDir), | ||
) | ||
.on('dir', FSEventsWatcher.normalizeProxy(dirCallback)) | ||
.on('file', FSEventsWatcher.normalizeProxy(fileCallback)) | ||
.on('error', errorCallback) | ||
.on('end', () => { | ||
endCallback(); | ||
}); | ||
} | ||
|
||
constructor( | ||
dir: string, | ||
opts: { | ||
root: string; | ||
ignored?: anymatch.Matcher; | ||
glob: string | Array<string>; | ||
dot: boolean; | ||
}, | ||
) { | ||
if (!fsevents) { | ||
throw new Error( | ||
'`fsevents` unavailable (this watcher can only be used on Darwin)', | ||
); | ||
} | ||
|
||
super(); | ||
|
||
this.dot = opts.dot || false; | ||
this.ignored = opts.ignored; | ||
this.glob = Array.isArray(opts.glob) ? opts.glob : [opts.glob]; | ||
|
||
this.hasIgnore = | ||
Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0); | ||
this.doIgnore = opts.ignored ? anymatch(opts.ignored) : () => false; | ||
|
||
this.root = path.resolve(dir); | ||
this.watcher = fsevents(this.root); | ||
|
||
this.watcher.start().on('change', this.handleEvent.bind(this)); | ||
this._tracked = new Set(); | ||
FSEventsWatcher.recReaddir( | ||
this.root, | ||
(filepath: string) => { | ||
this._tracked.add(filepath); | ||
}, | ||
(filepath: string) => { | ||
this._tracked.add(filepath); | ||
}, | ||
this.emit.bind(this, 'ready'), | ||
this.emit.bind(this, 'error'), | ||
this.ignored, | ||
); | ||
} | ||
|
||
/** | ||
* End watching. | ||
*/ | ||
close(callback?: () => void) { | ||
this.watcher.stop(); | ||
this.removeAllListeners(); | ||
if (typeof callback === 'function') { | ||
process.nextTick(callback.bind(null, null, true)); | ||
} | ||
} | ||
|
||
private isFileIncluded(relativePath: string) { | ||
if (this.doIgnore(relativePath)) { | ||
return false; | ||
} | ||
return this.glob.length | ||
? micromatch.some(relativePath, this.glob, {dot: this.dot}) | ||
: this.dot || micromatch.some(relativePath, '**/*'); | ||
} | ||
|
||
private handleEvent(filepath: string) { | ||
const relativePath = path.relative(this.root, filepath); | ||
if (!this.isFileIncluded(relativePath)) { | ||
return; | ||
} | ||
|
||
fs.lstat(filepath, (error, stat) => { | ||
if (error && error.code !== 'ENOENT') { | ||
this.emit('error', error); | ||
return; | ||
} | ||
|
||
if (error) { | ||
// Ignore files that aren't tracked and don't exist. | ||
if (!this._tracked.has(filepath)) { | ||
return; | ||
} | ||
|
||
this._emit(DELETE_EVENT, relativePath); | ||
this._tracked.delete(filepath); | ||
return; | ||
} | ||
|
||
if (this._tracked.has(filepath)) { | ||
this._emit(CHANGE_EVENT, relativePath, stat); | ||
} else { | ||
this._tracked.add(filepath); | ||
this._emit(ADD_EVENT, relativePath, stat); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Emit events. | ||
*/ | ||
private _emit(type: FsEventsWatcherEvent, file: string, stat?: fs.Stats) { | ||
this.emit(type, file, this.root, stat); | ||
this.emit(ALL_EVENT, type, file, this.root, stat); | ||
} | ||
} | ||
|
||
export = FSEventsWatcher; |
This file contains 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
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.
Uses same dependency as already required by
sane
, just adding explicitly since we rely on it now too.