Skip to content

Commit 0418015

Browse files
Merge pull request #358 from webpack/fix/case-insensitive-time-info
fix: give a path watched with other casing the time info of its file on case-insensitive file systems
2 parents 6695b9f + 1be3435 commit 0418015

5 files changed

Lines changed: 150 additions & 9 deletions

File tree

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+
On case-insensitive file systems, give a file watched with other casing (e.g. imported as `./B` while the file is `b.js`) the time info of the file instead of `null`, so webpack rebuilds it on every change, not only the first (fixes #228).

‎lib/DirectoryWatcher.js‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ class DirectoryWatcher extends EventEmitter {
233233
this.files = new Map();
234234
/** @type {Map<string, number>} */
235235
this.filesWithoutCase = new Map();
236+
// whether names in this directory ignore case, learned the first time a
237+
// watched path differs from a file on disk only by case
238+
/** @type {boolean | undefined} */
239+
this.caseInsensitive = undefined;
236240
/** @type {Map<string, Watcher<DirectoryWatcherEvents> | boolean>} */
237241
this.directories = new Map();
238242
/** @type {Map<string, Watcher<FileWatcherEvents>>} */
@@ -1155,14 +1159,63 @@ class DirectoryWatcher extends EventEmitter {
11551159
for (const watcher of watchers) {
11561160
const { path } = watcher;
11571161
if (!fileTimestamps.has(path)) {
1158-
fileTimestamps.set(path, null);
1162+
// on a case-insensitive file system a path with other casing
1163+
// is the same file and must change with it
1164+
const file = this.getFileWithOtherCase(path);
1165+
fileTimestamps.set(
1166+
path,
1167+
(file !== undefined && this.files.get(file)) || null,
1168+
);
11591169
}
11601170
}
11611171
}
11621172
}
11631173
return safeTime;
11641174
}
11651175

1176+
/**
1177+
* @param {string} target a watched path that is not in `files`
1178+
* @returns {string | undefined} the file it names on a case-insensitive file system
1179+
*/
1180+
getFileWithOtherCase(target) {
1181+
if (this.caseInsensitive === false) return;
1182+
const key = withoutCase(target);
1183+
if (!this.filesWithoutCase.has(key)) return;
1184+
let file;
1185+
for (const item of this.files.keys()) {
1186+
if (withoutCase(item) === key) {
1187+
file = item;
1188+
break;
1189+
}
1190+
}
1191+
if (file === undefined) return;
1192+
if (this.caseInsensitive === undefined) {
1193+
let fileStats;
1194+
try {
1195+
fileStats = fs.lstatSync(file);
1196+
} catch (_err) {
1197+
// changed meanwhile, the next scan updates `files`
1198+
return;
1199+
}
1200+
try {
1201+
const targetStats = fs.lstatSync(target);
1202+
this.caseInsensitive =
1203+
targetStats.ino === fileStats.ino &&
1204+
targetStats.dev === fileStats.dev;
1205+
} catch (err) {
1206+
// only a missing name proves case matters; EBUSY and the like
1207+
// leave it to the next collection
1208+
if (/** @type {NodeJS.ErrnoException} */ (err).code !== "ENOENT") {
1209+
return;
1210+
}
1211+
// a file removed meanwhile says nothing about the file system
1212+
if (!fs.existsSync(file)) return;
1213+
this.caseInsensitive = false;
1214+
}
1215+
}
1216+
return this.caseInsensitive ? file : undefined;
1217+
}
1218+
11661219
close() {
11671220
if (this.closed) return;
11681221
this.closed = true;

‎test/Casing.test.js‎

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,10 @@ if (fsIsCaseInsensitive) {
101101
expect(changes).toContain(path.join(fixtures, testFileRename));
102102
expect(removals).not.toContain(path.join(fixtures, testFileRename));
103103

104-
for (const file of files.keys()) {
105-
if (file.endsWith("hello.txt") && files.get(file)) {
106-
return done(new Error("Renamed file was still in timeInfoEntries"));
107-
}
108-
}
104+
// the old casing still names the renamed file
105+
expect(files.get(path.join(fixtures, testFile))).toBe(
106+
files.get(path.join(fixtures, testFileRename)),
107+
);
109108
return done();
110109
});
111110

@@ -120,11 +119,57 @@ if (fsIsCaseInsensitive) {
120119
});
121120
});
122121
});
122+
123+
it("should give a file watched with the wrong casing the time info of the file (#228)", (done) => {
124+
testHelper.file("a");
125+
testHelper.tick(1000, () => {
126+
const w = new Watchpack({
127+
aggregateTimeout: 100,
128+
});
129+
const wrongCase = path.join(fixtures, "A");
130+
w.watch({ files: [wrongCase] });
131+
testHelper.tick(500, () => {
132+
const before = w.getTimeInfoEntries().get(wrongCase);
133+
expect(before).toBeTruthy();
134+
w.on("aggregated", () => {
135+
const files = w.getTimeInfoEntries();
136+
w.close();
137+
const after = files.get(wrongCase);
138+
expect(after).toBe(files.get(path.join(fixtures, "a")));
139+
expect(after).not.toEqual(before);
140+
done();
141+
});
142+
testHelper.file("a");
143+
});
144+
});
145+
});
123146
});
124147
} else {
125-
describe("casing (no tests)", () => {
126-
it("pass", () => {
127-
expect(true).toBe(true);
148+
describe("casing (case-sensitive file system)", () => {
149+
beforeEach((done) => {
150+
testHelper.before(done);
151+
});
152+
153+
afterEach((done) => {
154+
testHelper.after(done);
155+
});
156+
157+
it("should report a path with other casing as missing on a case-sensitive file system", (done) => {
158+
testHelper.file("a");
159+
testHelper.tick(() => {
160+
const w = new Watchpack({
161+
aggregateTimeout: 100,
162+
});
163+
const wrongCase = path.join(fixtures, "A");
164+
w.watch({ files: [wrongCase] });
165+
testHelper.tick(500, () => {
166+
const files = w.getTimeInfoEntries();
167+
w.close();
168+
expect(files.get(wrongCase)).toBeNull();
169+
expect(files.get(path.join(fixtures, "a"))).toBeTruthy();
170+
done();
171+
});
172+
});
128173
});
129174
});
130175
}

‎test/DirectoryWatcherExtra.test.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,37 @@ describe("DirectoryWatcher extra coverage", () => {
479479
dw.close();
480480
});
481481

482+
it("getFileWithOtherCase does not decide case sensitivity on a transient lstat error", () => {
483+
testHelper.file("a");
484+
const dw = new DirectoryWatcher(
485+
getWatcherManager(EMPTY_OPTIONS),
486+
fixtures,
487+
EMPTY_OPTIONS,
488+
);
489+
const file = path.join(fixtures, "a");
490+
const otherCase = path.join(fixtures, "A");
491+
dw.setFileTime(file, Date.now(), true, false, "scan (file)");
492+
493+
const fs = require("graceful-fs");
494+
495+
const { lstatSync } = fs;
496+
const spy = jest
497+
.spyOn(fs, "lstatSync")
498+
.mockImplementation((target, ...args) => {
499+
if (target === otherCase) {
500+
throw Object.assign(new Error("EBUSY"), { code: "EBUSY" });
501+
}
502+
return lstatSync(target, ...args);
503+
});
504+
try {
505+
expect(dw.getFileWithOtherCase(otherCase)).toBeUndefined();
506+
expect(dw.caseInsensitive).toBeUndefined();
507+
} finally {
508+
spy.mockRestore();
509+
dw.close();
510+
}
511+
});
512+
482513
it("setFileTime detects a case-insensitive duplicate and triggers a rescan", () => {
483514
const dw = new DirectoryWatcher(
484515
getWatcherManager(EMPTY_OPTIONS),

‎types/DirectoryWatcher.d.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ declare class DirectoryWatcher extends EventEmitter<{
4646
files: Map<string, Entry>;
4747
/** @type {Map<string, number>} */
4848
filesWithoutCase: Map<string, number>;
49+
/** @type {boolean | undefined} */
50+
caseInsensitive: boolean | undefined;
4951
/** @type {Map<string, Watcher<DirectoryWatcherEvents> | boolean>} */
5052
directories: Map<string, Watcher<DirectoryWatcherEvents> | boolean>;
5153
/** @type {Map<string, Watcher<FileWatcherEvents>>} */
@@ -186,6 +188,11 @@ declare class DirectoryWatcher extends EventEmitter<{
186188
fileTimestamps: TimeInfoEntries,
187189
directoryTimestamps: TimeInfoEntries,
188190
): number;
191+
/**
192+
* @param {string} target a watched path that is not in `files`
193+
* @returns {string | undefined} the file it names on a case-insensitive file system
194+
*/
195+
getFileWithOtherCase(target: string): string | undefined;
189196
close(): void;
190197
}
191198
declare namespace DirectoryWatcher {

0 commit comments

Comments
 (0)