Skip to content

Commit 891bd89

Browse files
SimenBljharb
authored andcommitted
[Fix] correct behavior when requiring . with same name (#212)
Fixes #211.
1 parent 16c1170 commit 891bd89

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
@@ -87,7 +87,7 @@ module.exports = function resolve(x, options, callback) {
8787
function init(basedir) {
8888
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
8989
res = path.resolve(basedir, x);
90-
if (x === '..' || x.slice(-1) === '/') res += '/';
90+
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
9191
if ((/\/$/).test(x) && res === basedir) {
9292
loadAsDirectory(res, opts.package, onfile);
9393
} else loadAsFile(res, opts.package, onfile);

lib/sync.js

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

6969
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
7070
var res = path.resolve(absoluteStart, x);
71-
if (x === '..' || x.slice(-1) === '/') res += '/';
71+
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
7272
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
7373
if (m) return maybeUnwrapSymlink(m, opts);
7474
} 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)