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

lib: include cached modules in module.children #14132

Merged
merged 1 commit into from
Jul 24, 2017
Merged
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
12 changes: 8 additions & 4 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ function stat(filename) {
}
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) {
this.id = id;
this.exports = {};
this.parent = parent;
if (parent && parent.children) {
parent.children.push(this);
}

updateChildren(parent, this, false);
this.filename = null;
this.loaded = false;
this.children = [];
Expand Down Expand Up @@ -438,6 +440,7 @@ Module._load = function(request, parent, isMain) {

var cachedModule = Module._cache[filename];
if (cachedModule) {
updateChildren(parent, cachedModule, true);
return cachedModule.exports;
}

Expand All @@ -446,6 +449,7 @@ Module._load = function(request, parent, isMain) {
return NativeModule.require(filename);
}

// Don't call updateChildren(), Module constructor already does.
var module = new Module(filename, parent);

if (isMain) {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/GH-7131/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';
require('sys'); // Builtin should not show up in module.children array.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I picked a module that's unlikely to have already been included by test/common.js.

require('./b'); // This should.
require('./b'); // This should not.
module.exports = module.children.slice();
2 changes: 2 additions & 0 deletions test/fixtures/GH-7131/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
module.exports = module.children.slice();
13 changes: 13 additions & 0 deletions test/parallel/test-module-children.js
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 3 additions & 0 deletions test/sequential/test-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ try {
// modules that we've required, and that all of them contain
// the appropriate children, and so on.

const visited = new Set();
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);
id = id.replace(backslash, '/');
set[id] = child.children.reduce(red, {});
Expand Down