Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 079368a

Browse files
BridgeARBethGriggs
authored andcommittedApr 9, 2019
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 <guybedford@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com>
1 parent dbd0608 commit 079368a

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed
 

‎lib/internal/modules/cjs/loader.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,15 @@ const {
7878
const isWindows = process.platform === 'win32';
7979

8080
let requireDepth = 0;
81-
let statCache = new Map();
81+
let statCache = null;
8282
function stat(filename) {
8383
filename = path.toNamespacedPath(filename);
84-
if (statCache === null) statCache = new Map();
85-
let result = statCache.get(filename);
86-
if (result !== undefined) return result;
87-
result = internalModuleStat(filename);
88-
statCache.set(filename, result);
84+
if (statCache !== null) {
85+
const result = statCache.get(filename);
86+
if (result !== undefined) return result;
87+
}
88+
const result = internalModuleStat(filename);
89+
if (statCache !== null) statCache.set(filename, result);
8990
return result;
9091
}
9192

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

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

194195
function readPackage(requestPath) {
@@ -788,6 +789,7 @@ Module.prototype._compile = function(content, filename) {
788789
const exports = this.exports;
789790
const thisValue = exports;
790791
const module = this;
792+
if (requireDepth === 0) statCache = new Map();
791793
if (inspectorWrapper) {
792794
result = inspectorWrapper(compiledWrapper, thisValue, exports,
793795
require, module, filename, dirname);
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)
Please sign in to comment.