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: consider # as a valid dir symbol (fix #4701) #4703

Merged
merged 9 commits into from Oct 27, 2021
Merged

fix: consider # as a valid dir symbol (fix #4701) #4703

merged 9 commits into from Oct 27, 2021

Conversation

hyf0
Copy link
Contributor

@hyf0 hyf0 commented Aug 24, 2021

fix #4701.

Description

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@hyf0 hyf0 changed the title feat: consider # as a valid dir symbol fix(#4701). fix(vite): consider # as a valid dir symbol fix(#4701). Aug 24, 2021
@Shinigami92
Copy link
Member

It should fix #2346 🤔, does it?

@Shinigami92 Shinigami92 linked an issue Aug 24, 2021 that may be closed by this pull request
@hyf0
Copy link
Contributor Author

hyf0 commented Aug 24, 2021

@Shinigami92 Yes, It's the same problem. I also found related PR #2432 but It looks like a different approach to fix the problem. Should I close mine?

@patak-dev
Copy link
Member

Maybe you could fork #2432, and start from there? The PR is staled, it wasnt merged because of this requested change #2432 (comment)

@hyf0
Copy link
Contributor Author

hyf0 commented Aug 24, 2021

@patak-js I'm not sure the fix in #2432 is appropriate. After digging into codes, It seems that simply considering # as valid dir symbol fixes the problem.

@Shinigami92 Shinigami92 added the p4-important Violate documented behavior or significantly improves performance (priority) label Aug 24, 2021
@Shinigami92
Copy link
Member

Okay, sadly it looks like there are failing tests caused on these PR changes 🙁

@hyf0
Copy link
Contributor Author

hyf0 commented Aug 24, 2021

It looks like there are many edge cases for symbol #. There are situations that # could not consider a valid dir symbol.

if (isBuild) {
test('preserve postfix query/hash', () => {
expect(findAssetFile(/\.css$/, 'foo')).toMatch(`woff2?#iefix`)
})
}
})

I think the strategy @patak-js said is indeed a good strategy. I might still need to dig deep for a while.

@Shinigami92
Copy link
Member

It looks like there are many edge cases for symbol #. There are situations that # could not consider a valid dir symbol.

if (isBuild) {
test('preserve postfix query/hash', () => {
expect(findAssetFile(/\.css$/, 'foo')).toMatch(`woff2?#iefix`)
})
}
})

I think the strategy @patak-js said is indeed a good strategy. I might still need to dig deep for a while.

Thank you for tackling this issue
I think it is not allowed in query parameters, but in path 🤔
Could you try to implement a check for this?

@hyrious
Copy link
Contributor

hyrious commented Aug 24, 2021

I think it is not allowed in query parameters, but in path 🤔

Does it mean # is maybe a dir symbol only if it is before ??

//a.com/b#c?d      /b#c may be a valid file (so we have 2 fs tests here)
//a.com/b?c#d      only /b has to be processed

Also, consider this:

//a.com/#c/d#/#/#/f#g

Assume we have to check from the right side, wow...

/#c/d#/#/#/f#g
/#c/d#/#/#/f
/#c/d#/#/(index)
/#c/d#/(index)
/#c/d
/(index)

Here we must make sure that trailing slash is necessary for referencing /index,
otherwice the list above will be longer.

From the left side, it is obviously wrong, take /ext/#/abc for example,

/ext/(index)      maybe exist!, but /ext/#/abc should be considered first

@Shinigami92
Copy link
Member

I think it is not allowed in query parameters, but in path 🤔

Does it means # is maybe a dir symbol only if it is before ??

//a.com/b#c?d      /b#c may be a valid file (so we have 2 fs tests here)
//a.com/b?c#d      only /b has to be processed

Also, consider this:

//a.com/#c/d#/#/#/f#g

I assume yes, but could be wrong

@hyf0
Copy link
Contributor Author

hyf0 commented Aug 25, 2021

Kind of busy these days :(, I will look deeper this weekend.

@hyf0
Copy link
Contributor Author

hyf0 commented Aug 29, 2021

It's harder than I think :(. I'm not sure there is a way to pre-determine whether # is a dir symbol or a special query unless we actually try to resolve it.

I found the reason tests fail is because of processing CSS files.

.icon-clock {
background: url(../nested/fragment-bg.svg#icon-clock-view) no-repeat;
}
.icon-heart {
background: url(../nested/fragment-bg.svg#icon-heart-view) no-repeat;
}
.icon-arrow-right {
background: url(../nested/fragment-bg.svg#icon-arrow-right-view) no-repeat;

Simply considering # as a valid dir symbol will make vite could not find the correct SVG file imported from css, but <img /> is ok 🤔.
<img
class="svg-frag-img"
src="./nested/fragment.svg#icon-clock-view"
alt=""
/>

@bowencool
Copy link

Is there any update?

@hyf0
Copy link
Contributor Author

hyf0 commented Oct 20, 2021

The solution is an implementation of #2432 (comment). It should work, I just need to refactor it for better maintenance, then it should be ok to be reviewed.

@@ -10,7 +10,8 @@ export function loadFallbackPlugin(): Plugin {
name: 'vite:load-fallback',
async load(id) {
try {
return fs.readFile(cleanUrl(id), 'utf-8')
// if we don't add `await` here, we couldn't catch the error in readFile
Copy link
Contributor Author

@hyf0 hyf0 Oct 20, 2021

Choose a reason for hiding this comment

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

So, is this plugin never working 🤣 ?

@hyf0 hyf0 marked this pull request as ready for review October 21, 2021 02:16
@hyf0
Copy link
Contributor Author

hyf0 commented Oct 21, 2021

@Shinigami92 @patak-js Hi, the code should be ok to fix the issue. Sorry for pending so long.

patak-dev
patak-dev previously approved these changes Oct 22, 2021
Shinigami92
Shinigami92 previously approved these changes Oct 22, 2021
packages/vite/src/node/plugins/resolve.ts Outdated Show resolved Hide resolved
@hyf0 hyf0 dismissed stale reviews from Shinigami92 and patak-dev via be2d8fe October 22, 2021 10:30
@patak-dev
Copy link
Member

Let's merge this one once we kick the beta for 2.7 so we get some extra testing

@patak-dev patak-dev added this to the 2.7 milestone Oct 22, 2021
@patak-dev
Copy link
Member

For reference, this only solves part of the issues with # and ? in paths. But after discussing with @iheyunfei we are not sure if a more complete solution is desired here, or we should push for the ecosystem to stop using these characters in their path names.

@patak-dev patak-dev changed the title fix(vite): consider # as a valid dir symbol fix(#4701). fix: consider # as a valid dir symbol (fix #4701) Oct 27, 2021
@patak-dev patak-dev merged commit 52689c8 into vitejs:main Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-important Violate documented behavior or significantly improves performance (priority)
Projects
None yet
5 participants