-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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: stop running into null pathname errors #9036
fix: stop running into null pathname errors #9036
Conversation
replace url.parse (deprecated) with new URL
✅ Deploy Preview for vite-docs-main canceled.
|
The previous commit used wrong properties.
Excuse me for taking 3 commits to do this. I was a little dismissive and thought I could do it in GitHub UI only. Turns out doing it in a proper UI and running tests alongside would've been better :) |
@patak-dev @sapphi-red can you please have a look? |
I’m not sure what’s happening, but work is continuing on other PRs while this is a serious issue for anyone implementing things on top of vite and using newer Is anything unclear? |
I think it would be great if the fix would prevent existing code from crashing when changing minor Node.js version. |
IIUC Where is this |
It does. |
Thanks for taking a look! I've updated my project to
I pushed a repro repo (not very simplified unfortunately) here: https://github.com/wilsonjs/wilson2 Steps to reproduce:
What's important to remember: This works up until node Maybe the problem here is that |
Thanks for the detailed explanation. I was trying to understand the issue more accurately. |
const { pathname, search, hash } = parseUrl(url) | ||
const { pathname, search, hash } = new URL(url, 'relative://') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this changes the behavior of Line 243.
$ node -e "console.log(require('url').parse('\0virtual:aa'))" # node 16.14.1
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '\x00virtual:aa',
path: '\x00virtual:aa',
href: '\x00virtual:aa'
}
$ node -e "console.log(require('url').parse('\0virtual:aa'))" # node 16.16.0
Url {
protocol: 'virtual:',
slashes: null,
auth: null,
host: 'aa',
port: null,
hostname: 'aa',
hash: null,
search: null,
query: null,
pathname: null,
path: null,
href: 'virtual:aa'
}
$ node -e "console.log(new URL('\0virtual:aa', 'relative://'))" # node 16.16.0
URL {
href: 'virtual:aa',
origin: 'null',
protocol: 'virtual:',
username: '',
password: '',
host: '',
hostname: '',
port: '',
pathname: 'aa',
search: '',
searchParams: URLSearchParams {},
hash: ''
}
'\x00virtual:aa' // url.parse with node 16.14.1
'aa' // new URL with node 16.16.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it does. But it has a pathname now, while url.parse has null
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the pathname and thus the returned url
to be the same as before? As far as I've seen it's used as a key to urlToModuleMap
, which should work inside of ModuleGraph
.
However, it's also used in import-analysis plugin and urlToModuleMap
is read from in ssrStacktrace, which might be problematic. Tests don't seem to cover that, though - they're all green.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the pathname and thus the returned
url
to be the same as before?
I'm not familiar to code around here enough to say no. But I suppose it will break things when multiple urls got the same key for urlToModuleMap
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you known who could help? I’m surprised this is not a big issue to projects building with vite yet 🙈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll review it in the next days. We'll release 3.0.1 with current fixes to main tomorrow, and then review this issue to resolve it in 3.0.2. Thanks for digging into this @codepunkt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're welcome. Thanks to you, @sapphi-red and all other regular maintainers!
If there's anything I can do to help, let me know!
Replace
url.parse
(legacy) withnew URL
Description
Starting from node
16.15.0
(see nodejs/node#42196),url.parse
now trims leading and trailing C0 control characters. leading C0 control characters are recommended to be prefixed for resolved virtual module ids (see https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention).This means that if we use an URL like
\0virtual:mypackage-somedata
from a pluginsresolveId
method, pathname will be null and you will run into an error like this starting from node16.15.0
:This was also noted by @bjuriewicz in discussions: #8967
What is the purpose of this pull request?