Skip to content

Commit 3cc37aa

Browse files
fix: treat a file where a watched directory should be like a missing directory
1 parent 2ef4ad3 commit 3cc37aa

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎.changeset/missing-under-file.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"watchpack": patch
3+
---
4+
5+
Watch a missing file whose parent path is a file (e.g. `src/foo/index.js`, tried by a resolver alias, while `src/foo` is a file) like one whose parent is missing: no more `ENOTDIR` "Watchpack Error (initial scan)" log, and the file is reported once the parent becomes a directory (fixes #226's case).

‎lib/DirectoryWatcher.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,9 @@ class DirectoryWatcher extends EventEmitter {
904904
// (see #187).
905905
if (
906906
err.code === "ENOENT" ||
907+
// a file where the directory should be, e.g. `src/foo` for a
908+
// missing `src/foo/index.js` that a resolver tried (#226)
909+
err.code === "ENOTDIR" ||
907910
err.code === "EPERM" ||
908911
err.code === "EACCES" ||
909912
err.code === "ENODEV" ||

‎test/MissingUnderFile.test.js‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
const Watchpack = require("../lib");
5+
const TestHelper = require("./helpers/TestHelper");
6+
7+
const fixtures = path.join(__dirname, "fixtures");
8+
const testHelper = new TestHelper(fixtures);
9+
10+
// eslint-disable-next-line jest/no-confusing-set-timeout
11+
jest.setTimeout(20000);
12+
13+
describe("Missing file below a path that is a file (#226)", () => {
14+
beforeEach((done) => {
15+
testHelper.before(done);
16+
});
17+
18+
/** @type {InstanceType<typeof Watchpack>[]} */
19+
let watchers = [];
20+
21+
afterEach((done) => {
22+
// closed here, not in the test, so a failure cannot leave one running
23+
for (const w of watchers) w.close();
24+
watchers = [];
25+
jest.restoreAllMocks();
26+
testHelper.after(done);
27+
});
28+
29+
it("should report the file once its parent turns from a file into a directory", (done) => {
30+
// a resolver tried `foo/index.js` while `foo` is a file
31+
testHelper.file("foo");
32+
const missing = path.join(fixtures, "foo", "index.js");
33+
const errors = jest.spyOn(console, "error").mockImplementation(() => {});
34+
testHelper.tick(1000, () => {
35+
const w = new Watchpack({ aggregateTimeout: 100 });
36+
watchers.push(w);
37+
w.on("change", (item) => {
38+
expect(item).toBe(missing);
39+
expect(errors).not.toHaveBeenCalled();
40+
done();
41+
});
42+
w.watch({ missing: [missing], startTime: Date.now() });
43+
testHelper.tick(1000, () => {
44+
testHelper.remove("foo");
45+
testHelper.dir("foo");
46+
testHelper.file(path.join("foo", "index.js"));
47+
});
48+
});
49+
});
50+
});

0 commit comments

Comments
 (0)