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

stripLiteral for import.meta processing may fail while processing large literal (~10MB) #15759

Closed
7 tasks done
mnixry opened this issue Jan 31, 2024 · 2 comments
Closed
7 tasks done
Labels
duplicate This issue or pull request already exists

Comments

@mnixry
Copy link

mnixry commented Jan 31, 2024

Describe the bug

Related: #8054

In the asset-import-meta-url plugin, I'm utilizing strip-literal to sanitize input code.

let s: MagicString | undefined
const assetImportMetaUrlRE =
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/dg
const cleanString = stripLiteral(code)
let match: RegExpExecArray | null
while ((match = assetImportMetaUrlRE.exec(cleanString))) {

However, I've identified a potential issue with this approach. The implementation of jsToken, which strip-literal depends on, is based on regular expressions and may encounter a recursion size exceeded error if it comes across large string literals. This seems to be a problem originating from V8.

There is currently an open issue and a corresponding PR in the upstream repository aiming to provide a workaround for this issue: lydell/js-tokens#43. However, this solution is yet to be merged and appears unstable, potentially still failing in certain edge cases.

As a potential solution, I'm proposing to handle this exception and, in the event of a stripping failure, use the input string as it was originally. I believe this could effectively mitigate the issue until a more stable solution is implemented in the jsToken library. I would appreciate your thoughts and suggestions on this proposal.

Reproduction

https://stackblitz.com/edit/vitejs-vite-cruckz?file=main.js

Steps to reproduce

Write a JS in this form:

export const a = new URL('some.js', import.meta.url) // trigger import-meta-url plugin
export const b = "aaaaa[...repeat ~10 milion chars...]aaaa"

Import this file in Vite managed site file, it will fail with RangeError.

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.14.0 - /usr/local/bin/pnpm
npmPackages:
    vite: ^5.0.12 => 5.0.12

Used Package Manager

npm

Logs

 vite:time 1.10ms /counter.js +3ms
  vite:load 25.74ms [fs] /node_modules/vite/dist/client/env.mjs +169ms
  vite:import-analysis 0.06ms [no imports] node_modules/vite/dist/client/env.mjs +31ms
  vite:transform 0.74ms /node_modules/vite/dist/client/env.mjs +31ms
  vite:time 2.90ms /node_modules/vite/dist/client/env.mjs +9ms
  vite:load 28.46ms [fs] /test.js +31ms
13:56:42 [vite] Internal server error: too much recursion
  Plugin: vite:asset-import-meta-url
  File: /home/projects/vitejs-vite-cruckz/test.js

Validations

Copy link

stackblitz bot commented Jan 31, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@XiSenao
Copy link
Collaborator

XiSenao commented Jan 31, 2024

It seems that @sapphi-red has already been tracking this issue, Related issue.

@XiSenao XiSenao added duplicate This issue or pull request already exists and removed pending triage labels Jan 31, 2024
@XiSenao XiSenao closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants