Skip to content

Commit

Permalink
Merge fc60f8b into 2097948
Browse files Browse the repository at this point in the history
  • Loading branch information
CxRes committed May 13, 2019
2 parents 2097948 + fc60f8b commit f144115
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -167,6 +167,10 @@ instantiating the watching as chokidar discovers these file paths (before the `r
* `followSymlinks` (default: `true`). When `false`, only the
symlinks themselves will be watched for changes instead of following
the link references and bubbling events through the link's path.
* `reportErrorOnDanglingSymlinks` (default: `false`) When `followSymlinks: true`, Chokidar silently waits for paths under
dangling symlinks to be created. The user might instead want to confine Chokidar to wait for explicitly specified paths only.
When set to `true`, the dangling symlink is reported as an error instead and the absence of underlying symlinked path
is ignored.
* `cwd` (no default). The base directory from which watch `paths` are to be
derived. Paths emitted with events will be relative to this.
* `disableGlobbing` (default: `false`). If set to `true` then the strings passed to `.watch()` and `.add()` are treated as
Expand Down
18 changes: 18 additions & 0 deletions index.js
Expand Up @@ -287,6 +287,9 @@ constructor(_opts) {
if (opts.atomic) this._pendingUnlinks = new Map();

if (undef(opts, 'followSymlinks')) opts.followSymlinks = true;

if (undef(opts, 'reportErrorOnDanglingSymlinks')) opts.reportErrorOnDanglingSymlinks = false;
if (!opts.followSymlinks) opts.reportErrorOnDanglingSymlinks = false;

if (undef(opts, 'awaitWriteFinish')) opts.awaitWriteFinish = false;
if (opts.awaitWriteFinish === true) opts.awaitWriteFinish = {};
Expand Down Expand Up @@ -589,6 +592,21 @@ _handleError(error) {
return error || this.closed;
}

/**
* Special handler for dangling symlink errors
* @param {Error} error
* @returns {Boolean} True if Dangling conditions are met
*/
_handleDanglingSymlinkError(error) {
const code = error && error.code;
if (error && this.options.reportErrorOnDanglingSymlinks && code === 'ENOENT') {
error.message = `Dangling Symlink: ${error.message}`;
this.emit('error', error);
return true;
}
}


/**
* Helper utility for throttling
* @param {ThrottleType} actionType type being throttled
Expand Down
10 changes: 9 additions & 1 deletion lib/nodefs-handler.js
Expand Up @@ -548,7 +548,15 @@ async _addToNodeFs(path, initialAdd, priorWh, depth, target) {
return false;

} catch (error) {
if (this.fsw._handleError(error)) return path;
if (this.fsw._handleError(error)) {
// If path is a symlink, test if the error is due to a dangling symlink
// Based on the options user might want to report it and move on
// (rather than having to wait for the path to be recreated)
if (this.fsw._symlinkPaths.has(wh.watchPath)) {
if (this.fsw._handleDanglingSymlinkError(error)) ready();
}
return path;
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions types/index.d.ts
Expand Up @@ -93,6 +93,15 @@ export interface WatchOptions {
*/
followSymlinks?: boolean;

/**
* When `followSymlinks: true`, Chokidar silently waits for paths under dangling symlinks to be
* created. The user might instead want to confine themselves to watch explicitly specified paths.
*
* If set to `true`, the dangling symlink is reported as an error and Chokidar ignores
* the absence of the underlying path.
*/
reportErrorOnDanglingSymlinks?: boolean;

/**
* The base directory from which watch `paths` are to be derived. Paths emitted with events will
* be relative to this.
Expand Down

0 comments on commit f144115

Please sign in to comment.