evanw/esbuild · critical

Invalid charset

Error message

Invalid charset

What it means

A panic raised by validateASCIIOnly when the Charset value is not CharsetDefault, CharsetASCII, or CharsetUTF8. Internal invariant reachable only through Go-API misuse (out-of-range Charset).

Source

Thrown at pkg/api/api_impl.go:215

	case LogLevelWarning:
		return logger.LevelWarning
	case LogLevelError:
		return logger.LevelError
	case LogLevelSilent:
		return logger.LevelSilent
	default:
		panic("Invalid log level")
	}
}

func validateASCIIOnly(value Charset) bool {
	switch value {
	case CharsetDefault, CharsetASCII:
		return true
	case CharsetUTF8:
		return false
	default:
		panic("Invalid charset")
	}
}

func validateExternalPackages(value Packages) bool {
	switch value {
	case PackagesDefault, PackagesBundle:
		return false
	case PackagesExternal:
		return true
	default:
		panic("Invalid packages")
	}
}

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

View on GitHub (pinned to 6ff1d8b0d8)

Solutions

  1. Use only CharsetDefault, CharsetASCII, or CharsetUTF8.
  2. Validate deserialized values against an allowlist.
  3. Do not cast integers to Charset.
  4. Pin a single esbuild version.

Example fix

// before
opts.Charset = api.Charset(7)

// after
opts.Charset = api.CharsetUTF8
Defensive patterns

Strategy: type-guard

Validate before calling

func validCharset(c api.Charset) bool {
  switch c {
  case api.CharsetDefault, api.CharsetASCII, api.CharsetUTF8:
    return true
  }
  return false
}

Type guard

type Charset = 'ascii' | 'utf8'
function isCharset(v: unknown): v is Charset {
  return v === 'ascii' || v === 'utf8'
}

Prevention

When it happens

Trigger: Go API: BuildOptions.Charset = Charset(7). The JS API's Charset union ('ascii' | 'utf8') maps to valid constants and cannot trigger this.

Common situations: Reflective config decoding; fork ordinal drift; unsafe casts.

Related errors


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