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: Revert "preserve symlinks when requiring"
After much discussion, it seems that the change
broke the way require detects unique identity of
the binary modules. While resolving module's
full path is not sufficient and maybe even
not elegant (like anything that requires the use
of realpath() except for certain security scenarios),
we cannot break multiple inclusions of binary
modules across symlinked, substed or otherwise
hidden paths.

There should be a test case invoving a binary
module to check for issues related to the binary
module identity.

This reverts commit de1dc0a.
  • Loading branch information
saper committed May 2, 2016
commit 7ca9bc57f2d1a930c34b407847bca17a1c370b24
43 changes: 17 additions & 26 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,32 +97,27 @@ function readPackage(requestPath) {
return pkg;
}

function tryPackage(requestPath, exts, isMain) {
function tryPackage(requestPath, exts) {
var pkg = readPackage(requestPath);

if (!pkg) return false;

var filename = path.resolve(requestPath, pkg);
return tryFile(filename, isMain) ||
tryExtensions(filename, exts, isMain) ||
tryExtensions(path.resolve(filename, 'index'), exts, isMain);
return tryFile(filename) ||
tryExtensions(filename, exts) ||
tryExtensions(path.resolve(filename, 'index'), exts);
}

// check if the file exists and is not a directory
// resolve to the absolute realpath if running main module,
// otherwise resolve to absolute while keeping symlinks intact.
function tryFile(requestPath, isMain) {
function tryFile(requestPath) {
const rc = stat(requestPath);
if (isMain) {
return rc === 0 && fs.realpathSync(requestPath);
}
return rc === 0 && path.resolve(requestPath);
return rc === 0 && fs.realpathSync(requestPath);
}

// given a path check a the file exists with any of the set extensions
function tryExtensions(p, exts, isMain) {
function tryExtensions(p, exts) {
for (var i = 0; i < exts.length; i++) {
const filename = tryFile(p + exts[i], isMain);
const filename = tryFile(p + exts[i]);

if (filename) {
return filename;
Expand All @@ -132,7 +127,7 @@ function tryExtensions(p, exts, isMain) {
}

var warned = false;
Module._findPath = function(request, paths, isMain) {
Module._findPath = function(request, paths) {
if (path.isAbsolute(request)) {
paths = [''];
} else if (!paths || paths.length === 0) {
Expand All @@ -159,36 +154,32 @@ Module._findPath = function(request, paths, isMain) {
if (!trailingSlash) {
const rc = stat(basePath);
if (rc === 0) { // File.
if (!isMain) {
filename = path.resolve(basePath);
} else {
filename = fs.realpathSync(basePath);
}
filename = fs.realpathSync(basePath);
} else if (rc === 1) { // Directory.
if (exts === undefined)
exts = Object.keys(Module._extensions);
filename = tryPackage(basePath, exts, isMain);
filename = tryPackage(basePath, exts);
}

if (!filename) {
// try it with each of the extensions
if (exts === undefined)
exts = Object.keys(Module._extensions);
filename = tryExtensions(basePath, exts, isMain);
filename = tryExtensions(basePath, exts);
}
}

if (!filename) {
if (exts === undefined)
exts = Object.keys(Module._extensions);
filename = tryPackage(basePath, exts, isMain);
filename = tryPackage(basePath, exts);
}

if (!filename) {
// try it with each of the extensions at "index"
if (exts === undefined)
exts = Object.keys(Module._extensions);
filename = tryExtensions(path.resolve(basePath, 'index'), exts, isMain);
filename = tryExtensions(path.resolve(basePath, 'index'), exts);
}

if (filename) {
Expand Down Expand Up @@ -383,7 +374,7 @@ Module._load = function(request, parent, isMain) {
debug('Module._load REQUEST %s parent: %s', request, parent.id);
}

var filename = Module._resolveFilename(request, parent, isMain);
var filename = Module._resolveFilename(request, parent);

var cachedModule = Module._cache[filename];
if (cachedModule) {
Expand Down Expand Up @@ -421,7 +412,7 @@ function tryModuleLoad(module, filename) {
}
}

Module._resolveFilename = function(request, parent, isMain) {
Module._resolveFilename = function(request, parent) {
if (NativeModule.nonInternalExists(request)) {
return request;
}
Expand All @@ -433,7 +424,7 @@ Module._resolveFilename = function(request, parent, isMain) {
// 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);
var filename = Module._findPath(request, paths);
if (!filename) {
var err = new Error("Cannot find module '" + request + "'");
err.code = 'MODULE_NOT_FOUND';
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/module-require-symlink/foo.js

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

13 changes: 0 additions & 13 deletions test/fixtures/module-require-symlink/symlinked.js

This file was deleted.

57 changes: 0 additions & 57 deletions test/parallel/test-require-symlink.js

This file was deleted.