{"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":245,"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":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/evanw/esbuild/blob/6ff1d8b0d8c134e867a397eef39702a223ebef9e/pkg/api/api_impl.go#L227-L263","documentation":"A panic raised by validateTreeShaking when the TreeShaking value is not TreeShakingDefault, TreeShakingFalse, or TreeShakingTrue. Note the default branch depends on bundle/format (tree shaking is on when bundling or in IIFE). Internal invariant reachable only via Go-API misuse.","triggerScenarios":"Go API: BuildOptions.TreeShaking = TreeShaking(7). The JS API exposes treeShaking as a boolean-like union ('true' | 'false') and cannot produce an invalid ordinal.","commonSituations":"Reflective config decoding; fork ordinal drift; unsafe casts; assuming a numeric mapping that does not match upstream ordinals.","solutions":["Use only TreeShakingDefault, TreeShakingTrue, or TreeShakingFalse.","Validate deserialized values against an allowlist.","Do not cast integers to TreeShaking.","Pin a single esbuild version."],"exampleFix":"// before\nopts.TreeShaking = api.TreeShaking(7)\n\n// after\nopts.TreeShaking = api.TreeShakingTrue","handlingStrategy":"type-guard","validationCode":"func validTreeShaking(t api.TreeShaking) bool {\n  switch t {\n  case api.TreeShakingDefault, api.TreeShakingTrue, api.TreeShakingFalse:\n    return true\n  }\n  return false\n}","typeGuard":"type TreeShaking = true | false\nfunction isTreeShaking(v: unknown): v is TreeShaking {\n  return typeof v === 'boolean'\n}","tryCatchPattern":null,"preventionTips":["Use TreeShakingTrue / TreeShakingFalse (or the default) only.","Validate deserialized config values.","Do not cast integers to TreeShaking."],"tags":["api","tree-shaking","panic","invariant","go-api"],"analyzedSha":"6ff1d8b0d8c134e867a397eef39702a223ebef9e","analyzedAt":"2026-08-03T19:42:38.433Z","schemaVersion":2}