{"record":{"id":"12a4beff3f8821d0","repo":"evanw/esbuild","slug":"invalid-tree-shaking","errorCode":null,"errorMessage":"Invalid tree shaking","messagePattern":"Invalid tree shaking","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/api/api_impl.go","lineNumber":258,"sourceCode":"\t}\n}\n\nfunc validateTreeShaking(value TreeShaking, bundle bool, format Format) bool {\n\tswitch value {\n\tcase TreeShakingDefault:\n\t\t// If we're in an IIFE then there's no way to concatenate additional code\n\t\t// to the end of our output so we assume tree shaking is safe. And when\n\t\t// bundling we assume that tree shaking is safe because if you want to add\n\t\t// code to the bundle, you should be doing that by including it in the\n\t\t// bundle instead of concatenating it afterward, so we also assume tree\n\t\t// shaking is safe then. Otherwise we assume tree shaking is not safe.\n\t\treturn bundle || format == FormatIIFE\n\tcase TreeShakingFalse:\n\t\treturn false\n\tcase TreeShakingTrue:\n\t\treturn true\n\tdefault:\n\t\tpanic(\"Invalid tree shaking\")\n\t}\n}\n\nfunc validateLoader(value Loader) config.Loader {\n\tswitch value {\n\tcase LoaderBase64:\n\t\treturn config.LoaderBase64\n\tcase LoaderBinary:\n\t\treturn config.LoaderBinary\n\tcase LoaderCopy:\n\t\treturn config.LoaderCopy\n\tcase LoaderCSS:\n\t\treturn config.LoaderCSS\n\tcase LoaderDataURL:\n\t\treturn config.LoaderDataURL\n\tcase LoaderDefault:\n\t\treturn config.LoaderDefault\n\tcase LoaderEmpty:","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/evanw/esbuild/blob/f6058f8364fe7ab91ca57a83e02577ed74c9cae4/pkg/api/api_impl.go#L240-L276","documentation":"validateTreeShaking (pkg/api/api_impl.go:258) panics when TreeShaking is not TreeShakingDefault(0), TreeShakingFalse(1), or TreeShakingTrue(2). For TreeShakingDefault the function computes a value from `bundle` and `format` (true when bundling or IIFE; otherwise false to be safe with later concatenation); any other numeric value is undefined and aborts. Reached from validateBuildOptions.","triggerScenarios":"Passing a numeric TreeShaking from a cast, JSON-decoded struct, or inter-language bridge with mismatched ordinals. Fires during options construction, before scanning.","commonSituations":"Persisted configs that store tree-shaking as an int, version drift between a wrapper and core, or a typo such as `TreeShaking: api.TreeShakingBoth`.","solutions":["Prefer api.TreeShakingDefault (lets esbuild pick based on bundle/format); or set api.TreeShakingTrue / api.TreeShakingFalse explicitly.","Bounds-check decoded integers against 0..2 before casting.","Serialize the option as a string and map to the constant at load.","Realign wrapper and core esbuild versions."],"exampleFix":"// before\nopts := api.BuildOptions{TreeShaking: api.TreeShaking(7), Bundle: true}\n\n// after\nopts := api.BuildOptions{TreeShaking: api.TreeShakingDefault, Bundle: true}","handlingStrategy":"validation","validationCode":"func checkTreeShaking(t api.TreeShaking) error {\n    switch t {\n    case api.TreeShakingDefault, api.TreeShakingFalse, api.TreeShakingTrue:\n        return nil\n    }\n    return fmt.Errorf(\"invalid tree shaking %d (want 0..2)\", uint8(t))\n}","typeGuard":"func isValidTreeShaking(t api.TreeShaking) bool {\n    switch t {\n    case api.TreeShakingDefault, api.TreeShakingFalse, api.TreeShakingTrue:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Use TreeShakingDefault so esbuild picks based on bundle/format (safe with later concatenation).","Store the option as a string and translate at load.","Bounds-check decoded integers to 0..2.","Re-vendor all consumers after an esbuild upgrade."],"tags":["esbuild","config","validation","panic","tree-shaking","enum"],"backgroundTag":null,"analyzedSha":"f6058f8364fe7ab91ca57a83e02577ed74c9cae4","analyzedAt":"2026-08-09T18:37:22.223Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}