Skip to content

Conversation

@kamilogorek
Copy link
Contributor

@kamilogorek kamilogorek commented Apr 19, 2017

The current behavior is not in line with node's implementation.

require.resolve('./foo') // 'foo.js'
require.resolve('./foo/') // 'foo/index.js'

resolve.sync('./foo') // 'foo.js'
resolve.sync('./foo/') // 'foo.js'

It affects both, sync and async methods and I fixed both of them.

Related Jest issue with more details jestjs/jest#3199

Fixes #51. Closes #53.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Thanks, this seems like a great fix!

lib/async.js Outdated
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
var res = path.resolve(y, x);
if (x === '..') res += '/';
if (x === '..' || x[x.length - 1] === '/') res += '/';
Copy link
Member

Choose a reason for hiding this comment

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

indexing into strings doesn't work in IE 6-8 (it crashes pretty hard) - i'd prefer to use .slice(-1) or .charAt(x.length - 1) here.

lib/sync.js Outdated
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
var res = path.resolve(y, x);
if (x === '..') res += '/';
if (x === '..' || x[x.length - 1] === '/') res += '/';
Copy link
Member

Choose a reason for hiding this comment

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

also here

var dir = path.join(__dirname, 'resolver');

t.equal(
resolve.sync('./foo/', { basedir: path.join(dir, 'same_names') }),
Copy link
Member

Choose a reason for hiding this comment

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

could we also add a test for ./foo, to ensure that that ends up with foo.js?


var dir = path.join(__dirname, 'resolver');

resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
Copy link
Member

Choose a reason for hiding this comment

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

could we also add a test for ./foo, to ensure that resolves to foo.js?

@kamilogorek
Copy link
Contributor Author

@ljharb updated all 4 requests :)

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Thanks!

@ljharb ljharb merged commit a983d38 into browserify:master Apr 19, 2017
ljharb added a commit that referenced this pull request Apr 20, 2017
 - [Fix] `sync`: ensure that the path is a string, explicitly
 - [Fix] correctly resolve dir paths when file with the same name exists (#124)
 - [Fix] error code MODULE_NOT_FOUND instead of ENOTDIR (#121)
 - [Dev Deps] update `eslint`
 - [Tests] improve failure scenarios
 - [Tests] [eslint] add `npm run lint`
 - [Tests] up to `node` `v7.9`, `v6.10`, `v4.8`; comment out OSX builds
 - [Tests] node 0.6 can’t support an npm that understands scoped packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

node-resolve incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name

2 participants