panic: runtime error: slice bounds out of range
Starting from 7.0.0-dev.20250530.1 this issue start happening on my project.
I tracked down the offending commit (by locally building tsgo)
repro (thanks to @joshcartme ) https://github.com/joshcartme/tsgo-slice-bounds-repro.
Update: It happens for me when a file using React types without importing React which was supported on 5.8 and previously on tsgo.
Example:
component: React.ComponentType<any>
Here is full output
> /Users/hrh/project/typescript-go/built/local/tsgo --noEmit --skipLibCheck --project tsconfig.json
panic: runtime error: slice bounds out of range [:59935] with length 1010
goroutine 1 [running]:
github.com/microsoft/typescript-go/internal/scanner.GetLineAndCharacterOfPosition({0x104f145a8, 0x140098a1348}, 0xea1f)
/Users/hrh/project/typescript-go/internal/scanner/scanner.go:2322 +0x158
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).visitDeclarationSubtree(0x140ba05fb20, 0x14003dd1600)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:470 +0x808
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).visit(0x140002f1718?, 0x1048ea4cc?)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:143 +0x114
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).VisitSlice(0x140bb73f500, {0x14003deb288, 0x2, 0x2})
/Users/hrh/project/typescript-go/internal/ast/visitor.go:148 +0xa0
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).VisitNodes(0x140bb73f500, 0x14004898128)
/Users/hrh/project/typescript-go/internal/ast/visitor.go:99 +0x40
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).visitNodes(0x140002f1818?, 0x104b79aa8?)
/Users/hrh/project/typescript-go/internal/ast/visitor.go:236 +0x44
github.com/microsoft/typescript-go/internal/ast.(*UnionTypeNode).VisitEachChild(0x14003dd1680, 0x140bb73f500)
/Users/hrh/project/typescript-go/internal/ast/ast.go:6997 +0x30
github.com/microsoft/typescript-go/internal/ast.(*Node).VisitEachChild(...)
/Users/hrh/project/typescript-go/internal/ast/ast.go:217
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).VisitEachChild(...)
/Users/hrh/project/typescript-go/internal/ast/visitor.go:191
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).visitDeclarationSubtree(0x140ba05fb20, 0x14003dd1680)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:493 +0xce8
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).visit(0x104b33700?, 0x140ba06db88?)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:143 +0x114
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).transformTypeAliasDeclaration(0x140ba05fb20, 0x14004049100)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:1122 +0x78
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).transformTopLevelDeclaration(0x140ba05fb20, 0x14004049100)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:1090 +0x468
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).visitDeclarationStatements(0x140ba05fb20, 0x14004049100)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:940 +0x4c
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).visit(0x140002f1bc8?, 0x104b80454?)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:118 +0xec
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).VisitSlice(0x140bb73f500, {0x140023f5608, 0x106, 0x106})
/Users/hrh/project/typescript-go/internal/ast/visitor.go:148 +0xa0
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).VisitNodes(0x140bb73f500, 0x140041e7ce8)
/Users/hrh/project/typescript-go/internal/ast/visitor.go:99 +0x40
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).transformModuleDeclaration(0x140ba05fb20, 0x14003a26090)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:1179 +0xb8
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).transformTopLevelDeclaration(0x140ba05fb20, 0x14003a26090)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:1096 +0x438
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).transformAndReplaceLatePaintedStatements(0x140ba05fb20, 0x140098b4500)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:235 +0xf4
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).transformSourceFile(0x140ba05fb20, 0x140098a1348)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:186 +0x40
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).visitSourceFile(0x140ba05fb20, 0x140098a1348)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:172 +0x174
github.com/microsoft/typescript-go/internal/transformers/declarations.(*DeclarationTransformer).visit(0x140ba05fb20?, 0x140845f24e0?)
/Users/hrh/project/typescript-go/internal/transformers/declarations/transform.go:102 +0xc0
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).VisitNode(0x1133319f8?, 0x140c9250c68?)
/Users/hrh/project/typescript-go/internal/ast/visitor.go:51 +0x30
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).VisitSourceFile(...)
/Users/hrh/project/typescript-go/internal/ast/visitor.go:36
github.com/microsoft/typescript-go/internal/transformers.(*Transformer).TransformSourceFile(...)
/Users/hrh/project/typescript-go/internal/transformers/transformer.go:42
github.com/microsoft/typescript-go/internal/compiler.getDeclarationDiagnostics({0x104f24c90, 0x140c9250c68}, {0x104f264f8, 0x140377529a0}, 0x140098a1348)
/Users/hrh/project/typescript-go/internal/compiler/emitter.go:477 +0x134
github.com/microsoft/typescript-go/internal/compiler.(*Program).getDeclarationDiagnosticsForFile(0x1400049c540, {0x140000921a0?, 0x1403327d008?}, 0x140098a1348)
/Users/hrh/project/typescript-go/internal/compiler/program.go:576 +0x84
github.com/microsoft/typescript-go/internal/compiler.(*Program).getDiagnosticsHelper(0x1400049c540, {0x104f15568, 0x1053ea660}, 0x0?, 0x0?, 0x1?, 0x140002f2390)
/Users/hrh/project/typescript-go/internal/compiler/program.go:678 +0x1a0
github.com/microsoft/typescript-go/internal/compiler.(*Program).GetDeclarationDiagnostics(...)
/Users/hrh/project/typescript-go/internal/compiler/program.go:467
github.com/microsoft/typescript-go/internal/execute.emitFilesAndReportErrors({0x104f19750, 0x140000c4a50}, 0x1400049c540, 0x14000092d00)
/Users/hrh/project/typescript-go/internal/execute/tsc.go:277 +0x4b4
github.com/microsoft/typescript-go/internal/execute.performCompilation({0x104f19750, 0x140000c4a50}, 0x0, 0x14000192000, 0x14000092d00)
/Users/hrh/project/typescript-go/internal/execute/tsc.go:207 +0x1a4
github.com/microsoft/typescript-go/internal/execute.executeCommandLineWorker({0x104f19750, 0x140000c4a50}, 0x0, 0x14000192090)
/Users/hrh/project/typescript-go/internal/execute/tsc.go:135 +0x65c
github.com/microsoft/typescript-go/internal/execute.CommandLine({0x104f19750, 0x140000c4a50}, 0x0, {0x140000c4010, 0x4, 0x4})
/Users/hrh/project/typescript-go/internal/execute/tsc.go:35 +0x1f4
main.runMain()
/Users/hrh/project/typescript-go/cmd/tsgo/main.go:23 +0x120
main.main()
/Users/hrh/project/typescript-go/cmd/tsgo/main.go:10 +0x1c
Completed with failure in 3985ms
Do you have a repro? We can't fix the bug without some way of testing it. (None of our tests crash.)
We're also seeing this in a large codebase. I'm working on finding a simple repro, but in case it rings any bells it looks like the exception originates in:
https://github.com/microsoft/typescript-go/blob/e7a30f5106f7af66974b59bba2d9510c11b7bfb6/internal/scanner/scanner.go#L2092
In my case text has length 1286 (it looks like the source of a react component) and pos is 4763
Do you have a repro? We can't fix the bug without some way of testing it. (None of our tests crash.)
I am trying to create a repro but still not successful, it fails a couple of projects in our monorepo and could find where exactly but still on vanilla app it's not happening. probably a codebase size thing.
I have managed to create a repro here https://github.com/joshcartme/tsgo-slice-bounds-repro. The code is pretty nonsensical and convoluted and would be riddled with type errors (including non-existent files) if not for the panic, but removing just about anything about it will cause it to no longer panic. Running it right now gives the same error, here's the beginning of it:
$ yarn run tsgo
yarn run v1.22.22
$ /home/repro/tsgo-slice-bounds-repro/node_modules/.bin/tsgo
panic: runtime error: slice bounds out of range [481:398]
goroutine 80 [running]:
github.com/microsoft/typescript-go/internal/scanner.SkipTriviaEx({0xc000446000, 0x18e}, 0xc003b8d5c0?, 0x6dd508?)
Thanks @joshcartme I think I know why this is happening and I fixed my problem.
I was using types from React in .ts files without importing React specifically. For example I had a type like
component: React.ComponentType<any>
but the file didn't explicitly import React and importing it fixed the issue.
For your repro the problem is this file not having the import and if you just comment out this part, tsgo will work.
Hopefully the fix will be quick with your repro!
Thanks @hamidrezahanafi.
@jakebailey I've made the repro more coherent (by that I mean it has no type errors outside of what I think should be a type error when it panics). As is, it will induce a panic, if you swap the commented import with the un-commented one here https://github.com/joshcartme/tsgo-slice-bounds-repro/blob/bc97736d200cc6c43e236aa4f43b83d516ca4e46/src/components/view-one.tsx#L4-L5 it will have no type errors.
edit:
Something else I just noticed when it panics in my repro, pos https://github.com/microsoft/typescript-go/blob/a9843d93d66d42851d971f5aad97f509d4d2660a/internal/scanner/scanner.go#L2092 is always 177. So if you add the comment
// 8chars!
to the bottom of constants-panics.ts it will not panic and will instead produce the appropriate type error, because text will now be long enough to not have the slice be out of bounds.