Skip to content

Commit

Permalink
[Fix] preserveSymlinks: false ensure that files are realpathed
Browse files Browse the repository at this point in the history
Fixes #196.
  • Loading branch information
ljharb committed Jul 30, 2019
1 parent 230ff85 commit 74849a1
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 7 deletions.
10 changes: 9 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,13 @@
"object-curly-newline": 0,
"operator-linebreak": [2, "before"],
"sort-keys": 0,
}
},
"overrides": [
{
"files": "test/resolver/nested_symlinks/mylib/*.js",
"rules": {
"no-throw-literal": 0,
},
},
],
}
22 changes: 18 additions & 4 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,15 @@ module.exports = function resolve(x, options, callback) {
} else loadNodeModules(x, basedir, function (err, n, pkg) {
if (err) cb(err);
else if (core[x]) return cb(null, x);
else if (n) return cb(null, n, pkg);
else {
else if (n) {
return maybeUnwrapSymlink(n, opts, function (err, realN) {
if (err) {
cb(err);
} else {
cb(null, realN, pkg);
}
});
} else {
var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'");
moduleError.code = 'MODULE_NOT_FOUND';
cb(moduleError);
Expand All @@ -113,8 +120,15 @@ module.exports = function resolve(x, options, callback) {
else if (m) cb(null, m, pkg);
else loadAsDirectory(res, function (err, d, pkg) {
if (err) cb(err);
else if (d) cb(null, d, pkg);
else {
else if (d) {
maybeUnwrapSymlink(d, opts, function (err, realD) {
if (err) {
cb(err);
} else {
cb(null, realD, pkg);
}
});
} else {
var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'");
moduleError.code = 'MODULE_NOT_FOUND';
cb(moduleError);
Expand Down
4 changes: 2 additions & 2 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ module.exports = function (x, options) {
var res = path.resolve(absoluteStart, x);
if (x === '..' || x.slice(-1) === '/') res += '/';
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) return m;
if (m) return maybeUnwrapSymlink(m, opts);
} else if (core[x]) {
return x;
} else {
var n = loadNodeModulesSync(x, absoluteStart);
if (n) return n;
if (n) return maybeUnwrapSymlink(n, opts);
}

if (core[x]) return x;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"scripts": {
"prepublish": "safe-publish-latest",
"lint": "eslint .",
"pretests-only": "cd ./test/resolver/nested_symlinks && node mylib/sync && node mylib/async",
"tests-only": "tape test/*.js",
"pretest": "npm run lint",
"test": "npm run --silent tests-only",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions test/resolver/nested_symlinks/mylib/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var a = require.resolve('buffer/').replace(process.cwd(), '$CWD');
var b;
var c;

var test = function test() {
console.log(a, ': require.resolve, preserveSymlinks ' + (process.execArgv.indexOf('preserve-symlinks') > -1 ? 'true' : 'false'));
console.log(b, ': preserveSymlinks true');
console.log(c, ': preserveSymlinks false');

if (a !== b && a !== c) {
throw 'async: no match';
}
console.log('async: success! a matched either b or c\n');
};

require('resolve')('buffer/', { preserveSymlinks: true }, function (err, result) {
if (err) { throw err; }
b = result.replace(process.cwd(), '$CWD');
if (b && c) { test(); }
});
require('resolve')('buffer/', { preserveSymlinks: false }, function (err, result) {
if (err) { throw err; }
c = result.replace(process.cwd(), '$CWD');
if (b && c) { test(); }
});

1 change: 1 addition & 0 deletions test/resolver/nested_symlinks/mylib/node_modules/buffer

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/resolver/nested_symlinks/mylib/node_modules/resolve

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions test/resolver/nested_symlinks/mylib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "mylib",
"version": "0.0.0",
"description": "",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"buffer": "*"
}
}
12 changes: 12 additions & 0 deletions test/resolver/nested_symlinks/mylib/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var a = require.resolve('buffer/').replace(process.cwd(), '$CWD');
var b = require('resolve').sync('buffer/', { preserveSymlinks: true }).replace(process.cwd(), '$CWD');
var c = require('resolve').sync('buffer/', { preserveSymlinks: false }).replace(process.cwd(), '$CWD');

console.log(a, ': require.resolve, preserveSymlinks ' + (process.execArgv.indexOf('preserve-symlinks') > -1 ? 'true' : 'false'));
console.log(b, ': preserveSymlinks true');
console.log(c, ': preserveSymlinks false');

if (a !== b && a !== c) {
throw 'sync: no match';
}
console.log('sync: success! a matched either b or c\n');

0 comments on commit 74849a1

Please sign in to comment.