Skip to content

Commit

Permalink
[Fix] correct behavior when requiring . with same name
Browse files Browse the repository at this point in the history
Fix #211.
  • Loading branch information
SimenB authored and ljharb committed Feb 5, 2020
1 parent f4b27af commit 0d0ffa1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ module.exports = function resolve(x, options, callback) {
function validBasedir(basedir) {
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
res = path.resolve(basedir, x);
if (x === '..' || x.slice(-1) === '/') res += '/';
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
if ((/\/$/).test(x) && res === basedir) {
loadAsDirectory(res, opts.package, onfile);
} else loadAsFile(res, opts.package, onfile);
Expand Down
2 changes: 1 addition & 1 deletion lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = function resolveSync(x, options) {

if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
var res = path.resolve(absoluteStart, x);
if (x === '..' || x.slice(-1) === '/') res += '/';
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) return maybeUnwrapSymlink(m, opts);
} else if (isCore(x)) {
Expand Down
16 changes: 16 additions & 0 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,22 @@ test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is
});
});

test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
t.plan(2);

var dir = path.join(__dirname, 'resolver');

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

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

test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
var testFile = path.basename(__filename);

Expand Down
14 changes: 14 additions & 0 deletions test/resolver_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is
t.end();
});

test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
var dir = path.join(__dirname, 'resolver');

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

test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
var testFile = path.basename(__filename);

Expand Down

0 comments on commit 0d0ffa1

Please sign in to comment.