Skip to content

Commit

Permalink
[Breaking] bring resolve into line with require.resolve, which do…
Browse files Browse the repository at this point in the history
…es not respect trailing slashes on non-directories.
  • Loading branch information
ljharb committed Mar 13, 2017
1 parent 1354943 commit f4075e5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ module.exports = function resolve(x, options, callback) {

var res;
function validBasedir(basedir) {
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
res = path.resolve(basedir, x);
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
res = path.normalize(path.join(basedir, x));
if (x === '..' || x.slice(-1) === '/') res += '/';
if ((/\/$/).test(x) && res === basedir) {
loadAsDirectory(res, opts.package, onfile);
Expand Down
4 changes: 2 additions & 2 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ module.exports = function (x, options) {
throw dirError;
}

if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
var res = path.resolve(absoluteStart, x);
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
var res = path.normalize(path.join(basedir, x));
if (x === '..' || x.slice(-1) === '/') res += '/';
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) return m;
Expand Down
17 changes: 17 additions & 0 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@ test('async: #121 - treating an existing file as a dir when no basedir', functio
});
});

t.test('with a trailing slash', function (st) {
st.plan(4);

resolve('./' + testFile + '/', function (err, res, pkg) {
st.ok(err, 'there is an error');
st.notOk(res, 'no result');

st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
st.equal(
err && err.message,
'Cannot find module \'./' + testFile + '/\' from \'' + __dirname + '\'',
'can not find nonexistent module'
);
st.end();
});
});

t.test('with a fake directory', function (st) {
st.plan(4);

Expand Down
22 changes: 22 additions & 0 deletions test/resolver_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,28 @@ test('sync: #121 - treating an existing file as a dir when no basedir', function
st.end();
});

t.test('with a trailing slash', function (st) {
var path = './' + testFile + '/';
function run() { return resolve.sync(path); }

var result, error;
try {
result = run();
} catch (e) {
error = e;
}
st.ok(error, 'there is an error');
st.notOk(result, 'no result');
st.equal(error && error.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
st.equal(
error && error.message,
'Cannot find module \'' + path + '\' from \'' + __dirname + '\'',
'can not find nonexistent module'
);

st.end();
});

t.test('with a fake directory', function (st) {
function run() { return resolve.sync('./' + testFile + '/blah'); }

Expand Down

0 comments on commit f4075e5

Please sign in to comment.