Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules: fix repl require calling the same file again #26928

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ const {
const isWindows = process.platform === 'win32';

let requireDepth = 0;
let statCache = new Map();
let statCache = null;
function stat(filename) {
filename = path.toNamespacedPath(filename);
if (statCache === null) statCache = new Map();
let result = statCache.get(filename);
if (result !== undefined) return result;
result = internalModuleStat(filename);
statCache.set(filename, result);
if (statCache !== null) {
const result = statCache.get(filename);
if (result !== undefined) return result;
}
const result = internalModuleStat(filename);
if (statCache !== null) statCache.set(filename, result);
return result;
}

Expand Down Expand Up @@ -196,7 +197,7 @@ Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077');
// -> a.<ext>
// -> a/index.<ext>

// check if the directory is a package.json dir
// Check if the directory is a package.json dir.
const packageMainCache = Object.create(null);

function readPackage(requestPath) {
Expand Down Expand Up @@ -830,6 +831,7 @@ Module.prototype._compile = function(content, filename) {
const exports = this.exports;
const thisValue = exports;
const module = this;
if (requireDepth === 0) statCache = new Map();
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
if (inspectorWrapper) {
result = inspectorWrapper(compiledWrapper, thisValue, exports,
require, module, filename, dirname);
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-repl-require-after-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const common = require('../common');
const assert = require('assert');

const spawn = require('child_process').spawn;
// Use -i to force node into interactive mode, despite stdout not being a TTY
const child = spawn(process.execPath, ['-i']);

let out = '';
const input = "try { require('./non-existent.json'); } catch {} " +
"require('fs').writeFileSync('./non-existent.json', '1');" +
"require('./non-existent.json');";

child.stderr.on('data', common.mustNotCall());

child.stdout.setEncoding('utf8');
child.stdout.on('data', (c) => {
out += c;
});
child.stdout.on('end', common.mustCall(() => {
assert.strictEqual(out, '> 1\n> ');
}));

child.stdin.end(input);