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: runtime error: index out of range [7] with length 5 when building esm #3400

Closed
wilhen01 opened this issue Sep 21, 2023 · 6 comments
Closed

Comments

@wilhen01
Copy link

I'm trying to build a typescript cli tool using esbuild 0.19.3 with esm format using the following options:

const options: BuildOptions = {
  entryPoints: ['bin/aumi-cli.ts'],
  outdir: 'dist',
  platform: 'node',
  target: 'esnext',
  format: 'esm',
  bundle: true,
  minify: production && !debug,
  define: {
    'process.env.DEV': debug ? '"true"' : '"false"',
    'process.env.NODE_ENV': debug ? '"dev"' : '"production"',
  },
};

type is set as module in package.json.

tsconfig options as follows:

{
  "compilerOptions": {
    "lib": ["es2021"],
    "module": "ESNext",
    "moduleResolution": "bundler",
    "target": "es2019",

    // React support
    "jsx": "react",
    "esModuleInterop": true,

    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": false,
    "noImplicitAny": false,

    "noEmit": true
  },
  "include": ["**/*.ts", "**/*.tsx"]
}

Receiving the following error from esbuild

panic: runtime error: index out of range [7] with length 5

goroutine 20 [running]:
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x14000063b00, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2522 +0x948
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x14000063b00, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x14000063b00, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x14000063b00, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x14000063b00, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x14000063b00, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x14000063b00, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x14000063b00, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x14000063b00, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x14000063b00, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x14000063b00, 0x20d660?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).processScannedFiles(0x14000063b00, {0x1400001fef0, 0x1, 0x1400024c000?})
        github.com/evanw/esbuild/internal/bundler/bundler.go:2462 +0xeb8
github.com/evanw/esbuild/internal/bundler.ScanBundle(_, {_, _, _, _, _, _}, {_, _}, _, ...)
        github.com/evanw/esbuild/internal/bundler/bundler.go:1310 +0x90c
github.com/evanw/esbuild/pkg/api.rebuildImpl({0x140000780c0, {0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}, {0x0, 0x1, 0x2, ...}, ...}, ...)
        github.com/evanw/esbuild/pkg/api/api_impl.go:1511 +0x22c
github.com/evanw/esbuild/pkg/api.(*internalContext).rebuild(_)
        github.com/evanw/esbuild/pkg/api/api_impl.go:1031 +0x2dc
github.com/evanw/esbuild/pkg/api.(*internalContext).Rebuild(...)
        github.com/evanw/esbuild/pkg/api/api_impl.go:1092
github.com/evanw/esbuild/pkg/api.Build({0x2, 0x4, 0x0, 0x1400001e0f0, 0x0, {0x0, 0x0}, 0x0, 0x1, {0x0, ...}, ...})
        github.com/evanw/esbuild/pkg/api/api.go:384 +0x74
main.(*serviceType).handleBuildRequest(0x140000a1f50, 0x0?, 0x1400013a000?)
        github.com/evanw/esbuild/cmd/esbuild/service.go:763 +0xc90
main.(*serviceType).handleIncomingPacket.func2()
        github.com/evanw/esbuild/cmd/esbuild/service.go:235 +0x5c
created by main.(*serviceType).handleIncomingPacket
        github.com/evanw/esbuild/cmd/esbuild/service.go:233 +0x1b8
/Users/wilsoh58/Projects/sounds-offproduct/tools/aumi-cli/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:2313
      throw reject;
      ^

Error: The service was stopped
@wilhen01
Copy link
Author

I suspect you'll need a full repro, which I can probably pull together and put into Github at some point this week 👍 Figured I'd raise this first though in case there's something obvious I'm doing wrong with the config...

@wilhen01
Copy link
Author

wilhen01 commented Sep 29, 2023

I've commited a reproduction here: https://github.com/wilhen01/esbuild-bug-repro

I think I may have figured out what's going on - I set format: 'esm' under build.ts, but the build commands in the package.json are still specifying --format=cjs which seems to cause the error. There's maybe some validation required so that only one format specifier is set?

If I change the build command as follows: "build": "esbuild build.ts --format=esm | NODE_ENV=production node", and run it, I get a different error:

❯ pnpm build

> [email protected] build /Users/wilsoh58/Projects/esbuild-bug-repro
> esbuild build.ts --format=esm | NODE_ENV=production node

[stdin]:1
import { buildSync } from "esbuild";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at new Script (node:vm:94:7)
    at createScript (node:vm:251:10)
    at Object.runInThisContext (node:vm:299:10)
    at node:internal/process/execution:79:19
    at [stdin]-wrapper:6:22
    at evalScript (node:internal/process/execution:78:60)
    at node:internal/main/eval_stdin:30:5
    at Socket.<anonymous> (node:internal/process/execution:195:5)
    at Socket.emit (node:events:526:35)
    at endReadableNT (node:internal/streams/readable:1359:12)

Node.js v18.17.0
 ELIFECYCLE  Command failed with exit code 1.

I'm setting "type": "module" in package.json so I don't understand why this is happening. Any ideas @evanw ?

@hyrious
Copy link

hyrious commented Sep 29, 2023

I'm setting "type": "module" in package.json so I don't understand why this is happening.

It's not related to the panic but you only have to use node --input-type=module to tell Node.js that code from stdin is in ESM format.

@wilhen01
Copy link
Author

Thanks @hyrious! Setting the build command to esbuild build.ts --format=esm | NODE_ENV=production node --input-type=module brings me back around to the same panic error...

❯ pnpm build

> [email protected] build /Users/wilsoh58/Projects/esbuild-bug-repro
> esbuild build.ts --format=esm | NODE_ENV=production node --input-type=module

Building for DEV (DEBUG: off) with following options:
{
  entryPoints: [ 'bin/aumi-cli.ts' ],
  outdir: 'dist',
  platform: 'node',
  target: 'esnext',
  format: 'esm',
  bundle: true,
  minify: false,
  define: {
    'process.env.DEV': '"false"',
    'process.env.NODE_ENV': '"production"'
  }
}
panic: runtime error: index out of range [7] with length 5

goroutine 20 [running]:
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x140001c2d80, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2522 +0x948
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x140001c2d80, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x140001c2d80, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x140001c2d80, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x140001c2d80, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x140001c2d80, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x140001c2d80, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x140001c2d80, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x140001c2d80, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x140001c2d80, 0x0?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).validateTLA(0x140001c2d80, 0x1ab660?)
        github.com/evanw/esbuild/internal/bundler/bundler.go:2481 +0x118
github.com/evanw/esbuild/internal/bundler.(*scanner).processScannedFiles(0x140001c2d80, {0x14000187ec0, 0x1, 0x140001fa000?})
        github.com/evanw/esbuild/internal/bundler/bundler.go:2462 +0xeb8
github.com/evanw/esbuild/internal/bundler.ScanBundle(_, {_, _, _, _, _, _}, {_, _}, _, ...)
        github.com/evanw/esbuild/internal/bundler/bundler.go:1310 +0x90c
github.com/evanw/esbuild/pkg/api.rebuildImpl({0x14000180060, {0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}, {0x0, 0x1, 0x2, ...}, ...}, ...)
        github.com/evanw/esbuild/pkg/api/api_impl.go:1498 +0x22c
github.com/evanw/esbuild/pkg/api.(*internalContext).rebuild(_)
        github.com/evanw/esbuild/pkg/api/api_impl.go:1018 +0x2dc
github.com/evanw/esbuild/pkg/api.(*internalContext).Rebuild(...)
        github.com/evanw/esbuild/pkg/api/api_impl.go:1079
github.com/evanw/esbuild/pkg/api.Build({0x2, 0x4, 0x0, 0x140001860c0, 0x0, {0x0, 0x0}, 0x0, 0x1, {0x0, ...}, ...})
        github.com/evanw/esbuild/pkg/api/api.go:384 +0x74
main.(*serviceType).handleBuildRequest(0x140000a1ef0, 0x0?, 0x140000a1f80?)
        github.com/evanw/esbuild/cmd/esbuild/service.go:763 +0xc90
main.(*serviceType).handleIncomingPacket.func2()
        github.com/evanw/esbuild/cmd/esbuild/service.go:235 +0x5c
created by main.(*serviceType).handleIncomingPacket
        github.com/evanw/esbuild/cmd/esbuild/service.go:233 +0x1b8
node:internal/process/esm_loader:97
    internalBinding('errors').triggerUncaughtException(
                              ^

Error: The service was stopped
    at /Users/wilsoh58/Projects/esbuild-bug-repro/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1084:25
    at responseCallbacks.<computed> (/Users/wilsoh58/Projects/esbuild-bug-repro/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:703:9)
    at Socket.afterClose (/Users/wilsoh58/Projects/esbuild-bug-repro/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:693:28)
    at Socket.emit (node:events:526:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

Node.js v18.17.0
 ELIFECYCLE  Command failed with exit code 1.
 ```

@evanw
Copy link
Owner

evanw commented Oct 17, 2023

Thanks for providing this test case. This is a crash in esbuild's error reporting for the case where something uses require() on a file containing top-level await, which is nonsensical and is not allowed. The error is supposed to look like this:

✘ [ERROR] This require call is not allowed because the transitive dependency "node_modules/ink/build/reconciler.js" contains a top-level await

    node_modules/ink-gradient/dist.js:5:19:
      5 │ var _ink = require("ink");
        ╵                    ~~~~~

  The file "node_modules/ink/build/index.js" imports the file
  "node_modules/ink/build/hooks/use-input.js" here:

    node_modules/ink/build/index.js:8:36:
      8 │ export { default as useInput } from './hooks/use-input.js';
        ╵                                     ~~~~~~~~~~~~~~~~~~~~~~

  The file "node_modules/ink/build/hooks/use-input.js" imports the file
  "node_modules/ink/build/reconciler.js" here:

    node_modules/ink/build/hooks/use-input.js:4:23:
      4 │ import reconciler from '../reconciler.js';
        ╵                        ~~~~~~~~~~~~~~~~~~

  The top-level await in "node_modules/ink/build/reconciler.js" is here:

    node_modules/ink/build/reconciler.js:13:8:
      13 │         await import('./devtools.js');
         ╵         ~~~~~

The crash will be fixed in the next release of esbuild. In the meantime, you should fix your code to not call require() on a file containing a top-level await. In this case the solution may be to upgrade ink-gradient. I notice that ink-gradient@2 is CommonJS (which uses require()) but ink-gradient@3 is ESM (which uses import).

@evanw evanw closed this as completed in b0eddea Oct 17, 2023
@wilhen01
Copy link
Author

Thanks @evanw ! Will give the next version a try and see about resolving the issue with ink-gradient 👍

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