swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.09k stars 1.22k forks source link

[Spack] Valid async/await code is making build crash on GitHub Actions #7329

Open Vadorequest opened 1 year ago

Vadorequest commented 1 year ago

Describe the bug

Video of the issue

watch.screencastify.com/v/0v4BQsUPP90OZf5iu8pQ

When compiling (using spack), it works on my local computer, but crashes on GitHub Actions. It is definitely related to async/await keywords.

My first attempt, mixing both promises and async/await crashed. My second attempt, using only async/await crashed. My latest attempt, using only promises (.then/.catch) worked.

The playground link will not be useful, I couldn't reproduce the issue, I couldn't use spack in playground.

Input code

fetch(zapierWebhookUrl, {
      method: 'POST',
      body: JSON.stringify(data),
    })
      .then(async (response) => {
        const result = await response.json();
        console.debug('[Custom script] Zapier result', result);

        if (result?.status === 'success') {
          // Store the fact we already synced the user in LS, to avoid performing too many invocations to Zapier (to reduce costs)
          localStorage.setItem(LS_KEY_SYNCED_REGISTERED_DATE, '1')
        } else {
          console.error(`[Custom script] Error occurred:`, result)
        }
      })
      .catch((error) => {
        throw new Error(error)
      })

Config

{
  "minify": true,
  "jsc": {
    "minify": {
      "compress": {
        "unused": true
      },
      "mangle": false
    },
    "target": "es5"
  }
}

Playground link

https://play.swc.rs/?version=1.2.142&code=H4sIAAAAAAAAA4WSW2vbQBCF3%2FUrhrxoDUKKSUOpjBOCLYLbEBfLpaQlOOvV6NKstGJ3lpCU%2FPeuLi5O%2BtB9kZid%2BeacwwrVGIIX3laov%2BO%2BVOrxm5YwB78kak0cRV3JhENHKFQ9FCLBSZTR9MPZ9PT808fozOJ5%2FSIjf%2BaJHplx4g7z2wN3NBaVIdSYLTlhDNag3rypBWOfUDpbZUPHZVhl3usAVBLDDPe2YP7PhTWkajBCVy3dw4JLWTUF%2FOglAimwrduOQCX2HDjJK23oRhUFZqumW7duUuLiEfVV255AXqHMAtfbYTrhsR%2F038nM83J0Rtn7hILRWY1UKqfX%2F7pOt%2F7gYq%2By5xg%2Bp%2Bvb0JB2zCp%2FZj0u8F4nfUvotDWMaTStM4cTmF%2F8jYqsbuBwE%2F4yqmFOR3f3z7CVdDT6n5zGfIYxZ3Ccdxa74SqHkXjpVHOyBuZz9wyMFQKN8Sfjku5IJbhMSWleYGiQVoQ1u0l3X5K7XXp3u0iWu01yvUq3ycb9Lq%2B2SQD%2B1D%2B4AJQGj3AH3ai10uzhve6kK4MSwmr3WOKHI%2BU97k00%2FbNkrEcdRUOlVk%2FQ4NNAG%2B8Pqc68P5QPjQcHAwAA&config=H4sIAAAAAAAAA1WPSw7DIAwF95wCed1tu%2BgdcghEnYiIn2wiFUW5e4FA2%2BzwvDFP3oWUsLKGp9zLswxRESN950I4%2B6TehQBqp1iTiQluI125RrOyjA0dZwJJ0YKpbfG962BDYBx6Z854M%2Bf%2FQh1cJGS%2BilVVfrF4rRO9Elx4bS3sh6Qc8ax%2FwE8aZYm29i8YnsZiZeL4ADoI3bUTAQAA

Expected behavior

Compile code.

Actual behavior

Error message

$ spack
node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: load_transformed failed

Caused by:
    0: failed to analyze module
    1: failed to resolve regenerator-runtime from /home/runner/work/MonCDI/MonCDI/src/apps/Espace Client/global-script.ts
    2: index not found] {
  code: 'GenericFailure'
}

Node.js v18.16.0

image

For some reason, the "Input code" (which is built fine using Spack on a local computer) crashes when built on GitHub Actions.

image

Version

1.2.142

Additional context

"@swc/cli": "0.1.55",
"@swc/core": "1.2.142",
gmanninglive commented 1 year ago

This looks related to #1678

I believe you could either add regenerator-runtime as a dependency or target >= ES2017

DNA-PC commented 1 year ago

Using es2017 fixed it:

{
  "minify": true,
  "jsc": {
    "minify": {
      "compress": {
        "unused": true
      },
      "mangle": false
    },
    "target": "es2017"
  }
}

But installing regenerator-runtime didn't fix anything. Still getting this after having it installed, using Yarn.

yarn build
yarn run v1.22.17
warning package.json: No license field
$ spack
thread '<unnamed>' panicked at 'internal error: entered unreachable code: module item found but is_es6 is false: Import(ImportDecl { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #567 }, specifiers: [Default(ImportDefaultSpecifier { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, local: Ident { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #360 }, sym: Atom('regeneratorRuntime' type=dynamic), optional: false } })], src: Str { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, value: Atom('regenerator-runtime' type=dynamic), has_escape: false, kind: Synthesized }, type_only: false, asserts: None })', crates/swc_bundler/src/bundler/chunk/cjs.rs:129:29
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: panic detected: internal error: entered unreachable code: module item found but is_es6 is false: Import(ImportDecl { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #567 }, specifiers: [Default(ImportDefaultSpecifier { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, local: Ident { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #360 }, sym: Atom('regeneratorRuntime' type=dynamic), optional: false } })], src: Str { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, value: Atom('regenerator-runtime' type=dynamic), has_escape: false, kind: Synthesized }, type_only: false, asserts: None })] {
  code: 'GenericFailure'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
DNA-PC commented 1 year ago

Ideally, I'd prefer to keep ES5 for better browser compatibility.

DNA-PC commented 1 year ago

By upgrading all my swc deps, the error is now gone when running spack.

image This somehow helped: https://github.com/swc-project/swc/issues/1461

I need to test this on my production now, and see how it behaves.

DNA-PC commented 1 year ago

When the script is compiled by GitHub Action, an error is thrown thread '<unnamed>' panicked at 'cannot access a scoped thread local variable without callingsetfirst', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/scoped-tls-1.0.1/src/lib.rs:168:9, but I don't know if it's related to that or to something else yet.

DNA-PC commented 1 year ago

Opened https://github.com/swc-project/swc/issues/8058

Also, trying to figure out if Turbo can replace my Spack use-case: https://github.com/vercel/turbo/discussions/2350