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

module: Revert "preserve symlinks when requiring" #6721

Closed
wants to merge 2 commits into from
Closed
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
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
13 changes: 13 additions & 0 deletions test/addons/symlinked-module/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <node.h>
#include <v8.h>

void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world"));
}

void init(v8::Local<v8::Object> target) {
NODE_SET_METHOD(target, "hello", Method);
}

NODE_MODULE(binding, init);
8 changes: 8 additions & 0 deletions test/addons/symlinked-module/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
'targets': [
{
'target_name': 'binding',
'sources': [ 'binding.cc' ]
}
]
}
10 changes: 10 additions & 0 deletions test/addons/symlinked-module/submodule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
require('../../common');
const path = require('path');
const assert = require('assert');

module.exports.test = function(bindingDir) {
var mod = require(path.join(bindingDir, 'binding.node'));
assert(mod != null);
assert(mod.hello() == 'world');
};
32 changes: 32 additions & 0 deletions test/addons/symlinked-module/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
const common = require('../../common');
const fs = require('fs');
const path = require('path');
const assert = require('assert');

common.refreshTmpDir();

var destDir = path.resolve(common.tmpDir);

var location = [path.join(destDir, 'first'), path.join(destDir, 'second')];
fs.mkdirSync(location[0]);
try {
fs.symlinkSync(location[0], location[1]);
} catch (EPERM) {
common.skip('module identity test (no privs for symlinks)');
return;
}

const addonPath = path.join(__dirname, 'build', 'Release', 'binding.node');

var contents = fs.readFileSync(addonPath);
fs.writeFileSync(path.join(location[0], 'binding.node'), contents);
fs.writeFileSync(path.join(location[1], 'binding.node'), contents);

var primary = require(path.join(location[0], 'binding.node'));
assert(primary != null);
assert(primary.hello() == 'world');
var secondary = require(path.join(location[1], 'binding.node'));
assert(secondary != null);
assert(secondary.hello() == 'world');
require('./submodule').test(location[1]);
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.