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

Crash when --source-file-name used with non-es6 module type - index out of bounds: the len is 0 but the index is 0 #7035

Closed
normano64 opened this issue Mar 8, 2023 · 4 comments · Fixed by #7229
Assignees
Labels
Milestone

Comments

@normano64
Copy link

Describe the bug

Running the native cli binary on a typescript file that only contain types cause a crash when the --source-file-name flag is used together with the commonjs/umd/amd module type, it doesn't crash with the es6 module type

./swc-linux-x64-gnu compile --source-file-name test.ts --config-file .swcrc test.ts 
thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/sourcemap-6.2.2/src/types.rs:655:9

It seems to work fine for the es6 module type because it still produces a sourcemap when the file is types only while the others are producing empty sourcemaps

Input code

export type Test = {};

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    }
  },
  "module": {
    "type": "commonjs"
  },
  "sourceMaps": true
}

Playground link

No response

Expected behavior

Compiles without crashing

Actual behavior

Crashes

thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/sourcemap-6.2.2/src/types.rs:655:9

Version

1.3.38

Additional context

No response

@normano64 normano64 added the C-bug label Mar 8, 2023
@kdy1 kdy1 added this to the Planned milestone Mar 8, 2023
@chenyukang
Copy link
Contributor

@kdy1 assign to me?
btw, seems we don't have triagebot such as https://rustc-dev-guide.rust-lang.org/rustbot.html.

@chenyukang
Copy link
Contributor

@realtimetodie
This crash happened at this line:

source_map.set_source(0u32, source_file_name);

Will this function need to be removed? since #1388 is resolved.

The rootcause is when we set non-es6 in config file, the node here is a module whose body is a vec of stmt, all of the stmt's span is dummy.

pub fn emit_module(&mut self, node: &Module) -> Result {

begin to emit node: Module(Module { span: Span { lo: BytePos(1), hi: BytePos(23), ctxt: #0 }, body: [Stmt(Expr(ExprStmt { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, expr: Lit(Str(Str { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, value: Atom('use strict' type=dynamic), raw: None })) })), Stmt(Expr(ExprStmt { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, expr: Call(CallExpr { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, callee: Expr(Member(MemberExpr { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, obj: Ident(Ident { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, sym: Atom('Object' type=static), optional: false }), prop: Ident(Ident { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, sym: Atom('defineProperty' type=dynamic), optional: false }) })), args: [ExprOrSpread { spread: None, expr: Ident(Ident { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, sym: Atom('exports' type=inline), optional: false }) }, ExprOrSpread { spread: None, expr: Lit(Str(Str { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, value: Atom('__esModule' type=dynamic), raw: None })) }, ExprOrSpread { spread: None, expr: Object(ObjectLit { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, props: [Prop(KeyValue(KeyValueProp { key: Ident(Ident { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, sym: Atom('value' type=inline), optional: false }), value: Lit(Bool(Bool { span: Span { lo: BytePos(0), hi: BytePos(0), ctxt: #0 }, value: true })) }))] }) }], type_args: None }) }))], shebang: None })

so after emitting, the src_map_buf is still empty, which introduced an empty source_map:

let mut src_map_buf = vec![];

I think an empty source_map here is expected since the code is only valid for es6?

@kdy1
Copy link
Member

kdy1 commented Apr 9, 2023

I think we may skip #L549 when src_map_buf is empty

@swc-bot
Copy link
Collaborator

swc-bot commented May 13, 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 May 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

4 participants