diff --git a/lib/async.js b/lib/async.js index dd889dc7..a73b6a1f 100644 --- a/lib/async.js +++ b/lib/async.js @@ -44,8 +44,8 @@ module.exports = function resolve(x, options, callback) { } else loadAsFile(res, opts.package, onfile); } else loadNodeModules(x, basedir, function (err, n, pkg) { if (err) cb(err); - else if (n) cb(null, n, pkg); else if (core[x]) return cb(null, x); + else if (n) return cb(null, n, pkg); else { var moduleError = new Error("Cannot find module '" + x + "' from '" + basedir + "'"); moduleError.code = 'MODULE_NOT_FOUND'; diff --git a/lib/sync.js b/lib/sync.js index 2e0d5f24..97f89b5d 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -32,6 +32,8 @@ module.exports = function (x, options) { if (x === '..' || x.slice(-1) === '/') res += '/'; var m = loadAsFileSync(res) || loadAsDirectorySync(res); if (m) return m; + } else if (core[x]) { + return x; } else { var n = loadNodeModulesSync(x, basedir); if (n) return n; diff --git a/test/resolver.js b/test/resolver.js index b82927b2..116bb242 100644 --- a/test/resolver.js +++ b/test/resolver.js @@ -268,17 +268,6 @@ test('without basedir', function (t) { }); }); -test('#25: node modules with the same name as node stdlib modules', function (t) { - t.plan(1); - - var resolverDir = path.join(__dirname, 'resolver/punycode'); - - resolve('punycode', { basedir: resolverDir }, function (err, res, pkg) { - if (err) t.fail(err); - t.equal(res, path.join(resolverDir, 'node_modules/punycode/index.js')); - }); -}); - test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) { t.plan(2); diff --git a/test/resolver_sync.js b/test/resolver_sync.js index 4a4fd477..5b8057fe 100644 --- a/test/resolver_sync.js +++ b/test/resolver_sync.js @@ -168,17 +168,6 @@ test('incorrect main', function (t) { t.end(); }); -test('#25: node modules with the same name as node stdlib modules', function (t) { - var resolverDir = path.join(__dirname, 'resolver/punycode'); - - t.equal( - resolve.sync('punycode', { basedir: resolverDir }), - path.join(resolverDir, 'node_modules/punycode/index.js') - ); - - t.end(); -}); - var stubStatSync = function stubStatSync(fn) { var fs = require('fs'); var statSync = fs.statSync; diff --git a/test/shadowed_core.js b/test/shadowed_core.js new file mode 100644 index 00000000..98c52a76 --- /dev/null +++ b/test/shadowed_core.js @@ -0,0 +1,38 @@ +var test = require('tape'); +var resolve = require('../'); +var path = require('path'); + +test('shadowed core modules still return core module', function (t) { + t.plan(2); + + resolve('util', { basedir: path.join(__dirname, 'shadowed_core') }, function (err, res) { + t.ifError(err); + t.equal(res, 'util'); + }); +}); + +test('shadowed core modules still return core module [sync]', function (t) { + t.plan(1); + + var res = resolve.sync('util', { basedir: path.join(__dirname, 'shadowed_core') }); + + t.equal(res, 'util'); +}); + +test('shadowed core modules return shadow when appending `/`', function (t) { + t.plan(2); + + resolve('util/', { basedir: path.join(__dirname, 'shadowed_core') }, function (err, res) { + t.ifError(err); + t.equal(res, path.join(__dirname, 'shadowed_core/node_modules/util/index.js')); + }); +}); + +test('shadowed core modules return shadow when appending `/` [sync]', function (t) { + t.plan(1); + + var res = resolve.sync('util/', { basedir: path.join(__dirname, 'shadowed_core') }); + + t.equal(res, path.join(__dirname, 'shadowed_core/node_modules/util/index.js')); +}); + diff --git a/test/shadowed_core/.gitignore b/test/shadowed_core/.gitignore new file mode 100644 index 00000000..b337ca4a --- /dev/null +++ b/test/shadowed_core/.gitignore @@ -0,0 +1 @@ +!/node_modules diff --git a/test/shadowed_core/node_modules/util/index.js b/test/shadowed_core/node_modules/util/index.js new file mode 100644 index 00000000..e69de29b