Skip to content

Commit fc95653

Browse files
SimenBljharb
authored andcommitted
[Fix] correct behavior when requiring . with same name
Fix #211.
1 parent 3e4f5bb commit fc95653

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

lib/async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module.exports = function resolve(x, options, callback) {
101101
function validBasedir(basedir) {
102102
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
103103
res = path.resolve(basedir, x);
104-
if (x === '..' || x.slice(-1) === '/') res += '/';
104+
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
105105
if ((/\/$/).test(x) && res === basedir) {
106106
loadAsDirectory(res, opts.package, onfile);
107107
} else loadAsFile(res, opts.package, onfile);

lib/sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = function resolveSync(x, options) {
7474

7575
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
7676
var res = path.resolve(absoluteStart, x);
77-
if (x === '..' || x.slice(-1) === '/') res += '/';
77+
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
7878
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
7979
if (m) return maybeUnwrapSymlink(m, opts);
8080
} else if (isCore(x)) {

test/resolver.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,22 @@ test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is
315315
});
316316
});
317317

318+
test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
319+
t.plan(2);
320+
321+
var dir = path.join(__dirname, 'resolver');
322+
323+
resolve('./', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
324+
if (err) t.fail(err);
325+
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
326+
});
327+
328+
resolve('.', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
329+
if (err) t.fail(err);
330+
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
331+
});
332+
});
333+
318334
test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
319335
var testFile = path.basename(__filename);
320336

test/resolver_sync.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,20 @@ test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is
238238
t.end();
239239
});
240240

241+
test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
242+
var dir = path.join(__dirname, 'resolver');
243+
244+
t.equal(
245+
resolve.sync('./', { basedir: path.join(dir, 'same_names/foo') }),
246+
path.join(dir, 'same_names/foo/index.js')
247+
);
248+
t.equal(
249+
resolve.sync('.', { basedir: path.join(dir, 'same_names/foo') }),
250+
path.join(dir, 'same_names/foo/index.js')
251+
);
252+
t.end();
253+
});
254+
241255
test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
242256
var testFile = path.basename(__filename);
243257

0 commit comments

Comments
 (0)