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

vite:dep-scan transformations can cause syntax errors confusingly reported by esbuild #2528

Closed
tjk opened this issue Mar 16, 2021 · 7 comments

Comments

@tjk
Copy link
Contributor

tjk commented Mar 16, 2021

For example, if you write the code (notice await in a comment):

// something something await
watch(() => result.data.value, data => {
}, { immediate: true })

The code here:

if (js.includes('await')) {
js = js.replace(/\bawait\s/g, 'void ')
}

will produce the following code to get sent to esbuild:

// something something void       watch(() => result.data.value, data => {
}, { immediate: true })

User will just see this error:

 > /component.vue:53:9: error: Expected identifier but found "{"
    53 │       }, { immediate: true })
       ╵          ^

error during build:
Error: Build failed with 1 error:
html:/component.vue:53:9: error: Expected identifier but found "{"

Does a proper parse need to happen to not secretly cause these issues or would a naive 'in comment' or 'in string' heuristic be good enough? At the very least it would maybe be nice to see what the code esbuild errored on but that seems non-trivial given it is just provided entries and then communicates back progress asynchronously?

@yyx990803
Copy link
Member

It would be ideal to avoid incurring the cost of full AST parses here. It seems this edge case is easy to fix though.

@tjk
Copy link
Contributor Author

tjk commented Mar 16, 2021

That's a nice (and simple) fix. Thanks!

@rangoy
Copy link

rangoy commented May 26, 2021

It seems like this problem also applies to the use of for await loops to;

    for await (const x of y) {
        console.log(x);
    }

In this case await is still replaced with void and is causing an syntax error

 error: Expected "(" but found "void"
     │     for void (const x of y) {

@Niputi
Copy link
Contributor

Niputi commented May 26, 2021

@rangoy are you experiencing this error in development? you might need to force esbuild to higher version as it seems there have been a for loop code generation fix in this version https://github.com/evanw/esbuild/releases/tag/v0.12.2

@rangoy
Copy link

rangoy commented May 27, 2021

@Niputi, I'm experiencing this in development when running yarn dev.

I haven't traced the to the core but what I see is that it is related to the same replace;

I can use one of the following workarounds to get it working now;

  1. If i remove the space after await it works after the new change.
    for await(const x of y) {} instead of for await (const x of y) {}
    I think this works because the new version of the regex only matches when there's a space after await.

  2. Move the for await loops out of <script setup> to it's own ts modules

  3. Comment out the sections with for await during startup, and they work as they should if I put them back with the server running also works. After this it seems like the replace isn't done anymore and I can restart the server. If I delete node_modules/.vite the problem reappears and I have to use one of the workarounds get it working again

@rangoy
Copy link

rangoy commented May 27, 2021

I'm not sure if I get the reason why these replaces has to be done but I guess adding a new replace before the one changed in cbfc3e9 could solve the issue

Something like (untested)

js = js.replace(/\bfor\s+await(\s)/g, 'for$1')

@github-actions
Copy link

This issue has been locked since it has been closed for more than 14 days.

If you have found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Vite version. If you have any other comments you should join the chat at Vite Land or create a new discussion.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants