Skip to content

Commit 9c370c9

Browse files
goto-bus-stopljharb
authored andcommitted
[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 3a64219 commit 9c370c9

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
@@ -38,12 +38,12 @@ module.exports = function resolve(x, options, callback) {
3838
opts.paths = opts.paths || [];
3939

4040
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
41-
var res = path.resolve(parent, x);
41+
var res = path.resolve(basedir, x);
4242
if (x === '..' || x.slice(-1) === '/') res += '/';
43-
if (/\/$/.test(x) && res === parent) {
43+
if (/\/$/.test(x) && res === basedir) {
4444
loadAsDirectory(res, opts.package, onfile);
4545
} else loadAsFile(res, opts.package, onfile);
46-
} else loadNodeModules(x, parent, function (err, n, pkg) {
46+
} else loadNodeModules(x, basedir, function (err, n, pkg) {
4747
if (err) cb(err);
4848
else if (n) cb(null, n, pkg);
4949
else if (core[x]) return cb(null, x);

lib/sync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ module.exports = function (x, options) {
2929
opts.paths = opts.paths || [];
3030

3131
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
32-
var res = path.resolve(parent, x);
32+
var res = path.resolve(basedir, x);
3333
if (x === '..' || x.slice(-1) === '/') res += '/';
3434
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
3535
if (m) return m;
3636
} else {
37-
var n = loadNodeModulesSync(x, parent);
37+
var n = loadNodeModulesSync(x, basedir);
3838
if (n) return n;
3939
}
4040

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)