mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
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: https://github.com/nodejs/node/pull/26928 Fixes: https://github.com/nodejs/node/issues/26926 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com>
This commit is contained in:
parent
1087805eeb
commit
8a6dcd040f
@ -78,14 +78,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;
|
||||
}
|
||||
|
||||
@ -188,7 +189,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) {
|
||||
@ -794,6 +795,7 @@ Module.prototype._compile = function(content, filename) {
|
||||
const exports = this.exports;
|
||||
const thisValue = exports;
|
||||
const module = this;
|
||||
if (requireDepth === 0) statCache = new Map();
|
||||
if (inspectorWrapper) {
|
||||
result = inspectorWrapper(compiledWrapper, thisValue, exports,
|
||||
require, module, filename, dirname);
|
||||
|
25
test/parallel/test-repl-require-after-write.js
Normal file
25
test/parallel/test-repl-require-after-write.js
Normal file
@ -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);
|
Loading…
Reference in New Issue
Block a user