Skip to content

Commit 5e9d428

Browse files
committed
[Fix] error code MODULE_NOT_FOUND instead of ENOTDIR
Fixes #121.
1 parent a983d38 commit 5e9d428

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

lib/async.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ module.exports = function resolve(x, options, callback) {
2020

2121
var isFile = opts.isFile || function (file, cb) {
2222
fs.stat(file, function (err, stat) {
23-
if (err && err.code === 'ENOENT') cb(null, false);
24-
else if (err) cb(err);
25-
else cb(null, stat.isFile() || stat.isFIFO());
23+
if (!err) {
24+
return cb(null, stat.isFile() || stat.isFIFO());
25+
}
26+
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
27+
return cb(err);
2628
});
2729
};
2830
var readFile = opts.readFile || fs.readFile;

lib/sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function (x, options) {
1313
try {
1414
var stat = fs.statSync(file);
1515
} catch (e) {
16-
if (e && e.code === 'ENOENT') return false;
16+
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
1717
throw e;
1818
}
1919
return stat.isFile() || stat.isFIFO();

test/resolver.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,34 @@ test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is
294294
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
295295
});
296296
});
297+
298+
test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
299+
var testFile = path.basename(__filename);
300+
301+
t.test('sanity check', function (st) {
302+
st.plan(1);
303+
resolve('./' + testFile, function (err, res, pkg) {
304+
if (err) t.fail(err);
305+
st.equal(res, __filename, 'sanity check');
306+
});
307+
});
308+
309+
t.test('with a fake directory', function (st) {
310+
st.plan(4);
311+
312+
resolve('./' + testFile + '/blah', function (err, res, pkg) {
313+
st.ok(err, 'there is an error');
314+
st.notOk(res, 'no result');
315+
316+
st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
317+
st.equal(
318+
err && err.message,
319+
'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
320+
'can not find nonexistent module'
321+
);
322+
st.end();
323+
});
324+
});
325+
326+
t.end();
327+
});

test/resolver_sync.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,37 @@ test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is
217217
);
218218
t.end();
219219
});
220+
221+
test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
222+
var testFile = path.basename(__filename);
223+
224+
t.test('sanity check', function (st) {
225+
st.equal(
226+
resolve.sync('./' + testFile),
227+
__filename,
228+
'sanity check'
229+
);
230+
st.end();
231+
});
232+
233+
t.test('with a fake directory', function (st) {
234+
function run() { return resolve.sync('./' + testFile + '/blah'); }
235+
236+
st.throws(run, 'throws an error');
237+
238+
try {
239+
run();
240+
} catch (e) {
241+
st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
242+
st.equal(
243+
e.message,
244+
'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
245+
'can not find nonexistent module'
246+
);
247+
}
248+
249+
st.end();
250+
});
251+
252+
t.end();
253+
});

0 commit comments

Comments
 (0)