Skip to content

Commit

Permalink
[Fix] resolution when filename option is passed
Browse files Browse the repository at this point in the history
PR #162 introduced a `parent` variable that contains either the full
filename of the requiring module or the `basedir`. Some of the places
where `basedir` was used were updated to use `parent` instead, but
that's not correct; the `path.resolve()` calls would receive the full
filename instead of the directory name.

This patch reverts the improper `parent` uses to `basedir`.
  • Loading branch information
goto-bus-stop committed Jun 17, 2018
1 parent 698a3e1 commit 29495fb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ module.exports = function resolve(x, options, callback) {
var res;
function validBasedir() {
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
res = path.resolve(parent, x);
res = path.resolve(basedir, x);
if (x === '..' || x.slice(-1) === '/') res += '/';
if (/\/$/.test(x) && res === parent) {
if (/\/$/.test(x) && res === basedir) {
loadAsDirectory(res, opts.package, onfile);
} else loadAsFile(res, opts.package, onfile);
} else loadNodeModules(x, parent, function (err, n, pkg) {
} 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);
Expand Down
4 changes: 2 additions & 2 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ module.exports = function (x, options) {
}

if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
var res = path.resolve(parent, x);
var res = path.resolve(basedir, x);
if (x === '..' || x.slice(-1) === '/') res += '/';
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) return m;
} else if (core[x]) {
return x;
} else {
var n = loadNodeModulesSync(x, parent);
var n = loadNodeModulesSync(x, basedir);
if (n) return n;
}

Expand Down
7 changes: 6 additions & 1 deletion test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var test = require('tape');
var resolve = require('../');

test('async foo', function (t) {
t.plan(11);
t.plan(12);
var dir = path.join(__dirname, 'resolver');

resolve('./foo', { basedir: dir }, function (err, res, pkg) {
Expand All @@ -30,6 +30,11 @@ test('async foo', function (t) {
t.equal(pkg.main, 'resolver');
});

resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo.js'));
});

resolve('foo', { basedir: dir }, function (err) {
t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
Expand Down
5 changes: 5 additions & 0 deletions test/resolver_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ test('foo', function (t) {
path.join(dir, 'foo.js')
);

t.equal(
resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
path.join(dir, 'foo.js')
);

t.throws(function () {
resolve.sync('foo', { basedir: dir });
});
Expand Down

0 comments on commit 29495fb

Please sign in to comment.