Skip to content

Commit

Permalink
prevent reading directories as files when building fs cache webpack#1…
Browse files Browse the repository at this point in the history
…6417

Looks like a nodejs bug, introduced in v23.0.0 - commit hash 00b2f07f
It should be fixed in v23.2.0 - dd9b6833

bug: nodejs/node#54160
fix: nodejs/node#55527

I am able to replicate this on v23.1.0.
  • Loading branch information
jeremybobbin committed Dec 5, 2024
1 parent 644f1d1 commit d20ff8c
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions lib/FileSystemInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3265,34 +3265,44 @@ class FileSystemInfo {
* @private
*/
_readFileHash(path, callback) {
this.fs.readFile(path, (err, content) => {
this.fs.stat(path, (err, _stat) => {
if (err) {
if (err.code === "EISDIR") {
this._fileHashes.set(path, "directory");
return callback(null, "directory");
}
if (err.code === "ENOENT") {
this._fileHashes.set(path, null);
return callback(null, null);
}
if (err.code === "ERR_FS_FILE_TOO_LARGE") {
/** @type {Logger} */
(this.logger).warn(`Ignoring ${path} for hashing as it's very large`);
this._fileHashes.set(path, "too large");
return callback(null, "too large");
}
return callback(/** @type {WebpackError} */ (err));
}

const hash = createHash(this._hashFunction);
const stat = /** @type {IStats} */ (_stat);
if (stat.isDirectory()) {
this._fileHashes.set(path, "directory");
return callback(null, "directory");
}

this.fs.readFile(path, (err, content) => {
if (err) {
if (err.code === "ERR_FS_FILE_TOO_LARGE") {
/** @type {Logger} */
(this.logger).warn(
`Ignoring ${path} for hashing as it's very large`
);
this._fileHashes.set(path, "too large");
return callback(null, "too large");
}
return callback(/** @type {WebpackError} */ (err));
}

const hash = createHash(this._hashFunction);

hash.update(/** @type {string | Buffer} */ (content));
hash.update(/** @type {string | Buffer} */ (content));

const digest = /** @type {string} */ (hash.digest("hex"));
const digest = /** @type {string} */ (hash.digest("hex"));

this._fileHashes.set(path, digest);
this._fileHashes.set(path, digest);

callback(null, digest);
callback(null, digest);
});
});
}

Expand Down

0 comments on commit d20ff8c

Please sign in to comment.