evanw/esbuild · critical
Invalid tree shaking
Error message
Invalid tree shaking
What it means
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.
Source
Thrown at pkg/api/api_impl.go:245
}
}
func validateTreeShaking(value TreeShaking, bundle bool, format Format) bool {
switch value {
case TreeShakingDefault:
// If we're in an IIFE then there's no way to concatenate additional code
// to the end of our output so we assume tree shaking is safe. And when
// bundling we assume that tree shaking is safe because if you want to add
// code to the bundle, you should be doing that by including it in the
// bundle instead of concatenating it afterward, so we also assume tree
// shaking is safe then. Otherwise we assume tree shaking is not safe.
return bundle || format == FormatIIFE
case TreeShakingFalse:
return false
case TreeShakingTrue:
return true
default:
panic("Invalid tree shaking")
}
}
func validateLoader(value Loader) config.Loader {
switch value {
case LoaderBase64:
return config.LoaderBase64
case LoaderBinary:
return config.LoaderBinary
case LoaderCopy:
return config.LoaderCopy
case LoaderCSS:
return config.LoaderCSS
case LoaderDataURL:
return config.LoaderDataURL
case LoaderDefault:
return config.LoaderDefault
case LoaderEmpty:View on GitHub (pinned to 6ff1d8b0d8)
Solutions
- Use only TreeShakingDefault, TreeShakingTrue, or TreeShakingFalse.
- Validate deserialized values against an allowlist.
- Do not cast integers to TreeShaking.
- Pin a single esbuild version.
Example fix
// before opts.TreeShaking = api.TreeShaking(7) // after opts.TreeShaking = api.TreeShakingTrue
Defensive patterns
Strategy: type-guard
Validate before calling
func validTreeShaking(t api.TreeShaking) bool {
switch t {
case api.TreeShakingDefault, api.TreeShakingTrue, api.TreeShakingFalse:
return true
}
return false
} Type guard
type TreeShaking = true | false
function isTreeShaking(v: unknown): v is TreeShaking {
return typeof v === 'boolean'
} Prevention
- Use TreeShakingTrue / TreeShakingFalse (or the default) only.
- Validate deserialized config values.
- Do not cast integers to TreeShaking.
When it happens
Trigger: Go API: BuildOptions.TreeShaking = TreeShaking(7). The JS API exposes treeShaking as a boolean-like union ('true' | 'false') and cannot produce an invalid ordinal.
Common situations: Reflective config decoding; fork ordinal drift; unsafe casts; assuming a numeric mapping that does not match upstream ordinals.
Related errors
AI-assisted analysis of evanw/esbuild@6ff1d8b0d8 (2026-08-03).
Data as JSON: /data/errors/12a4beff3f8821d0.json.
Report an issue: GitHub.