evanw/esbuild · critical

Invalid format

Error message

Invalid format

What it means

A panic raised by validateFormat when the Format value is not one of FormatDefault, FormatIIFE, FormatCommonJS, FormatESModule. As with the other validate* panics, the typed public API should prevent this; reaching it means an out-of-range Format was constructed, typically via unsafe Go code.

Source

Thrown at pkg/api/api_impl.go:132

	case PlatformNeutral:
		return config.PlatformNeutral
	default:
		panic("Invalid platform")
	}
}

func validateFormat(value Format) config.Format {
	switch value {
	case FormatDefault:
		return config.FormatPreserve
	case FormatIIFE:
		return config.FormatIIFE
	case FormatCommonJS:
		return config.FormatCommonJS
	case FormatESModule:
		return config.FormatESModule
	default:
		panic("Invalid format")
	}
}

func validateSourceMap(value SourceMap) config.SourceMap {
	switch value {
	case SourceMapNone:
		return config.SourceMapNone
	case SourceMapLinked:
		return config.SourceMapLinkedWithComment
	case SourceMapInline:
		return config.SourceMapInline
	case SourceMapExternal:
		return config.SourceMapExternalWithoutComment
	case SourceMapInlineAndExternal:
		return config.SourceMapInlineAndExternal
	default:
		panic("Invalid source map")
	}

View on GitHub (pinned to 6ff1d8b0d8)

Solutions

  1. Use only the documented Format constants (FormatIIFE, FormatCommonJS, FormatESModule, FormatDefault).
  2. Validate decoded values against an allowlist before assignment.
  3. Avoid casting arbitrary integers to Format.
  4. Re-vendor the latest esbuild Go package if you suspect an ABI drift.

Example fix

// before
opts.Format = api.Format(rawValue)  // rawValue out of range

// after
switch rawValue {
case "iife": opts.Format = api.FormatIIFE
case "cjs":  opts.Format = api.FormatCommonJS
case "esm": opts.Format = api.FormatESModule
}
Defensive patterns

Strategy: type-guard

Validate before calling

func validFormat(f api.Format) bool {
  switch f {
  case api.FormatDefault, api.FormatIIFE, api.FormatCommonJS, api.FormatESModule:
    return true
  }
  return false
}

Type guard

type Format = 'iife' | 'cjs' | 'esm'
function isFormat(v: unknown): v is Format {
  return v === 'iife' || v === 'cjs' || v === 'esm'
}

Prevention

When it happens

Trigger: Go API: set BuildOptions.Format = Format(42) or any value produced by an unchecked cast. The JS API constrains Format to 'iife' | 'cjs' | 'esm' and cannot trigger this. Fires during option validation at build start.

Common situations: Reading the format from an untyped config map and casting with Format(intVal) without validation; ABI mismatch between a vendored esbuild fork and the public enum; fuzzing or property-based tests that synthesize random enum values.

Related errors


AI-assisted analysis of evanw/esbuild@6ff1d8b0d8 (2026-08-03). Data as JSON: /data/errors/edafada400bfbf4b.json. Report an issue: GitHub.