Skip to content

Commit 6179a7f

Browse files
BridgeARBethGriggs
authored andcommitted
module: fix repl require calling the same file again
This makes sure multiple require calls will not fail in case a file was created after the first attempt. PR-URL: #26928 Fixes: #26926 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Jan Krems <[email protected]>
1 parent f686200 commit 6179a7f

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ const {
7979
const isWindows = process.platform === 'win32';
8080

8181
let requireDepth = 0;
82-
let statCache = new Map();
82+
let statCache = null;
8383
function stat(filename) {
8484
filename = path.toNamespacedPath(filename);
85-
if (statCache === null) statCache = new Map();
86-
let result = statCache.get(filename);
87-
if (result !== undefined) return result;
88-
result = internalModuleStat(filename);
89-
statCache.set(filename, result);
85+
if (statCache !== null) {
86+
const result = statCache.get(filename);
87+
if (result !== undefined) return result;
88+
}
89+
const result = internalModuleStat(filename);
90+
if (statCache !== null) statCache.set(filename, result);
9091
return result;
9192
}
9293

@@ -189,7 +190,7 @@ Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077');
189190
// -> a.<ext>
190191
// -> a/index.<ext>
191192

192-
// check if the directory is a package.json dir
193+
// Check if the directory is a package.json dir.
193194
const packageMainCache = Object.create(null);
194195

195196
function readPackage(requestPath) {
@@ -820,6 +821,7 @@ Module.prototype._compile = function(content, filename) {
820821
const exports = this.exports;
821822
const thisValue = exports;
822823
const module = this;
824+
if (requireDepth === 0) statCache = new Map();
823825
if (inspectorWrapper) {
824826
result = inspectorWrapper(compiledWrapper, thisValue, exports,
825827
require, module, filename, dirname);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
6+
const spawn = require('child_process').spawn;
7+
// Use -i to force node into interactive mode, despite stdout not being a TTY
8+
const child = spawn(process.execPath, ['-i']);
9+
10+
let out = '';
11+
const input = "try { require('./non-existent.json'); } catch {} " +
12+
"require('fs').writeFileSync('./non-existent.json', '1');" +
13+
"require('./non-existent.json');";
14+
15+
child.stderr.on('data', common.mustNotCall());
16+
17+
child.stdout.setEncoding('utf8');
18+
child.stdout.on('data', (c) => {
19+
out += c;
20+
});
21+
child.stdout.on('end', common.mustCall(() => {
22+
assert.strictEqual(out, '> 1\n> ');
23+
}));
24+
25+
child.stdin.end(input);

0 commit comments

Comments
 (0)