module: only set cache when finding module succeeds · nodejs/node@aecd5eb · GitHub
Skip to content

Commit aecd5eb

Browse files
author
zhangyongsheng
committed
module: only set cache when finding module succeeds
PR-URL: #36642 Fixes: #36638 Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f2c2615 commit aecd5eb

3 files changed

Lines changed: 103 additions & 1 deletion

File tree

benchmark/module/module-require.js

Lines changed: 80 additions & 0 deletions

lib/internal/modules/cjs/loader.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ function stat(filename) {
148148
if (result !== undefined) return result;
149149
}
150150
const result = internalModuleStat(filename);
151-
if (statCache !== null) statCache.set(filename, result);
151+
if (statCache !== null && result >= 0) {
152+
// Only set cache when `internalModuleStat(filename)` succeeds.
153+
statCache.set(filename, result);
154+
}
152155
return result;
153156
}
154157

test/parallel/test-module-cache.js

Lines changed: 19 additions & 0 deletions

0 commit comments

Comments
 (0)