Skip to content

Commit bd39676

Browse files
committed
[Breaking] bring resolve into line with require.resolve, which does not respect trailing slashes on non-directories.
1 parent 1354943 commit bd39676

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

lib/async.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ module.exports = function resolve(x, options, callback) {
8080
var res;
8181
function validBasedir(basedir) {
8282
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
83-
res = path.resolve(basedir, x);
83+
res = path.normalize(path.join(basedir, x));
8484
if (x === '..' || x.slice(-1) === '/') res += '/';
8585
if ((/\/$/).test(x) && res === basedir) {
8686
loadAsDirectory(res, opts.package, onfile);

lib/sync.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = function (x, options) {
6161
}
6262

6363
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
64-
var res = path.resolve(absoluteStart, x);
64+
var res = path.normalize(path.join(basedir, x));
6565
if (x === '..' || x.slice(-1) === '/') res += '/';
6666
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
6767
if (m) return m;

test/resolver.js

+17
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,23 @@ test('async: #121 - treating an existing file as a dir when no basedir', functio
310310
});
311311
});
312312

313+
t.test('with a trailing slash', function (st) {
314+
st.plan(4);
315+
316+
resolve('./' + testFile + '/', function (err, res, pkg) {
317+
st.ok(err, 'there is an error');
318+
st.notOk(res, 'no result');
319+
320+
st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
321+
st.equal(
322+
err && err.message,
323+
'Cannot find module \'./' + testFile + '/\' from \'' + __dirname + '\'',
324+
'can not find nonexistent module'
325+
);
326+
st.end();
327+
});
328+
});
329+
313330
t.test('with a fake directory', function (st) {
314331
st.plan(4);
315332

test/resolver_sync.js

+22
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,28 @@ test('sync: #121 - treating an existing file as a dir when no basedir', function
235235
st.end();
236236
});
237237

238+
t.test('with a trailing slash', function (st) {
239+
var path = './' + testFile + '/';
240+
function run() { return resolve.sync(path); }
241+
242+
var result, error;
243+
try {
244+
result = run();
245+
} catch (e) {
246+
error = e;
247+
}
248+
st.ok(error, 'there is an error');
249+
st.notOk(result, 'no result');
250+
st.equal(error && error.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
251+
st.equal(
252+
error && error.message,
253+
'Cannot find module \'' + path + '\' from \'' + __dirname + '\'',
254+
'can not find nonexistent module'
255+
);
256+
257+
st.end();
258+
});
259+
238260
t.test('with a fake directory', function (st) {
239261
function run() { return resolve.sync('./' + testFile + '/blah'); }
240262

0 commit comments

Comments
 (0)