Skip to content
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

[New] include filename in error message #161

Closed
wants to merge 9 commits into from
13 changes: 7 additions & 6 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,23 @@ module.exports = function resolve(x, options, callback) {
var readFile = opts.readFile || fs.readFile;

var extensions = opts.extensions || ['.js'];
var basedir = opts.basedir || path.dirname(caller());
var y = opts.basedir || path.dirname(caller());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this use a better name than "y"? I'd previously renamed this to "basedir" for clarity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ljharb I just basically left @jmm's code intact, but I can certainly change y to basedir instead.

However, note that y is really just a temporary variable and not intended to have any significant meaning, I'm guessing from @jmm's code, because at the end of the day, what you want is, parent and y just splits up the derivation of parent into two parts for clarity.

Anyway, let me know whether you still want y to be basedir and I will make the change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the time they made their PR, the variable was called "y" :-) please do change it.

var parent = opts.filename || y;

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
9 changes: 5 additions & 4 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,26 @@ module.exports = function (x, options) {
var readFileSync = opts.readFileSync || fs.readFileSync;

var extensions = opts.extensions || ['.js'];
var basedir = opts.basedir || path.dirname(caller());
var y = opts.basedir || path.dirname(caller());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

var parent = opts.filename || y;

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
15 changes: 13 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,12 @@ 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
12 changes: 12 additions & 0 deletions test/resolver_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ 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