Skip to content

[New] include filename in error message #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,22 @@ module.exports = function resolve(x, options, callback) {

var extensions = opts.extensions || ['.js'];
var basedir = opts.basedir || path.dirname(caller());
var parent = opts.filename || basedir;

opts.paths = opts.paths || [];

if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
var res = path.resolve(basedir, x);
var res = path.resolve(parent, x);
if (x === '..' || x.slice(-1) === '/') res += '/';
if (/\/$/.test(x) && res === basedir) {
if (/\/$/.test(x) && res === parent) {
loadAsDirectory(res, opts.package, onfile);
} else loadAsFile(res, opts.package, onfile);
} else loadNodeModules(x, basedir, function (err, n, pkg) {
} else loadNodeModules(x, parent, function (err, n, pkg) {
if (err) cb(err);
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 + "'");
var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'");
moduleError.code = 'MODULE_NOT_FOUND';
cb(moduleError);
}
Expand All @@ -60,7 +61,7 @@ module.exports = function resolve(x, options, callback) {
if (err) cb(err);
else if (d) cb(null, d, pkg);
else {
var moduleError = new Error("Cannot find module '" + x + "' from '" + basedir + "'");
var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'");
moduleError.code = 'MODULE_NOT_FOUND';
cb(moduleError);
}
Expand Down
7 changes: 4 additions & 3 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ module.exports = function (x, options) {

var extensions = opts.extensions || ['.js'];
var basedir = opts.basedir || path.dirname(caller());
var parent = opts.filename || basedir;

opts.paths = opts.paths || [];

if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
var res = path.resolve(basedir, x);
var res = path.resolve(parent, x);
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);
var n = loadNodeModulesSync(x, parent);
if (n) return n;
}

if (core[x]) return x;

var err = new Error("Cannot find module '" + x + "' from '" + basedir + "'");
var err = new Error("Cannot find module '" + x + "' from '" + parent + "'");
err.code = 'MODULE_NOT_FOUND';
throw err;

Expand Down
14 changes: 12 additions & 2 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var test = require('tape');
var resolve = require('../');

test('async foo', function (t) {
t.plan(10);
t.plan(11);
var dir = path.join(__dirname, 'resolver');

resolve('./foo', { basedir: dir }, function (err, res, pkg) {
Expand Down Expand Up @@ -34,6 +34,11 @@ test('async foo', function (t) {
t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});

// Test that filename is reported as the "from" value when passed.
resolve('foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err) {
t.equal(err.message, "Cannot find module 'foo' from '" + path.join(dir, 'baz.js') + "'");
});
});

test('bar', function (t) {
Expand Down Expand Up @@ -176,7 +181,7 @@ test('normalize', function (t) {
});

test('cup', function (t) {
t.plan(4);
t.plan(5);
var dir = path.join(__dirname, 'resolver');

resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
Expand All @@ -193,6 +198,11 @@ test('cup', function (t) {
t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});

// Test that filename is reported as the "from" value when passed.
resolve('./cup', { basedir: dir, extensions: ['.js'], filename: path.join(dir, 'cupboard.js') }, function (err, res) {
t.equal(err.message, "Cannot find module './cup' from '" + path.join(dir, 'cupboard.js') + "'");
});
});

test('mug', function (t) {
Expand Down
11 changes: 11 additions & 0 deletions test/resolver_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ test('foo', function (t) {
resolve.sync('foo', { basedir: dir });
});

// Test that filename is reported as the "from" value when passed.
t.throws(
function () {
resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
},
{
name: 'Error',
message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
}
);

t.end();
});

Expand Down