Skip to content

Commit 29495fb

Browse files
committed
[Fix] resolution when filename option is passed
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`.
1 parent 698a3e1 commit 29495fb

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

lib/async.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ module.exports = function resolve(x, options, callback) {
6161
var res;
6262
function validBasedir() {
6363
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
64-
res = path.resolve(parent, x);
64+
res = path.resolve(basedir, x);
6565
if (x === '..' || x.slice(-1) === '/') res += '/';
66-
if (/\/$/.test(x) && res === parent) {
66+
if (/\/$/.test(x) && res === basedir) {
6767
loadAsDirectory(res, opts.package, onfile);
6868
} else loadAsFile(res, opts.package, onfile);
69-
} else loadNodeModules(x, parent, function (err, n, pkg) {
69+
} else loadNodeModules(x, basedir, function (err, n, pkg) {
7070
if (err) cb(err);
7171
else if (core[x]) return cb(null, x);
7272
else if (n) return cb(null, n, pkg);

lib/sync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ module.exports = function (x, options) {
4444
}
4545

4646
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
47-
var res = path.resolve(parent, x);
47+
var res = path.resolve(basedir, x);
4848
if (x === '..' || x.slice(-1) === '/') res += '/';
4949
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
5050
if (m) return m;
5151
} else if (core[x]) {
5252
return x;
5353
} else {
54-
var n = loadNodeModulesSync(x, parent);
54+
var n = loadNodeModulesSync(x, basedir);
5555
if (n) return n;
5656
}
5757

test/resolver.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var test = require('tape');
33
var resolve = require('../');
44

55
test('async foo', function (t) {
6-
t.plan(11);
6+
t.plan(12);
77
var dir = path.join(__dirname, 'resolver');
88

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

33+
resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
34+
if (err) t.fail(err);
35+
t.equal(res, path.join(dir, 'foo.js'));
36+
});
37+
3338
resolve('foo', { basedir: dir }, function (err) {
3439
t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
3540
t.equal(err.code, 'MODULE_NOT_FOUND');

test/resolver_sync.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ test('foo', function (t) {
1515
path.join(dir, 'foo.js')
1616
);
1717

18+
t.equal(
19+
resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
20+
path.join(dir, 'foo.js')
21+
);
22+
1823
t.throws(function () {
1924
resolve.sync('foo', { basedir: dir });
2025
});

0 commit comments

Comments
 (0)