Skip to content

Commit

Permalink
lib: simplify Module._resolveLookupPaths
Browse files Browse the repository at this point in the history
This commit consists of two changes:

* Avoids returning request/id *just* for the debug() output
* Returns `null` instead of an empty array for the list of paths

PR-URL: #10789
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
mscdex committed Mar 11, 2017
1 parent e32425b commit c672077
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
29 changes: 15 additions & 14 deletions lib/module.js
Expand Up @@ -326,14 +326,14 @@ if (process.platform === 'win32') {
// 'index.' character codes
var indexChars = [ 105, 110, 100, 101, 120, 46 ];
var indexLen = indexChars.length;
Module._resolveLookupPaths = function(request, parent) {
Module._resolveLookupPaths = function(request, parent, newReturn) {
if (NativeModule.nonInternalExists(request)) {
return [request, []];
debug('looking for %j in []', request);
return (newReturn ? null : [request, []]);
}

var reqLen = request.length;
// Check for relative path
if (reqLen < 2 ||
if (request.length < 2 ||
request.charCodeAt(0) !== 46/*.*/ ||
(request.charCodeAt(1) !== 46/*.*/ &&
request.charCodeAt(1) !== 47/*/*/)) {
Expand All @@ -355,15 +355,18 @@ Module._resolveLookupPaths = function(request, parent) {
}
}

return [request, paths];
debug('looking for %j in %j', request, paths);
return (newReturn ? (paths.length > 0 ? paths : null) : [request, paths]);
}

// with --eval, parent.id is not set and parent.filename is null
if (!parent || !parent.id || !parent.filename) {
// make require('./path/to/foo') work - normally the path is taken
// from realpath(__filename) but with eval there is no filename
var mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths);
return [request, mainPaths];

debug('looking for %j in %j', request, mainPaths);
return (newReturn ? mainPaths : [request, mainPaths]);
}

// Is the parent an index module?
Expand Down Expand Up @@ -413,7 +416,9 @@ Module._resolveLookupPaths = function(request, parent) {
debug('RELATIVE: requested: %s set ID to: %s from %s', request, id,
parent.id);

return [id, [path.dirname(parent.filename)]];
var parentDir = [path.dirname(parent.filename)];
debug('looking for %j in %j', id, parentDir);
return (newReturn ? parentDir : [id, parentDir]);
};


Expand Down Expand Up @@ -472,16 +477,12 @@ Module._resolveFilename = function(request, parent, isMain) {
return request;
}

var resolvedModule = Module._resolveLookupPaths(request, parent);
var id = resolvedModule[0];
var paths = resolvedModule[1];
var paths = Module._resolveLookupPaths(request, parent, true);

// look up the filename first, since that's the cache key.
debug('looking for %j in %j', id, paths);

var filename = Module._findPath(request, paths, isMain);
if (!filename) {
var err = new Error("Cannot find module '" + request + "'");
var err = new Error(`Cannot find module '${request}'`);
err.code = 'MODULE_NOT_FOUND';
throw err;
}
Expand Down Expand Up @@ -564,7 +565,7 @@ Module.prototype._compile = function(content, filename) {
if (!resolvedArgv) {
// we enter the repl if we're not given a filename argument.
if (process.argv[1]) {
resolvedArgv = Module._resolveFilename(process.argv[1], null);
resolvedArgv = Module._resolveFilename(process.argv[1], null, false);
} else {
resolvedArgv = 'repl';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/repl.js
Expand Up @@ -712,7 +712,7 @@ REPLServer.prototype.createContext = function() {
}

const module = new Module('<repl>');
module.paths = Module._resolveLookupPaths('<repl>', parentModule)[1];
module.paths = Module._resolveLookupPaths('<repl>', parentModule, true) || [];

const require = internalModule.makeRequireFunction(module);
context.module = module;
Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-module-relative-lookup.js
Expand Up @@ -4,7 +4,12 @@ require('../common');
const assert = require('assert');
const _module = require('module'); // avoid collision with global.module
const lookupResults = _module._resolveLookupPaths('./lodash');
const paths = lookupResults[1];
let paths = lookupResults[1];

assert.strictEqual(paths[0], '.',
'Current directory gets highest priority for local modules');

paths = _module._resolveLookupPaths('./lodash', null, true);

assert.strictEqual(paths && paths[0], '.',
'Current directory gets highest priority for local modules');

0 comments on commit c672077

Please sign in to comment.