Skip to content

attila-kun/goscriptbug

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goscript map access bug repro

This repository is a minimal reproduction for a goscript code generation bug.

Minimal source involved

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
}

Summary

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 defined

Why this is a bug

In 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.

Reproduction

Requirements:

  • Go
  • Bun

Run:

./reproduce_bug.sh

That script:

  1. Deletes output/
  2. Runs the local compiler in cmd/compile
  3. Executes demo-bug.ts with Bun

Expected behavior

The generated code should be runnable, and this call:

console.log(ReadValue("foo"));

should print:

bar

Actual behavior

The 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 defined

The bad output is in output/@goscript/goscriptbug/src/read.gs.ts.

About

Minimal reproduction of a goscript compiler bug

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors