-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
Code matching the import regex breaks vite #4027
Comments
Close at #4054 |
Thanks for the fast fix. E.g. the following code still crashes: <script lang="ts">
/*
import this from "non-existant";
*/
</script> Error message: yarn run v1.22.10
warning package.json: No license field
$ vite
error when starting dev server:
Error: The following dependencies are imported but could not be resolved:
non-existant (imported by /home/sebi/tmp/viterepro/vite-project/src/App.svelte)
Are they installed?
at optimizeDeps (/home/sebi/Downloads/vite/packages/vite/src/node/optimizer/index.ts:170:11)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at runOptimize (/home/sebi/Downloads/vite/packages/vite/src/node/server/index.ts:542:40)
at Server.httpServer.listen (/home/sebi/Downloads/vite/packages/vite/src/node/server/index.ts:556:9)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. |
I’m not sure if that fixes everything :) <script lang="ts">
//; import this from "non-existant";
</script> |
Hi, I’d like to note that regular expressions—which is more or less what regex is—are only able to parse some things, but code is too complex to be parsed correctly with regular expressions (to be exact, context free languages that are not just regular languages cannot be parsed via regular expressions). In practice, that means, no matter how much the regex to detect imports is tuned, it should always be possible to find an example of valid typescript code where an import is incorrectly detected and the vite dev server subsequently fails to compile the code. To give an example: <script lang="ts">
`
import this from "non-existant";
`
</script> Note that these multiline strings can be nested indefinitely, so trying to count <script lang="ts">
`
${`
import a from "b";
`}
`
</script> A typescript parser is the only tool that will be correct in all cases. |
I'm aware. Thanks for taking the time to write this down and push for Vite to be correct here. Check out this comment about why this is implemented at this point in this way
The changes that we just added thanks to your reports should reduce the number of false positives that look more problematic than false negatives. I don't think we are going to see real cases of the two examples you listed above. If they do, for the first one, we could see if the error we can filter these false positives with some extra checks (maybe checking the path before adding the import?). The second one shouldn't be an issue. But this may be something that we may be able to better fix as you said removing the regex with a plugin or config later in esbuild. If someone finds a better way, that will be good. |
Describe the bug
Vite contains a heuristic to detect import sources:
vite/packages/vite/src/node/optimizer/scan.ts
Lines 32 to 41 in da2a41d
This works most of the time. However, if this regex matches something that is not actually an import, it breaks*.
*: There are some conditions for it to actually break:
node_modules/.vite
is emptyvite
, usingvite build
does not break)The following svelte code matches the regex and fails to build:
Reproduction
System Info
Binaries: Node: 14.17.1 - /run/current-system/sw/bin/node Yarn: 1.22.10 - /run/current-system/sw/bin/yarn npm: 6.14.13 - /run/current-system/sw/bin/npm npmPackages: vite: ^2.3.8 => 2.3.8
Used Package Manager
yarn
Logs
Validations
The text was updated successfully, but these errors were encountered: