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

panic: Expected scope, found scope while parsing #4088

Open
dloebl opened this issue Feb 20, 2025 · 2 comments
Open

panic: Expected scope, found scope while parsing #4088

dloebl opened this issue Feb 20, 2025 · 2 comments

Comments

@dloebl
Copy link

dloebl commented Feb 20, 2025

While updating to esbuild v0.25.0, we noticed that some inputs cause esbuild to panic. I've tried my best to minify the input JS, but it's still a rather large reproducer.

Input:
min_js.zip

Steps:

$ esbuild min.js

✘ [ERROR] panic: Expected scope (7, 186) in min.js, found scope (7, 165) (while parsing "min.js")

  debug.Stack (runtime/debug/stack.go:26)
  helpers.PrettyPrintedStack (internal/helpers/stack.go:9)
  bundler.parseFile.func1 (internal/bundler/bundler.go:254)
  panic (runtime/panic.go:785)
  js_parser.Parse.func1 (internal/js_parser/js_parser.go:17334)
  panic (runtime/panic.go:785)
  js_parser.(*parser).pushScopeForVisitPass (internal/js_parser/js_parser.go:8421)
  js_parser.(*parser).visitFn (internal/js_parser/js_parser.go:16735)
  js_parser.(*parser).visitExprInOut (internal/js_parser/js_parser.go:15432)
  js_parser.(*parser).visitExpr (internal/js_parser/js_parser.go:12819)
[..]

1 error

Version: 0.25.0

@evanw
Copy link
Owner

evanw commented Feb 20, 2025

Huh, that's some crazy code. Here's the code for ease of access:

(function(){[(function(){})]|function(){for([(function(){})];;)e}(((/)/))=function(){for([({})];;)[]}((function(){((([(f)])))}))=function(){switch(((L))){case(u):([(function(){})]);case(function(){}):((("")))}(function(){})}()[(f)])[(f)]&function(){for(;([(f)])[(f)];)[(i)]}(function(){return{n(){for(;[(0)];){((([])))}}}[(c)](function(){try{[];if(t){}}catch{}}) try{}catch{}if(([({})])){(([]))}try{for([(f)];;){}}catch{}}[({})]);try{}catch{}for(;;([(i)]=function(){([(f)]=t)}));})

I'm guessing this is related to some unintentional scope manipulation in the new switch optimizations.

Edit: Here's a minimal reproduction:

switch (0) {
  case 1: x = function() {}
  case function() {}: break
}

Perhaps related to function expressions inside case statements, which isn't something I've ever seen real code do.

@goliafrs
Copy link

goliafrs commented Feb 26, 2025

After updating to Vite version 6.2.0, which includes an upgrade to esbuild version 0.25.0, I encountered the following error during the build process:

[plugin:vite:esbuild] Transform failed with 1 error:
error: panic: Expected scope (7, 1596) in /var/client-side/src/plugins/router.ts, found scope (0, 908) (while parsing "/var/client-side/src/plugins/router.ts")

Environment:
Build Tool: Vite 6.2.0
Package Manager: pnpm
Dependencies:

  • vite: ^6.2.0
  • vue: ^3.5.13
  • vue-router: ^4.5.0
  • @vitejs/plugin-vue: ^5.2.1
  • @vitejs/plugin-vue-jsx: ^4.1.1
  • typescript: 5.7.3

The error appears to be related to a switch-case statement within the router.ts file. Here's the relevant portion of the code:

router.beforeEach(async (to, from, next) => {
  // ... other code

  switch (true) {
    case to.matched.some(({ meta }) => meta.layout === 'authorized'):
      // ... handling
      break;
    // ... other cases
    default:
      next();
  }

  // ... other code
});

I've been using similar code for several years, and such constructs are common in my codebase.

For those seeking a quick temporary solution to this issue, downgrading Vite to version 6.1.x may help, as it uses an earlier version of esbuild.

pnpm add --save-dev vite@6.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants