lib: include cached modules in module.children
`module.children` is supposed to be the list of modules included by this module but lib/module.js failed to update the list when the included module was retrieved from `Module._cache`. Fixes: https://github.com/nodejs/node/issues/7131 PR-URL: https://github.com/nodejs/node/pull/14132 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
parent
355523fcfb
commit
c83d9bbffb
@ -46,15 +46,17 @@ function stat(filename) {
|
|||||||
}
|
}
|
||||||
stat.cache = null;
|
stat.cache = null;
|
||||||
|
|
||||||
|
function updateChildren(parent, child, scan) {
|
||||||
|
var children = parent && parent.children;
|
||||||
|
if (children && !(scan && children.includes(child)))
|
||||||
|
children.push(child);
|
||||||
|
}
|
||||||
|
|
||||||
function Module(id, parent) {
|
function Module(id, parent) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.exports = {};
|
this.exports = {};
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
if (parent && parent.children) {
|
updateChildren(parent, this, false);
|
||||||
parent.children.push(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.filename = null;
|
this.filename = null;
|
||||||
this.loaded = false;
|
this.loaded = false;
|
||||||
this.children = [];
|
this.children = [];
|
||||||
@ -438,6 +440,7 @@ Module._load = function(request, parent, isMain) {
|
|||||||
|
|
||||||
var cachedModule = Module._cache[filename];
|
var cachedModule = Module._cache[filename];
|
||||||
if (cachedModule) {
|
if (cachedModule) {
|
||||||
|
updateChildren(parent, cachedModule, true);
|
||||||
return cachedModule.exports;
|
return cachedModule.exports;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -446,6 +449,7 @@ Module._load = function(request, parent, isMain) {
|
|||||||
return NativeModule.require(filename);
|
return NativeModule.require(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't call updateChildren(), Module constructor already does.
|
||||||
var module = new Module(filename, parent);
|
var module = new Module(filename, parent);
|
||||||
|
|
||||||
if (isMain) {
|
if (isMain) {
|
||||||
|
5
test/fixtures/GH-7131/a.js
vendored
Normal file
5
test/fixtures/GH-7131/a.js
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
'use strict';
|
||||||
|
require('sys'); // Builtin should not show up in module.children array.
|
||||||
|
require('./b'); // This should.
|
||||||
|
require('./b'); // This should not.
|
||||||
|
module.exports = module.children.slice();
|
2
test/fixtures/GH-7131/b.js
vendored
Normal file
2
test/fixtures/GH-7131/b.js
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
'use strict';
|
||||||
|
module.exports = module.children.slice();
|
13
test/parallel/test-module-children.js
Normal file
13
test/parallel/test-module-children.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// Flags: --no-deprecation
|
||||||
|
'use strict';
|
||||||
|
const common = require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const dir = path.join(common.fixturesDir, 'GH-7131');
|
||||||
|
const b = require(path.join(dir, 'b'));
|
||||||
|
const a = require(path.join(dir, 'a'));
|
||||||
|
|
||||||
|
assert.strictEqual(a.length, 1);
|
||||||
|
assert.strictEqual(b.length, 0);
|
||||||
|
assert.deepStrictEqual(a[0].exports, b);
|
@ -237,7 +237,10 @@ try {
|
|||||||
// modules that we've required, and that all of them contain
|
// modules that we've required, and that all of them contain
|
||||||
// the appropriate children, and so on.
|
// the appropriate children, and so on.
|
||||||
|
|
||||||
|
const visited = new Set();
|
||||||
const children = module.children.reduce(function red(set, child) {
|
const children = module.children.reduce(function red(set, child) {
|
||||||
|
if (visited.has(child)) return set;
|
||||||
|
visited.add(child);
|
||||||
let id = path.relative(path.dirname(__dirname), child.id);
|
let id = path.relative(path.dirname(__dirname), child.id);
|
||||||
id = id.replace(backslash, '/');
|
id = id.replace(backslash, '/');
|
||||||
set[id] = child.children.reduce(red, {});
|
set[id] = child.children.reduce(red, {});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user