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

[@swc/core@1.3.7 + ] index out of bounds: the len is 1 but the index is 1 #6255

Closed
mizi-lin opened this issue Oct 26, 2022 · 33 comments
Closed
Labels

Comments

@mizi-lin
Copy link

Describe the bug

thread '<unnamed>' panicked at 'index out of bounds: the len is 1 but the index is 1', /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/swc_common-0.29.6/src/syntax_pos/hygiene.rs

env: typescript v4.8.4

in @swc/core v1.3.6 ok

Input code

// 
class HuluCssModules extends Visitor {
  visitImportDeclaration(expression) {
     return expression;
  }      
}

exports.default = HuluCssModules;


### Config

```json
{
            test: /\.(js|mjs|jsx|ts|tsx)$/,
            exclude: /node_modules/,
            use: {
                loader: require.resolve('swc-loader'),
                options: {
                    parseMap: true,
                    sync: true,
                    jsc: {
                        parser: {
                            syntax: 'typescript',
                            tsx: true
                        },
                        transform: {
                            react: {
                                runtime: 'automatic',
                                throwIfNamespace: true,
                                development: devMode,
                                useBuiltins: false,
                                refresh: true
                            }
                        }
                    },
                    plugin: (m) => new HuluCssModules().visitProgram(m)
                }
            }
        }


### Playground link

_No response_

### Expected behavior

compile no error

### Actual behavior

_No response_

### Version

v1.3.10

### Additional context

_No response_
@mizi-lin mizi-lin added the C-bug label Oct 26, 2022
@longlho
Copy link

longlho commented Oct 26, 2022

I'm also seeing this in our formatjs swc plugin

@kdy1
Copy link
Member

kdy1 commented Oct 26, 2022

@longlho Can you provide a repro case?

@kdy1 kdy1 added this to the need milestone Oct 26, 2022
@longlho
Copy link

longlho commented Oct 26, 2022

It's a little hard to isolate it because our plugin does a bunch of transformations, but specifically this test fail:
Test: https://github.com/formatjs/formatjs/blob/main/packages/swc-plugin/tests/issue-3362.test.ts
Fixture: https://github.com/formatjs/formatjs/blob/main/packages/swc-plugin/tests/fixtures/issue-3362.tsx
The dep upgrade bot put up the PR: formatjs/formatjs#3846 which failed our build
Build log: https://app.buildbuddy.io/invocation/5bac68ac-7edb-48a2-8018-b8a6bb32faf1

In the fixture our plugin basically tries to find all intl.formatMessage and transform the params (removing/adding some attributes). In the test specifically it's mainly a no-op since it just makes sure our plugin doesn't bomb swc. The result snapshot is here: https://github.com/formatjs/formatjs/blob/main/packages/swc-plugin/tests/__snapshots__/issue-3362.test.ts.snap (kinda looks exactly the same as the fixture except for the downtranspilation piece).

When I run with RUST_BACKTRACE=full the stack trace doesn't look very useful:

thread '<unnamed>' panicked at 'index out of bounds: the len is 1 but the index is 1', /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/swc_common-0.29.10/src/syntax_pos/hygiene.rs:580:34
stack backtrace:
   0:        0x11fd42cd7 - _napi_register_module_v1
   1:        0x11fa1621a - <unknown>
   2:        0x11fd1639c - _napi_register_module_v1
   3:        0x11fd4370b - _napi_register_module_v1
   4:        0x11fd43f52 - _napi_register_module_v1
   5:        0x11fd43a44 - _napi_register_module_v1
   6:        0x11fd439a8 - _napi_register_module_v1
   7:        0x11fd43972 - _napi_register_module_v1
   8:        0x12104fa03 - _wasmer_vm_raise_trap
   9:        0x12104fa76 - _wasmer_vm_raise_trap
  10:        0x12071e7a6 - _napi_register_module_v1
  11:        0x1206b293b - _napi_register_module_v1
  12:        0x12071994c - _napi_register_module_v1
  13:        0x11fecb93b - _napi_register_module_v1
  14:        0x11fe788de - _napi_register_module_v1
  15:        0x11f8cc732 - <unknown>
  16:        0x10f7da46a - _worker
  17:     0x7ff80f16d4e1 - __pthread_start
 FAIL  packages/swc-plugin/tests/issue-3362.test.ts
  ✕ github issue #3362 (30 ms)

  ● github issue #3362

    failed to handle: index out of bounds: the len is 1 but the index is 1

      Stack backtrace:
         0: _napi_register_module_v1
         1: _wasmer_vm_raise_trap
         2: <unknown>
         3: _worker
         4: __pthread_start

Hope that helps. Also this is the only test that failed and the other tests seem ok, so this might be some edge case.

The code snippet that @mizi-lin posted seems a lot simpler and also crapped out?

@kdy1 kdy1 self-assigned this Oct 26, 2022
@kdy1 kdy1 modified the milestones: need, Planned Oct 26, 2022
@aelbore
Copy link

aelbore commented Oct 27, 2022

I got this error also

@kdy1
Copy link
Member

kdy1 commented Oct 27, 2022

Does it work on v1.3.5 and v1.3.4?

@kdy1
Copy link
Member

kdy1 commented Oct 27, 2022

@IWANABETHATGUY I think #6019 is the cause, thoughts?
There's no PR touches it other than #6019

@kdy1
Copy link
Member

kdy1 commented Oct 27, 2022

@aelbore Can you try 1.3.4 and 1.3.5?

@aelbore
Copy link

aelbore commented Oct 27, 2022

@kdy1 works on <=1.3.6
heres the minimal reproduction

import { transformSync, plugins } from '@swc/core'
import { Visitor } from '@swc/core/Visitor.js'

const content = `
  const store = () => {
  
  }

  export default store;
`

class MyPlugin extends Visitor {
  visitModule(e) {
    return e;
  }
}

const myPlugin = () => {
  return (p) => new MyPlugin().visitProgram(p)
}

const { code } = transformSync(content, {
  filename: 'abc.ts',
  jsc: {
    parser: {
      syntax: 'typescript',
      tsx: true
    }
  },
  plugin: plugins([
    myPlugin()
  ])
})

console.log(code)

@aelbore
Copy link

aelbore commented Oct 27, 2022

if you change the content to this

const content = `
  export default () => {
  
  } 
 `

it works

@kdy1
Copy link
Member

kdy1 commented Oct 27, 2022

So #6118 is the cause.
But seems like @longlho has a different issue from this.
@longlho Can you try v1.3.6?

@IWANABETHATGUY
Copy link
Contributor

I expected this will not cause issues, i will try to revert the code and see if we could fix this issue.

@kdy1
Copy link
Member

kdy1 commented Oct 27, 2022

Oh, nevermind. Your plugin was based on js, so it was an identical issue.
@IWANABETHATGUY That was not the cause, sorry

@kdy1 kdy1 removed this from the Planned milestone Oct 27, 2022
@kdy1 kdy1 removed their assignment Oct 27, 2022
@IWANABETHATGUY
Copy link
Contributor

Oh, nevermind. Your plugin was based on js, so it was an identical issue. @IWANABETHATGUY That was not the cause, sorry

That's fine, hhhh

@aelbore
Copy link

aelbore commented Oct 27, 2022

another thing, empty plugin cause error also

plugin: plugins([])

@kdy1
Copy link
Member

kdy1 commented Oct 27, 2022

For formatjs, please use https://github.com/kwonoj/swc-plugin-formatjs instead

@aelbore
Copy link

aelbore commented Nov 12, 2022

Just want to ask if theres any plan to fix this? though version <=1.3.6 works Many thanks

@longlho
Copy link

longlho commented Nov 12, 2022

For formatjs, please use kwonoj/swc-plugin-formatjs instead

that's not maintained but us btw

@kdy1
Copy link
Member

kdy1 commented Nov 12, 2022

@aelbore Not sure. Fix would be trivial but I don't want to use my time for this

@aelbore
Copy link

aelbore commented Nov 12, 2022

Thanks no worries for now i will use version <= 1.3.6 👍

@aelbore
Copy link

aelbore commented Nov 30, 2022

Just to add also

this cause the error

import {  display  } from './display'


const randomId = () => { 
  return display()
}

export { display }
export { randomId }

Solution

import {  display  } from './display'

export const randomId = () => { 
  return display()
}

export { display } from './display' 

@kdy1
Copy link
Member

kdy1 commented Nov 30, 2022

I'm not going to fix this by myself as fixing this encourages wrong usage

@kdy1
Copy link
Member

kdy1 commented Nov 30, 2022

I didn't close it because I would accept PR

@bxt
Copy link

bxt commented Dec 5, 2022

So what is actually the issue here? I'm seeing the "index out of bounds" in my build and came here to find a fix or at least a workaround. I read "this encourages wrong usage", so I would like to know what I'm doing wrong. I also read "Fix would be trivial" so I assume a root cause is found, but I don't see it, is is the default exports? How would a PR to fix this look like?

@vjpr
Copy link

vjpr commented Jan 18, 2023

I'm encountering the same issue. I don't understand the wrong usage...


EDIT

I was having same issue here: #6255 (comment)

Crash

import foo from 'foo'
export {foo}

Fix

export {foo} from 'foo'

Is this syntax not valid in TS?

An error would be nice indeed.

@RexSkz
Copy link

RexSkz commented May 17, 2023

For anyone still encountering the issue: it may not be the issue from @swc/core itself; it may be due to the plugins.

I tried the 1.3.58 version of @swc/core, and there is no export {xxx}; in my project, but I still got the "index out of bounds" message. But when I pasted my code to the SWC playground, it worked well. There's also no issue when I use 1.3.7 version in the playground.

Then I removed all plugins in my project and found it worked... After several tries, I found that both options.plugin and options.jsc.experimental.plugins can cause this issue.

I'm not familiar with Rust so I didn't find the root cause, but removing all SWC plugins is an acceptable solution for me.

@CanadaHonk
Copy link

I get this with a blank plugin in JS just extending Visitor (class MyPlugin extends Visitor { }). How does this "encourage wrong usage"? The only way to not cause this error (on my machine) is to not use any JS plugins.

@guiled
Copy link

guiled commented Jun 12, 2023

So, I comment as well to say that I have a similar error with this parsed code

myGlob = function () {
    let a = 42;
};
myGlob();

I can't remove the plugins because I use them to transform code for a specific parser. I don't know rust as well in order to make a PR.
Is the wrong usage mentioned before the fact that we use JS plugins instead of rs plugins ?

rene-leanix added a commit to rene-leanix/swc-jest-preset-angular that referenced this issue Jun 23, 2023
There is a bug when using a plugin:
swc-project/swc#6255
@rene-leanix
Copy link

I also encountered this now with completely unrelated code to be transformed (see #7570) and the use of a plugin that merely returns the module/program. In my case, it's not possible to adjust the code, as that's provided by third parties.

I'm not going to fix this by myself as fixing this encourages wrong usage

@kdy1, would you mind elaborating why the wrong usage is in this case? The use of the plugin hook?

As the removal of the plugin fixes the issue in all instances, it means that the issue lies with calling the transform function with an swc-generated AST for those code snippets.

In other words,

const module = parseSync('fn((obj) => obj?.method());');
transformSync(module);

fails, but

transformSync('fn((obj) => obj?.method());');

succeeds.

Shouldn't this be fixed?

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Jun 23, 2023
@kdy1
Copy link
Member

kdy1 commented Jun 23, 2023

@rene-leanix transform(Module) will also be removed when we can make a breaking change.

@CanadaHonk
Copy link

I have this issue when just using transform(string), when using a blank JS plugin.

@kdy1
Copy link
Member

kdy1 commented Jun 23, 2023

JS plugins will be removed in v2, and it's deprecated.

@rene-leanix
Copy link

Thanks for the quick response and clarification, @kdy1! Is there any place I can read up about the major (especially breaking) changes planned for v2?

If JS plugins are removed, could there be a way to register a (published or local) Rust plugin from within the JS code for a particular transformation?

@swc-bot
Copy link
Collaborator

swc-bot commented Jul 24, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Jul 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests