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

fix(jest-resolve): don't confuse directories with files #8912

Merged
merged 5 commits into from
Feb 6, 2020

Conversation

SimenB
Copy link
Member

@SimenB SimenB commented Sep 4, 2019

Summary

Fixes #8910

This might be an inherited bug from resolve: https://github.com/browserify/resolve/blob/bd3f55188f0f3d1dac1e29c2f9a0eaa0878d4bd0/lib/sync.js#L69 which this code came from in #4315.
EDIT: No, seems like resolve works correctly

Test plan

Test added

const resolveTarget = path.resolve(basedir, target);
let resolveTarget = path.resolve(basedir, target);
if (target === '..' || target.endsWith('/')) {
resolveTarget += '/';
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

not sure about the target.endsWith('/') but, what if the target is . instead of ./?

Copy link
Member Author

Choose a reason for hiding this comment

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

good call. I added a test for it, but I don't think it's the correct fix either

Copy link
Collaborator

@thymikee thymikee Sep 8, 2019

Choose a reason for hiding this comment

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

How about checking for directory first?

diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts
index 40ded2c1f..131234b8c 100644
--- a/packages/jest-resolve/src/defaultResolver.ts
+++ b/packages/jest-resolve/src/defaultResolver.ts
@@ -60,10 +60,7 @@ function resolveSync(

   if (REGEX_RELATIVE_IMPORT.test(target)) {
     // resolve relative import
-    let resolveTarget = path.resolve(basedir, target);
-    if (target === '.' || target === '..' || target.endsWith('/')) {
-      resolveTarget += '/';
-    }
+    const resolveTarget = path.resolve(basedir, target);
     const result = tryResolve(resolveTarget);
     if (result) {
       return result;
@@ -100,7 +97,7 @@ function resolveSync(
     const dir = path.dirname(name);
     let result;
     if (isDirectory(dir)) {
-      result = resolveAsFile(name) || resolveAsDirectory(name);
+      result = resolveAsDirectory(name) || resolveAsFile(name);
     }
     if (result) {
       // Dereference symlinks to ensure we don't create a separate

Seems to work, test are passing.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, that was my original fix, see 499e10a. But it's not that way upstream, which works correctly...

Copy link
Contributor

Choose a reason for hiding this comment

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

Why does upstream not handle '.' though? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

hah, good catch. That's a bug there, I opened up a PR. But that means I'm actually happy with this implementation 😅

@codecov-io
Copy link

codecov-io commented Sep 4, 2019

Codecov Report

Merging #8912 into master will decrease coverage by 0.01%.
The diff coverage is 57.14%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #8912      +/-   ##
==========================================
- Coverage   65.04%   65.02%   -0.02%     
==========================================
  Files         283      283              
  Lines       12160    12114      -46     
  Branches     3015     3000      -15     
==========================================
- Hits         7909     7877      -32     
+ Misses       3608     3598      -10     
+ Partials      643      639       -4
Impacted Files Coverage Δ
packages/jest-resolve/src/defaultResolver.ts 68.96% <57.14%> (-0.37%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c4c2ad4...39f0cae. Read the comment docs.

Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

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

@@ -156,6 +156,20 @@ describe('resolveModule', () => {
});
expect(resolved).toBe(require.resolve('../__mocks__/mockJsDependency.js'));
});

it('does not confuse directories with files', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there tests for '..'?

const resolveTarget = path.resolve(basedir, target);
let resolveTarget = path.resolve(basedir, target);
if (target === '..' || target.endsWith('/')) {
resolveTarget += '/';
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does upstream not handle '.' though? 🤔

@SimenB
Copy link
Member Author

SimenB commented Feb 5, 2020

I looked into this again in light of #9520 - this bug is present in upstream resolve, I must have tested it wrong when I looked into it. Opened up an issue: browserify/resolve#211

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

require('./') broken if parent directory has file with the same name
5 participants