From 3e4f5bb85fd3070867c29977e3ae8aa048c6b228 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 5 Feb 2020 08:51:29 -1000 Subject: [PATCH 1/2] [Tests] allow node 5 on windows to fail due to npm registry bug --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index 729e8ea0..9458fb82 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -23,6 +23,7 @@ environment: matrix: # fast_finish: true allow_failures: + - nodejs_version: "5" # due to windows npm bug, registry-side - nodejs_version: "0.8" - nodejs_version: "0.6" From fc95653c6f8159f9a13ceacc7d859e9e9839838a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 5 Feb 2020 17:23:15 +0100 Subject: [PATCH 2/2] [Fix] correct behavior when requiring `.` with same name Fix #211. --- lib/async.js | 2 +- lib/sync.js | 2 +- test/resolver.js | 16 ++++++++++++++++ test/resolver_sync.js | 14 ++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/async.js b/lib/async.js index 40227a1f..fc6d6839 100644 --- a/lib/async.js +++ b/lib/async.js @@ -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); diff --git a/lib/sync.js b/lib/sync.js index 651ecc27..11c2a559 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -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)) { diff --git a/test/resolver.js b/test/resolver.js index 5df8e1d0..e7575834 100644 --- a/test/resolver.js +++ b/test/resolver.js @@ -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); diff --git a/test/resolver_sync.js b/test/resolver_sync.js index aa8e2568..5a96f081 100644 --- a/test/resolver_sync.js +++ b/test/resolver_sync.js @@ -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);