{"record":{"id":"cbca968a49aaeb86","repo":"evanw/esbuild","slug":"invalid-platform","errorCode":null,"errorMessage":"Invalid platform","messagePattern":"Invalid platform","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/api/api_impl.go","lineNumber":117,"sourceCode":"\t\tparts = append(parts, config.PathTemplate{\n\t\t\tData:        template,\n\t\t\tPlaceholder: config.NoPlaceholder,\n\t\t})\n\t}\n\n\treturn parts\n}\n\nfunc validatePlatform(value Platform) config.Platform {\n\tswitch value {\n\tcase PlatformDefault, PlatformBrowser:\n\t\treturn config.PlatformBrowser\n\tcase PlatformNode:\n\t\treturn config.PlatformNode\n\tcase PlatformNeutral:\n\t\treturn config.PlatformNeutral\n\tdefault:\n\t\tpanic(\"Invalid platform\")\n\t}\n}\n\nfunc validateFormat(value Format) config.Format {\n\tswitch value {\n\tcase FormatDefault:\n\t\treturn config.FormatPreserve\n\tcase FormatIIFE:\n\t\treturn config.FormatIIFE\n\tcase FormatCommonJS:\n\t\treturn config.FormatCommonJS\n\tcase FormatESModule:\n\t\treturn config.FormatESModule\n\tdefault:\n\t\tpanic(\"Invalid format\")\n\t}\n}\n","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/evanw/esbuild/blob/f6058f8364fe7ab91ca57a83e02577ed74c9cae4/pkg/api/api_impl.go#L99-L135","documentation":"esbuild panics in validatePlatform (pkg/api/api_impl.go:117) when BuildOptions.Platform holds a Platform value outside the declared iota enum. Platform is a `uint8` whose only valid values are PlatformDefault(0), PlatformBrowser(1), PlatformNode(2), PlatformNeutral(3); the function switch has no default case, so any other value aborts the process. The panic exists because the public Go API assumes callers pass declared constants, so an out-of-range value signals corruption the bundler cannot safely continue past.","triggerScenarios":"Calling api.Build / api.Context / api.Transform with a BuildOptions struct whose Platform field was assigned from a raw integer (e.g. `Platform: 99`), produced by JSON unmarshalling into a `uint8` cast to Platform, or constructed by a code generator / FFI bridge whose numeric mapping differs from this esbuild version. The panic fires synchronously inside validateBuildOptions -> validatePlatform before any file is read.","commonSituations":"Mixing esbuild versions (host Go binary built from one checkout, JS/CLI bridge expecting another), hand-rolling a BuildOptions over a serialization boundary, copying example code from a newer docs page against an older binary, or a typo assigning `Platform: PlatformBrowserX`. Also seen when embedding esbuild via cgo and forgetting to map an upstream enum change after a re-vendor.","solutions":["Assign Platform only via the declared constants: api.PlatformDefault / PlatformBrowser / PlatformNode / PlatformNeutral, or leave the field unset to use the zero-value PlatformDefault.","If deserializing options from JSON/protobuf, validate the integer is in {0,1,2,3} before casting to Platform, or use a string mapping table you control.","Rebuild/realign the binary and any bridge/generator against the same esbuild commit so enum ordinals match.","Pin the esbuild version in go.mod / package.json so a future re-vendor cannot silently renumber the iota."],"exampleFix":"// before\nopts := api.BuildOptions{Platform: api.Platform(7)}\nres := api.Build(opts)\n\n// after\nopts := api.BuildOptions{Platform: api.PlatformBrowser}\nres := api.Build(opts)","handlingStrategy":"validation","validationCode":"// validPlatform maps the only acceptable numeric values for api.Platform.\nvar validPlatform = map[api.Platform]bool{\n    api.PlatformDefault: true,\n    api.PlatformBrowser: true,\n    api.PlatformNode:   true,\n    api.PlatformNeutral:true,\n}\n\nfunc checkPlatform(p api.Platform) error {\n    if !validPlatform[p] {\n        return fmt.Errorf(\"invalid platform %d (want 0..3)\", uint8(p))\n    }\n    return nil\n}\n\n// call before api.Build / api.Context / api.Transform:\nif err := checkPlatform(opts.Platform); err != nil { return err }","typeGuard":"func isValidPlatform(p api.Platform) bool {\n    switch p {\n    case api.PlatformDefault, api.PlatformBrowser, api.PlatformNode, api.PlatformNeutral:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Never cast an arbitrary integer to api.Platform at a trust boundary; map named strings to constants instead.","Run a unit test that round-trips every option you serialize to JSON and back, asserting each enum stays in range.","Pin esbuild in go.mod AND in any JS/wrapper package.json to the same version, and rebuild bridges after upgrades.","Leave Platform unset to rely on the safe zero-value PlatformDefault when the platform is not important."],"tags":["esbuild","config","validation","panic","platform","enum"],"backgroundTag":null,"analyzedSha":"f6058f8364fe7ab91ca57a83e02577ed74c9cae4","analyzedAt":"2026-08-09T18:37:22.223Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}