evanw/esbuild · critical

Invalid source map

Error message

Invalid source map

What it means

A panic raised by validateSourceMap when the SourceMap value is not one of the documented constants (SourceMapNone, SourceMapLinked, SourceMapInline, SourceMapExternal, SourceMapInlineAndExternal). The typed JS/TS API makes this unreachable; only Go-API misuse (out-of-range SourceMap) hits it.

Source

Thrown at pkg/api/api_impl.go:149

	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")
	}
}

func validateLegalComments(value LegalComments, bundle bool) config.LegalComments {
	switch value {
	case LegalCommentsDefault:
		if bundle {
			return config.LegalCommentsEndOfFile
		} else {
			return config.LegalCommentsInline
		}
	case LegalCommentsNone:
		return config.LegalCommentsNone
	case LegalCommentsInline:
		return config.LegalCommentsInline
	case LegalCommentsEndOfFile:
		return config.LegalCommentsEndOfFile
	case LegalCommentsLinked:

View on GitHub (pinned to 6ff1d8b0d8)

Solutions

  1. Use only the documented SourceMap constants.
  2. Map user-facing strings to constants through a guarded switch.
  3. Never cast an unchecked integer to SourceMap.
  4. Add a regression test enumerating every supported sourcemap mode.

Example fix

// before
opts.Sourcemap = api.SourceMap(7)

// after
opts.Sourcemap = api.SourceMapLinked  // or Inline/External/Both/None
Defensive patterns

Strategy: type-guard

Validate before calling

func validSourceMap(s api.SourceMap) bool {
  switch s {
  case api.SourceMapNone, api.SourceMapLinked, api.SourceMapInline, api.SourceMapExternal, api.SourceMapInlineAndExternal:
    return true
  }
  return false
}

Type guard

type SourceMap = true | false | 'inline' | 'linked' | 'external' | 'both'
function isSourceMap(v: unknown): v is SourceMap {
  return [true, false, 'inline', 'linked', 'external', 'both'].includes(v as any)
}

Prevention

When it happens

Trigger: Go API: assign BuildOptions.Sourcemap = SourceMap(7). The JS API's Sourcemap option is a closed union ('true' | 'false' | 'inline' | 'linked' | 'external' | 'both') that maps to valid constants and cannot reach this panic.

Common situations: Config decoded from JSON into a raw int then cast; a fork that renamed/removed a SourceMap variant while the caller still references the old ordinal; unsafe pointer tricks over the struct field.

Related errors


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