Skip to content

Commit

Permalink
Revert "module: preserve symlinks when requiring"
Browse files Browse the repository at this point in the history
This reverts commit de1dc0a.

While this is a legitimate bug fix for a legitimate problem,
the fix itself is causing too much breakage. We need to step
back and find another way of addressing the problem.
  • Loading branch information
jasnell committed May 2, 2016
1 parent 9f8d0ea commit 8b634e8
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 103 deletions.
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.

0 comments on commit 8b634e8

Please sign in to comment.