Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix async incorrect main #28

Closed
wants to merge 2 commits into from

1 participant

@thlorenz

sync resolve already found index.js if "main" field in package.json was incorrectly specified (same way that nodejs does).
This fix makes async resolve behave the same way.

If the white space changes are a problem let me know. -- my vim is set up to remove unnecessary white space on save (I forgot about that).

thlorenz added some commits
@thlorenz thlorenz adding tests to reproduce the problem
- resolver test shows that async resolve fails when main field in package.json is incorrect
- resolver_sync test shows that sync resolve finds index.js under same circumstance
2c5a208
@thlorenz thlorenz async resolve now falls back to 'index.js' if main field in package.j…
…son is incorrect
4ada98d
@thlorenz thlorenz closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 30, 2013
  1. @thlorenz

    adding tests to reproduce the problem

    thlorenz authored
    - resolver test shows that async resolve fails when main field in package.json is incorrect
    - resolver_sync test shows that sync resolve finds index.js under same circumstance
  2. @thlorenz
This page is out of date. Refresh to see the latest.
View
40 lib/async.js
@@ -4,13 +4,13 @@ var path = require('path');
module.exports = function resolve (x, opts, cb) {
if (core[x]) return cb(null, x);
-
+
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
if (!opts) opts = {};
-
+
var isFile = opts.isFile || function (file, cb) {
fs.stat(file, function (err, stat) {
if (err && err.code === 'ENOENT') cb(null, false)
@@ -19,14 +19,14 @@ module.exports = function resolve (x, opts, cb) {
});
};
var readFile = opts.readFile || fs.readFile;
-
+
var extensions = opts.extensions || [ '.js' ];
var y = opts.basedir
|| path.dirname(require.cache[__filename].parent.filename)
;
-
+
opts.paths = opts.paths || [];
-
+
if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) {
loadAsFile(path.resolve(y, x), function (err, m, pkg) {
if (err) cb(err)
@@ -43,17 +43,17 @@ module.exports = function resolve (x, opts, cb) {
else if (n) cb(null, n, pkg)
else cb(new Error("Cannot find module '" + x + "'"))
});
-
+
function loadAsFile (x, pkg, cb) {
if (typeof pkg === 'function') {
cb = pkg;
pkg = opts.package;
}
-
+
(function load (exts) {
if (exts.length === 0) return cb(null, undefined);
var file = x + exts[0];
-
+
isFile(file, function (err, ex) {
if (err) cb(err)
else if (ex) cb(null, file, pkg)
@@ -61,33 +61,35 @@ module.exports = function resolve (x, opts, cb) {
});
})([''].concat(extensions));
}
-
+
function loadAsDirectory (x, fpkg, cb) {
if (typeof fpkg === 'function') {
cb = fpkg;
fpkg = opts.package;
}
-
+
var pkgfile = path.join(x, '/package.json');
isFile(pkgfile, function (err, ex) {
if (err) return cb(err);
if (!ex) return loadAsFile(path.join(x, '/index'), fpkg, cb);
-
+
readFile(pkgfile, function (err, body) {
if (err) return cb(err);
try {
var pkg = JSON.parse(body);
}
catch (err) {}
-
+
if (opts.packageFilter) {
pkg = opts.packageFilter(pkg, x);
}
-
+
if (pkg.main) {
loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) {
if (err) return cb(err);
if (m) return cb(null, m, pkg);
+ if (!pkg) return loadAsFile(path.join(x, '/index'), pkg, cb);
+
var dir = path.resolve(x, pkg.main);
loadAsDirectory(dir, pkg, function (err, n, pkg) {
if (err) return cb(err);
@@ -97,17 +99,17 @@ module.exports = function resolve (x, opts, cb) {
});
return;
}
-
+
loadAsFile(path.join(x, '/index'), pkg, cb);
});
});
}
-
+
function loadNodeModules (x, start, cb) {
(function process (dirs) {
if (dirs.length === 0) return cb(null, undefined);
var dir = dirs[0];
-
+
loadAsFile(path.join(dir, '/', x), undefined, function (err, m, pkg) {
if (err) return cb(err);
if (m) return cb(null, m, pkg);
@@ -119,11 +121,11 @@ module.exports = function resolve (x, opts, cb) {
});
})(nodeModulesPaths(start));
}
-
+
function nodeModulesPaths (start, cb) {
var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
var parts = start.split(splitRe);
-
+
var dirs = [];
for (var i = parts.length - 1; i >= 0; i--) {
if (parts[i] === 'node_modules') continue;
@@ -132,7 +134,7 @@ module.exports = function resolve (x, opts, cb) {
'node_modules'
);
if (!parts[0].match(/([A-Za-z]:)/)) {
- dir = '/' + dir;
+ dir = '/' + dir;
}
dirs.push(dir);
}
View
68 test/resolver.js
@@ -4,31 +4,31 @@ var resolve = require('../');
test('async foo', function (t) {
t.plan(9);
var dir = __dirname + '/resolver';
-
+
resolve('./foo', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo.js');
t.equal(pkg, undefined);
});
-
+
resolve('./foo.js', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo.js');
t.equal(pkg, undefined);
});
-
+
resolve('./foo', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo.js');
t.equal(pkg.main, 'resolver');
});
-
+
resolve('./foo.js', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo.js');
t.equal(pkg.main, 'resolver');
});
-
+
resolve('foo', { basedir : dir }, function (err) {
t.equal(err.message, "Cannot find module 'foo'");
});
@@ -37,19 +37,19 @@ test('async foo', function (t) {
test('bar', function (t) {
t.plan(6);
var dir = __dirname + '/resolver';
-
+
resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/bar/node_modules/foo/index.js');
t.equal(pkg, undefined);
});
-
+
resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/bar/node_modules/foo/index.js');
t.equal(pkg, undefined);
});
-
+
resolve('foo', { basedir : dir + '/bar', package: { main: 'bar' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/bar/node_modules/foo/index.js');
@@ -60,13 +60,13 @@ test('bar', function (t) {
test('baz', function (t) {
t.plan(4);
var dir = __dirname + '/resolver';
-
+
resolve('./baz', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/baz/quux.js');
t.equal(pkg.main, 'quux.js');
});
-
+
resolve('./baz', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/baz/quux.js');
@@ -77,37 +77,37 @@ test('baz', function (t) {
test('biz', function (t) {
t.plan(12);
var dir = __dirname + '/resolver/biz/node_modules';
-
+
resolve('./grux', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/grux/index.js');
t.equal(pkg, undefined);
});
-
+
resolve('./grux', { basedir : dir, package: { main: 'biz' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/grux/index.js');
t.equal(pkg.main, 'biz');
});
-
+
resolve('tiv', { basedir : dir + '/grux' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/tiv/index.js');
t.equal(pkg, undefined);
});
-
+
resolve('tiv', { basedir : dir + '/grux', package: { main: 'grux' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/tiv/index.js');
t.equal(pkg, undefined);
});
-
+
resolve('grux', { basedir : dir + '/tiv' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/grux/index.js');
t.equal(pkg, undefined);
});
-
+
resolve('grux', { basedir : dir + '/tiv', package: { main: 'tiv' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/grux/index.js');
@@ -118,7 +118,7 @@ test('biz', function (t) {
test('quux', function (t) {
t.plan(2);
var dir = __dirname + '/resolver/quux';
-
+
resolve('./foo', { basedir : dir, package: { main: 'quux' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo/index.js');
@@ -129,7 +129,7 @@ test('quux', function (t) {
test('normalize', function (t) {
t.plan(2);
var dir = __dirname + '/resolver/biz/node_modules/grux';
-
+
resolve('../grux', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/index.js');
@@ -140,18 +140,18 @@ test('normalize', function (t) {
test('cup', function (t) {
t.plan(3);
var dir = __dirname + '/resolver';
-
+
resolve('./cup', { basedir : dir, extensions : [ '.js', '.coffee' ] },
function (err, res) {
if (err) t.fail(err);
t.equal(res, dir + '/cup.coffee');
});
-
+
resolve('./cup.coffee', { basedir : dir }, function (err, res) {
if (err) t.fail(err);
t.equal(res, dir + '/cup.coffee');
});
-
+
resolve('./cup', { basedir : dir, extensions : [ '.js' ] },
function (err, res) {
t.equal(err.message, "Cannot find module './cup'");
@@ -161,18 +161,18 @@ test('cup', function (t) {
test('mug', function (t) {
t.plan(3);
var dir = __dirname + '/resolver';
-
+
resolve('./mug', { basedir : dir }, function (err, res) {
if (err) t.fail(err);
t.equal(res, dir + '/mug.js');
});
-
+
resolve('./mug', { basedir : dir, extensions : [ '.coffee', '.js' ] },
function (err, res) {
if (err) t.fail(err);
t.equal(res, dir + '/mug.coffee');
});
-
+
resolve('./mug', { basedir : dir, extensions : [ '.js', '.coffee' ] },
function (err, res) {
t.equal(res, dir + '/mug.js');
@@ -184,23 +184,35 @@ test('other path', function (t) {
var resolverDir = __dirname + '/resolver';
var dir = resolverDir + '/bar';
var otherDir = resolverDir + '/other_path';
-
+
resolve('root', { basedir : dir, paths: [otherDir] }, function (err, res) {
if (err) t.fail(err);
t.equal(res, resolverDir + '/other_path/root.js');
});
-
+
resolve('lib/other-lib', { basedir : dir, paths: [otherDir] },
function (err, res) {
if (err) t.fail(err);
t.equal(res, resolverDir + '/other_path/lib/other-lib.js');
});
-
+
resolve('root', { basedir : dir, }, function (err, res) {
t.equal(err.message, "Cannot find module 'root'");
});
-
+
resolve('zzz', { basedir : dir, paths: [otherDir] }, function (err, res) {
t.equal(err.message, "Cannot find module 'zzz'");
});
});
+
+test('incorrect main', function (t) {
+ t.plan(1)
+
+ var resolverDir = __dirname + '/resolver';
+ var dir = resolverDir + '/incorrect_main';
+
+ resolve('./incorrect_main', { basedir : resolverDir }, function (err, res, pkg) {
+ if (err) t.fail(err);
+ t.equal(res, dir + '/index.js');
+ });
+});
View
2  test/resolver/incorrect_main/index.js
@@ -0,0 +1,2 @@
+// this is the actual main file 'index.js', not 'wrong.js' like the package.json would indicate
+module.exports = 1;
View
3  test/resolver/incorrect_main/package.json
@@ -0,0 +1,3 @@
+{
+ "main" : "wrong.js"
+}
View
48 test/resolver_sync.js
@@ -3,27 +3,27 @@ var resolve = require('../');
test('foo', function (t) {
var dir = __dirname + '/resolver';
-
+
t.equal(
resolve.sync('./foo', { basedir : dir }),
dir + '/foo.js'
);
-
+
t.equal(
resolve.sync('./foo.js', { basedir : dir }),
dir + '/foo.js'
);
-
+
t.throws(function () {
resolve.sync('foo', { basedir : dir });
});
-
+
t.end();
});
test('bar', function (t) {
var dir = __dirname + '/resolver';
-
+
t.equal(
resolve.sync('foo', { basedir : dir + '/bar' }),
dir + '/bar/node_modules/foo/index.js'
@@ -33,7 +33,7 @@ test('bar', function (t) {
test('baz', function (t) {
var dir = __dirname + '/resolver';
-
+
t.equal(
resolve.sync('./baz', { basedir : dir }),
dir + '/baz/quux.js'
@@ -47,12 +47,12 @@ test('biz', function (t) {
resolve.sync('./grux', { basedir : dir }),
dir + '/grux/index.js'
);
-
+
t.equal(
resolve.sync('tiv', { basedir : dir + '/grux' }),
dir + '/tiv/index.js'
);
-
+
t.equal(
resolve.sync('grux', { basedir : dir + '/tiv' }),
dir + '/grux/index.js'
@@ -78,21 +78,21 @@ test('cup', function (t) {
}),
dir + '/cup.coffee'
);
-
+
t.equal(
resolve.sync('./cup.coffee', {
basedir : dir
}),
dir + '/cup.coffee'
);
-
+
t.throws(function () {
resolve.sync('./cup', {
basedir : dir,
extensions : [ '.js' ]
})
});
-
+
t.end();
});
@@ -102,7 +102,7 @@ test('mug', function (t) {
resolve.sync('./mug', { basedir : dir }),
dir + '/mug.js'
);
-
+
t.equal(
resolve.sync('./mug', {
basedir : dir,
@@ -110,7 +110,7 @@ test('mug', function (t) {
}),
dir + '/mug.coffee'
);
-
+
t.equal(
resolve.sync('./mug', {
basedir : dir,
@@ -118,7 +118,7 @@ test('mug', function (t) {
}),
dir + '/mug.js'
);
-
+
t.end();
});
@@ -128,14 +128,14 @@ test('other path', function (t) {
var otherDir = resolverDir + '/other_path';
var path = require('path');
-
+
t.equal(
resolve.sync('root', {
basedir : dir,
paths: [otherDir] }),
resolverDir + '/other_path/root.js'
);
-
+
t.equal(
resolve.sync('lib/other-lib', {
basedir : dir,
@@ -146,12 +146,24 @@ test('other path', function (t) {
t.throws(function () {
resolve.sync('root', { basedir : dir, });
});
-
+
t.throws(function () {
resolve.sync('zzz', {
basedir : dir,
paths: [otherDir] });
});
-
+
t.end();
});
+
+test('incorrect main', function (t) {
+ var resolverDir = __dirname + '/resolver';
+ var dir = resolverDir + '/incorrect_main';
+
+ t.equal(
+ resolve.sync('./incorrect_main', { basedir : resolverDir }),
+ dir + '/index.js'
+ )
+
+ t.end()
+});
Something went wrong with that request. Please try again.