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

Follow symlinks outside of watched folder #232

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ watchpack high level API doesn't map directly to watchers. Instead a three level
- The real watchers are created by the `DirectoryWatcher`.
- Files are never watched directly. This should keep the watcher count low.
- Watching can be started in the past. This way watching can start after file reading.
- Symlinks are not followed, instead the symlink is watched.

## API

Expand Down
70 changes: 46 additions & 24 deletions lib/DirectoryWatcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
Expand Down Expand Up @@ -61,6 +61,7 @@
this.watcherManager = watcherManager;
this.options = options;
this.path = directoryPath;
this.watchingSymlink = false;
// safeTime is the point in time after which reading is safe to be unchanged
// timestamp is a value that should be compared with another timestamp (mtime)
/** @type {Map<string, { safeTime: number, timestamp: number }} */
Expand Down Expand Up @@ -111,8 +112,8 @@
this.watchInParentDirectory();
}
this.watcher = watchEventSource.watch(this.path);
this.watcher.on("change", this.onWatchEvent.bind(this));
this.watcher.on("error", this.onWatcherError.bind(this));
this.watcher.on("change", this.onWatchEvent.bind(this));
}
} catch (err) {
this.onWatcherError(err);
Expand Down Expand Up @@ -393,6 +394,7 @@
const checkStats = () => {
if (this.closed) return;
this._activeEvents.set(filename, false);

fs.lstat(filePath, (err, stats) => {
if (this.closed) return;
if (this._activeEvents.get(filename) === true) {
Expand Down Expand Up @@ -549,11 +551,6 @@
fs.readdir(this.path, (err, items) => {
if (this.closed) return;
if (err) {
if (err.code === "ENOENT" || err.code === "EPERM") {
this.onDirectoryRemoved("scan readdir failed");
} else {
this.onScanError(err);
}
this.initialScan = false;
this.initialScanFinished = Date.now();
if (initial) {
Expand Down Expand Up @@ -626,22 +623,7 @@
}
});
for (const itemPath of itemPaths) {
fs.lstat(itemPath, (err2, stats) => {
if (this.closed) return;
if (err2) {
if (
err2.code === "ENOENT" ||
err2.code === "EPERM" ||
err2.code === "EACCES" ||
err2.code === "EBUSY"
) {
this.setMissing(itemPath, initial, "scan (" + err2.code + ")");
} else {
this.onScanError(err2);
}
itemFinished();
return;
}
const handleStats = (stats, symlinkStats) => {
if (stats.isFile() || stats.isSymbolicLink()) {
if (stats.mtime) {
ensureFsAccuracy(stats.mtime);
Expand All @@ -653,7 +635,11 @@
true,
"scan (file)"
);
} else if (stats.isDirectory()) {
}
if (
stats.isDirectory() ||
(symlinkStats && symlinkStats.isDirectory())
) {
if (!initial || !this.directories.has(itemPath))
this.setDirectory(
itemPath,
Expand All @@ -663,6 +649,42 @@
);
}
itemFinished();
};
fs.lstat(itemPath, (err2, stats) => {
if (this.closed) return;
if (err2) {
if (
err2.code === "ENOENT" ||
err2.code === "EPERM" ||
err2.code === "EACCES" ||
err2.code === "EBUSY"

Check warning on line 660 in lib/DirectoryWatcher.js

View check run for this annotation

Codecov / codecov/patch

lib/DirectoryWatcher.js#L656-L660

Added lines #L656 - L660 were not covered by tests
) {
this.setMissing(itemPath, initial, "scan (" + err2.code + ")");

Check warning on line 662 in lib/DirectoryWatcher.js

View check run for this annotation

Codecov / codecov/patch

lib/DirectoryWatcher.js#L662

Added line #L662 was not covered by tests
} else {
this.onScanError(err2);

Check warning on line 664 in lib/DirectoryWatcher.js

View check run for this annotation

Codecov / codecov/patch

lib/DirectoryWatcher.js#L664

Added line #L664 was not covered by tests
}
itemFinished();
return;

Check warning on line 667 in lib/DirectoryWatcher.js

View check run for this annotation

Codecov / codecov/patch

lib/DirectoryWatcher.js#L666-L667

Added lines #L666 - L667 were not covered by tests
}
if (
stats.isSymbolicLink() &&
this.watcherManager.options.followSymlinks
) {
fs.stat(itemPath, (err3, symlinkStats) => {
if (this.closed) return;
// something is wrong with the symlink, but not with the file itself
if (err3) {
handleStats(stats);
this.watchingSymlink = false;
return;

Check warning on line 679 in lib/DirectoryWatcher.js

View check run for this annotation

Codecov / codecov/patch

lib/DirectoryWatcher.js#L677-L679

Added lines #L677 - L679 were not covered by tests
}
this.watchingSymlink = true;
handleStats(stats, symlinkStats);
});
} else {
this.watchingSymlink = false;
handleStats(stats);
}
});
}
itemFinished();
Expand Down
16 changes: 16 additions & 0 deletions test/Watchpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,8 @@ describe("Watchpack", function() {
testHelper.symlinkFile(path.join("a", "b", "link"), "c");
testHelper.symlinkFile(path.join("a", "b", "link2"), "link");
testHelper.symlinkFile("link2", "link/link/link2");
testHelper.dir("b");
testHelper.symlinkDir(path.join("b", "link"), path.join("..", "a", "b"));

testHelper.tick(1000, done);
});
Expand Down Expand Up @@ -1368,6 +1370,20 @@ describe("Watchpack", function() {
}
);
});

it("should detect a change to symlinked file outside watched directory", function(done) {
expectWatchEvent(
[],
path.join(fixtures, "b"),
changes => {
Array.from(changes).should.be.eql([path.join(fixtures, "b")]);
done();
},
() => {
testHelper.file(path.join("a", "b", "d"));
}
);
});
});
} else {
it("symlinks");
Expand Down
Loading