This repository is a minimal reproduction for a goscript code generation bug.
src/types.go
type Foo struct {
Value string
}
var Storage = map[string]Foo{
"foo": {Value: "bar"},
}src/read.go
func ReadValue(key string) string {
return Storage[key].Value
}A plain Go map read:
func ReadValue(key string) string {
return Storage[key].Value
}is compiled into TypeScript that calls mapGet with new Foo() as the zero-value fallback:
export function ReadValue(key: string): string {
return $.mapGet(Storage, key, new Foo())[0].Value
}However, the generated file does not import Foo, so the code crashes at runtime with:
ReferenceError: Foo is not definedIn the source Go code, Storage[key] is just a normal read from a map[string]Foo. That access should not fail because of a missing symbol introduced by code generation.
The compiler lowers the map read into a mapGet(...) call and injects a struct zero value with new Foo(), but it does not also make Foo available in the generated module. The result is invalid output even though the Go input is valid.
Requirements:
- Go
- Bun
Run:
./reproduce_bug.shThat script:
- Deletes
output/ - Runs the local compiler in
cmd/compile - Executes
demo-bug.tswith Bun
The generated code should be runnable, and this call:
console.log(ReadValue("foo"));should print:
barThe compiler succeeds, but running the generated output fails:
Compiled successfully to output/
Testing direct map access (will fail with ReferenceError due to bug):
1 | import * as $ from "@goscript/builtin/index.js"
2 | import { Storage } from "./types.gs.js";
3 |
4 | export function ReadValue(key: string): string {
5 | return $.mapGet(Storage, key, new Foo())[0].Value
^
ReferenceError: Foo is not definedThe bad output is in output/@goscript/goscriptbug/src/read.gs.ts.