Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/missing-under-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"watchpack": patch
---

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).
3 changes: 3 additions & 0 deletions lib/DirectoryWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,9 @@ class DirectoryWatcher extends EventEmitter {
// (see #187).
if (
err.code === "ENOENT" ||
// a file where the directory should be, e.g. `src/foo` for a
// missing `src/foo/index.js` that a resolver tried (#226)
err.code === "ENOTDIR" ||
err.code === "EPERM" ||
err.code === "EACCES" ||
err.code === "ENODEV" ||
Expand Down
50 changes: 50 additions & 0 deletions test/MissingUnderFile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict";

const path = require("path");
const Watchpack = require("../lib");
const TestHelper = require("./helpers/TestHelper");

const fixtures = path.join(__dirname, "fixtures");
const testHelper = new TestHelper(fixtures);

// eslint-disable-next-line jest/no-confusing-set-timeout
jest.setTimeout(20000);

describe("Missing file below a path that is a file (#226)", () => {
beforeEach((done) => {
testHelper.before(done);
});

/** @type {InstanceType<typeof Watchpack>[]} */
let watchers = [];

afterEach((done) => {
// closed here, not in the test, so a failure cannot leave one running
for (const w of watchers) w.close();
watchers = [];
jest.restoreAllMocks();
testHelper.after(done);
});

it("should report the file once its parent turns from a file into a directory", (done) => {
// a resolver tried `foo/index.js` while `foo` is a file
testHelper.file("foo");
const missing = path.join(fixtures, "foo", "index.js");
const errors = jest.spyOn(console, "error").mockImplementation(() => {});
testHelper.tick(1000, () => {
const w = new Watchpack({ aggregateTimeout: 100 });
watchers.push(w);
w.on("change", (item) => {
expect(item).toBe(missing);
expect(errors).not.toHaveBeenCalled();
done();
});
w.watch({ missing: [missing], startTime: Date.now() });
testHelper.tick(1000, () => {
testHelper.remove("foo");
testHelper.dir("foo");
testHelper.file(path.join("foo", "index.js"));
});
});
});
});
Loading