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: filePath with "#" is a directory #2432

Closed
wants to merge 8 commits into from
Closed

fix: filePath with "#" is a directory #2432

wants to merge 8 commits into from

Conversation

YufJi
Copy link

@YufJi YufJi commented Mar 8, 2021

before, vite did not check filePah that includes '#' is adirectory; when code includes

const contains = require('es5-ext/string/#/contains')

throw error

@antfu antfu added the p3-minor-bug An edge case that only affects very specific usage (priority) label Mar 13, 2021
@@ -234,7 +234,7 @@ function tryFsResolve(
if (postfixIndex < 0) {
postfixIndex = fsPath.indexOf('#')
}
if (postfixIndex > 0) {
if (postfixIndex > 0 && !fs.existsSync(fsPath.slice(0, postfixIndex + 1))) {
Copy link
Member

Choose a reason for hiding this comment

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

Should use accessSync with try-catch (reason see the comment in tryResolveFile). And maybe we can extract it to a util function.

Copy link
Member

Choose a reason for hiding this comment

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

This would only work for cases where full extensions are being used. I think the more complete fix here is:

  1. Try resolving without removing postfixes first
  2. If that didn't work, treat ? and # as postfixes, remove them and try resolving again.

@@ -234,7 +234,7 @@ function tryFsResolve(
if (postfixIndex < 0) {
postfixIndex = fsPath.indexOf('#')
}
if (postfixIndex > 0) {
if (postfixIndex > 0 && !fs.existsSync(fsPath.slice(0, postfixIndex + 1))) {
Copy link
Member

Choose a reason for hiding this comment

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

This would only work for cases where full extensions are being used. I think the more complete fix here is:

  1. Try resolving without removing postfixes first
  2. If that didn't work, treat ? and # as postfixes, remove them and try resolving again.

@patak-dev patak-dev linked an issue Mar 28, 2021 that may be closed by this pull request
@patak-dev
Copy link
Member

patak-dev commented Mar 28, 2021

This PR would fix #2737, I linked that issue.

@YufJi would you like to work on the requested changes or do you prefer others to take on the task?

@Shinigami92 Shinigami92 marked this pull request as draft March 31, 2021 09:40
@YufJi
Copy link
Author

YufJi commented Mar 31, 2021

I have modified the code as yyx suggested

@Shinigami92
Copy link
Member

@YufJi There is a failing test, could you run rebuild everything locally and test it?

rm -Rf node_modules
yarn install
yarn build-vite
yarn build-plugin-vue
yarn test

@Shinigami92
Copy link
Member

@YufJi Okay tested it locally and I don't know why but ffd4c0d definitely broke it 🙁
Can you try fix it?

Seems that packages/playground/worker/__tests__/worker.spec.ts is failing, hope that helps

@YufJi
Copy link
Author

YufJi commented Apr 1, 2021

@YufJi Okay tested it locally and I don't know why but ffd4c0d definitely broke it 🙁
Can you try fix it?

Seems that packages/playground/worker/__tests__/worker.spec.ts is failing, hope that helps

Hi~ help review the change code.

@YufJi YufJi requested a review from yyx990803 April 1, 2021 18:50
@Shinigami92 Shinigami92 requested a review from antfu April 1, 2021 19:27
@Shinigami92 Shinigami92 marked this pull request as ready for review April 1, 2021 19:27
@Shinigami92
Copy link
Member

Shinigami92 commented Apr 1, 2021

@YufJi could you please update your branch

git switch main
git remote add upstream git://github.com/vitejs/vite.git
git fetch upstream
git pull upstream main
git push
git switch fix--filePath-with-'#'-is-a-direction
git merge main
# resolve conflicts if occur
git commit -m "chore: merge main into pr"
git push

or how to update a fork: https://gist.github.com/CristinaSolana/1885435


cause we are seeing something odd here. there is an optimization in another PR https://github.com/vitejs/vite/pull/2718/files#diff-9b81bb364c02eab9494a7d27a5effc400cacbffd3b8f349c192f890c37bfc83fR243-R247 that was brought in, but it isn't in your PR because we think its quite out of date

@YufJi
Copy link
Author

YufJi commented Apr 1, 2021

@YufJi could you please update your branch

git pull upstream main

or how to update a fork: https://gist.github.com/CristinaSolana/1885435

cause we are seeing something odd here. there is an optimization in another PR https://github.com/vitejs/vite/pull/2718/files#diff-9b81bb364c02eab9494a7d27a5effc400cacbffd3b8f349c192f890c37bfc83fR243-R247 that was brought in, but it isn't in your PR because we think its quite out of date

okay, let me try it~

@Shinigami92
Copy link
Member

Shinigami92 commented Apr 1, 2021

@YufJi I just updated the comment above to help sync the fork

let res: string | undefined

// 1、Try resolving without removing postfixes first
if ((res = tryResolve(file, postfix, options, tryIndex))) {
Copy link
Member

@patak-dev patak-dev Apr 2, 2021

Choose a reason for hiding this comment

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

We are going to lose the optimization introduced in #2718 with this PR.

When people use imports with extensions, we should first check for an exact match before trying the list of auto-resolved extensions. Also, I think that we should first with the extensions removed to avoid two checks per file for common cases.

The order of the tryResolveFile calls should be:

  file ( tryIndex: false )
  file ( tryIndex: false, removing postfixes )

  for each ext
    file+ext
    file+ext ( removing postfixes ) 

  file ( tryIndex: true )
  file ( tryIndex: true, removing postfixes )

I also think that if ? and # are inside the folder path and are not part of the file, we can safely remove the postfixes so we avoid the double checks. So we should check for the presence of ? and # only after the last /. This change alone should fix most issues (the other changes in this PR are still important though).

@patak-dev patak-dev added p4-important Violate documented behavior or significantly improves performance (priority) and removed p3-minor-bug An edge case that only affects very specific usage (priority) labels Apr 6, 2021
@patak-dev
Copy link
Member

@hannoeru reported that Vite + pnpm v6 has package resolution issues due to the use of # in paths. Bumping the priority of this PR.

@patak-dev
Copy link
Member

Great, at the end, they are going to replace # by + in pnpm, because webpack is also having issues. I'll leave the priority bump though because IMO it is still important that we get this in.

@benmccann
Copy link
Collaborator

It looks like this may possibly fix #2346 and #2118 in which case those could be added as linked issues

@benmccann
Copy link
Collaborator

Is there a test that can be added to prevent regressions?

@caticodev
Copy link

@knightly-bot build this

@knightly-bot
Copy link

Nightly Build

🌒 Knightly build enabled, release every night at 00:00 UTC (skip if no change)

📦 npm package

Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

@Eazymov
Copy link

Eazymov commented May 13, 2021

Any updates on this?)

@timvlaer
Copy link

I wonder if it isn't enough to start scanning for '#' after the last slash as @patak-js suggested, see timvlaer@8d94d5d Do I oversee any other scenario's that are covered by this PR?

I can open a new PR from my branch or @YufJi you can take it along in this branch. Same to me.

Changing the fromIndex fixes #2346 for me.


let postfixIndex = fsPath.indexOf('?')
if (postfixIndex < 0) {
postfixIndex = fsPath.indexOf('#')
Copy link
Contributor

@hyrious hyrious Jul 11, 2021

Choose a reason for hiding this comment

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

As #2346 (comment) said, can we just remove this line so that # are always treated as part of file path?

@yyx990803 yyx990803 changed the title fix: filePath with "#" is a direction fix: filePath with "#" is a directory Jul 23, 2021
@0xinhua
Copy link

0xinhua commented Aug 3, 2021

Same error while use vite, Any updates of this mr ?

@WesleyKapow
Copy link

🚫 Currently blocking migration from CRA to vite :'(

@bowencool
Copy link

Any news?

@Shinigami92
Copy link
Member

Any news?

@bowencool Yes! See #4703 @iheyunfei is working on it

@weixiao-huang
Copy link

Is there any update? Currently blocking migration from Webpack to vite

@fi3ework
Copy link
Member

fi3ework commented Oct 11, 2021

For anyone who encountered the # issue with es5-ext, here's my workaround. Ugly, but it works. 🤨

Add to Vite config resolve.alias. Maybe I'll continue to finish the PR after knowing all the context.

        {
          find: /^es5-ext\/(.*)?\/#\/(.*)$/,
          replacement: path.resolve(require.resolve('es5-ext'), '../$1/../#/$2'),
        },
        {
          find: /^.\/#$/,
          replacement: '../#/index.js',
        },

@patak-dev
Copy link
Member

Thanks for the work in this PR, which was used in part by #4703, closing as it is now merged

@patak-dev patak-dev closed this Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has workaround p4-important Violate documented behavior or significantly improves performance (priority)
Projects
None yet