{"id":"cbca968a49aaeb86","repo":"evanw/esbuild","slug":"invalid-platform","errorCode":null,"errorMessage":"Invalid platform","messagePattern":"Invalid platform","errorType":"validation","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/6ff1d8b0d8c134e867a397eef39702a223ebef9e/pkg/api/api_impl.go#L99-L135","documentation":"A panic raised by validatePlatform when the Platform value passed to Build/Context/Transform is not one of the known enum constants (PlatformDefault, PlatformBrowser, PlatformNode, PlatformNeutral). This is an internal invariant: the public API types are supposed to make an invalid value impossible, so a panic signals either Go-API misuse (constructing an invalid Platform) or memory corruption.","triggerScenarios":"Via the Go API, call Build with Platform set to a value cast from an out-of-range integer (e.g. Platform(99)). Not reachable from the JS API where Platform is a closed string-literal union. Fires at the start of option validation, before any file is read.","commonSituations":"Reflective code that decodes a Platform from JSON/protobuf into a raw int and casts without bounds-checking; cgo or unsafe code corrupting the enum; forks of esbuild that add a platform constant without updating the switch.","solutions":["Only use the documented Platform constants; never construct Platform from an arbitrary integer.","If decoding from external data, validate the value is one of the known constants before assigning it to options.","Pin to esbuild's released API surface and avoid unsafe casts over the Platform type.","Add a unit test that round-trips every documented Platform value through your config loader."],"exampleFix":"// before\nopts.Platform = api.Platform(99)  // invalid cast\n\n// after\nopts.Platform = api.PlatformNode  // or Browser/Neutral/Default","handlingStrategy":"type-guard","validationCode":"// JS: nothing to validate; the typed union prevents invalid values.\n// Go: guard before assigning.\nfunc validPlatform(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}","typeGuard":"type Platform = 'browser' | 'node' | 'neutral'\nfunction isPlatform(v: unknown): v is Platform {\n  return v === 'browser' || v === 'node' || v === 'neutral'\n}","tryCatchPattern":null,"preventionTips":["Never cast an integer to api.Platform in Go.","When decoding config, validate against the documented constants with a default branch.","Pin a single esbuild version to keep enum ordinals stable."],"tags":["api","platform","panic","invariant","go-api"],"analyzedSha":"6ff1d8b0d8c134e867a397eef39702a223ebef9e","analyzedAt":"2026-08-03T19:42:38.433Z","schemaVersion":2}