Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
module: accept Windows relative path
Before this change, require('.\\test.js') failed while
require('./test.js') succeeded. This changes _resolveLookupPaths so
that both ways are accepted on Windows.

Fixes: #21918
  • Loading branch information
joaocgreis committed Sep 1, 2018
commit cf0e7c2316a229dbe30254d1c3bf3a23bda69492
15 changes: 9 additions & 6 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const {
CHAR_9,
} = require('internal/constants');

const isWindows = process.platform === 'win32';

function stat(filename) {
filename = path.toNamespacedPath(filename);
const cache = stat.cache;
Expand Down Expand Up @@ -310,7 +312,7 @@ Module._findPath = function(request, paths, isMain) {
// 'node_modules' character codes reversed
var nmChars = [ 115, 101, 108, 117, 100, 111, 109, 95, 101, 100, 111, 110 ];
var nmLen = nmChars.length;
if (process.platform === 'win32') {
if (isWindows) {
// 'from' is the __dirname of the module.
Module._nodeModulePaths = function(from) {
// guarantee that 'from' is absolute.
Expand Down Expand Up @@ -403,11 +405,12 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
return (newReturn ? null : [request, []]);
}

// Check for relative path
// Check for non-relative path
if (request.length < 2 ||
request.charCodeAt(0) !== CHAR_DOT ||
(request.charCodeAt(1) !== CHAR_DOT &&
request.charCodeAt(1) !== CHAR_FORWARD_SLASH)) {
request.charCodeAt(1) !== CHAR_FORWARD_SLASH &&
(!isWindows || request.charCodeAt(1) !== CHAR_BACKWARD_SLASH))) {
var paths = modulePaths;
if (parent) {
if (!parent.paths)
Expand Down Expand Up @@ -480,7 +483,9 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {

// make sure require('./path') and require('path') get distinct ids, even
// when called from the toplevel js file
if (parentIdPath === '.' && id.indexOf('/') === -1) {
if (parentIdPath === '.' &&
id.indexOf('/') === -1 &&
(!isWindows || id.indexOf('\\') === -1)) {
id = './' + id;
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This change looked necessary, but this piece of code does not seem to be used anywhere in node core nor tested. newReturn seems to be passed as true everywhere. Hence, I'm not sure about this, let me know if I should leave this as it was.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

newReturn is for backwards compatibility. There were some people using the private, underscored methods directly and were expecting the old return value.


Expand Down Expand Up @@ -746,8 +751,6 @@ Module.runMain = function() {
};

Module._initPaths = function() {
const isWindows = process.platform === 'win32';

var homeDir;
var nodePath;
if (isWindows) {
Expand Down
24 changes: 16 additions & 8 deletions test/parallel/test-module-relative-lookup.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');
const _module = require('module'); // avoid collision with global.module
const lookupResults = _module._resolveLookupPaths('./lodash');
let paths = lookupResults[1];

// Current directory gets highest priority for local modules
assert.strictEqual(paths[0], '.');
function runTest(moduleName) {
const lookupResults = _module._resolveLookupPaths(moduleName);
let paths = lookupResults[1];

paths = _module._resolveLookupPaths('./lodash', null, true);
// Current directory gets highest priority for local modules
assert.strictEqual(paths[0], '.');

// Current directory gets highest priority for local modules
assert.strictEqual(paths && paths[0], '.');
paths = _module._resolveLookupPaths(moduleName, null, true);

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

runTest('./lodash');
if (common.isWindows) {
runTest('.\\lodash');
}
Copy link
Copy Markdown
Member

@Trott Trott Aug 9, 2018

Choose a reason for hiding this comment

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

Should there be an else clause added that asserts that runTest('.\\lodash') on non-Windows throws or whatever it is supposed to do?