oven-sh/bun · error · bun_exe_format::pe::Error

SectionExists

Error message

SectionExists

What it means

add_bun_section found an existing section named exactly '.bun' in the section table (src/exe_format/pe.rs:481-485) and refuses to add a second one. A .bun section means the base executable is itself already a Bun `--compile` output with an embedded module graph; layering another graph would leave two conflicting payloads.

Source

Thrown at src/exe_format/pe.rs:29

    #[error("OutOfBounds")]
    OutOfBounds,
    #[error("BadAlignment")]
    BadAlignment,
    #[error("Overflow")]
    Overflow,
    #[error("InvalidPEFile")]
    InvalidPEFile,
    #[error("InvalidDOSSignature")]
    InvalidDOSSignature,
    #[error("InvalidPESignature")]
    InvalidPESignature,
    #[error("UnsupportedPEFormat")]
    UnsupportedPEFormat,
    #[error("InsufficientHeaderSpace")]
    InsufficientHeaderSpace,
    #[error("TooManySections")]
    TooManySections,
    #[error("SectionExists")]
    SectionExists,
    #[error("InputIsSigned")]
    InputIsSigned,
    #[error("InvalidSecurityDirectory")]
    InvalidSecurityDirectory,
    #[error("SecurityDirInsideImage")]
    SecurityDirInsideImage,
    #[error("UnexpectedOverlayPresent")]
    UnexpectedOverlayPresent,
    #[error("InsufficientSpace")]
    InsufficientSpace,
}

/// Windows PE Binary manipulation for codesigning standalone executables
pub struct PEFile {
    pub(crate) data: Vec<u8>,
    // Store offsets instead of pointers to avoid invalidation after resize
    pub(crate) pe_header_offset: usize,

View on GitHub (pinned to 8c5296ac45)

Solutions

  1. Compile from the pristine Bun executable plus your entrypoint, never from a previously compiled standalone exe.
  2. Separate build input/output directories so a previous build's .exe can never be picked up as the base.
  3. Clear BUN_COMPILE_CACHE if it may hold an injected binary.
  4. If you need to update an embedded app, rebuild from source with `bun build --compile` again rather than re-patching the old artifact.

Example fix

# before: re-compiling an already-compiled exe
bun build app.ts --compile --outfile app.exe   # app.exe later reused as base
bun build app.ts --compile --base app.exe --outfile app2.exe  # SectionExists
# after: always base on the stock runtime
bun build app.ts --compile --outfile app2.exe
Defensive patterns

Strategy: validation

Validate before calling

const bytes = await Bun.file('base.exe').arrayBuffer();
const dv = new DataView(bytes);
const peOff = dv.getUint32(0x3c, true);
const numSections = dv.getUint16(peOff + 6, true);
const optSize = dv.getUint16(peOff + 20, true);
const table = peOff + 24 + optSize;
for (let i = 0; i < numSections; i++) {
  const name = new Uint8Array(bytes, table + i * 40, 8);
  if (String.fromCharCode(...name).startsWith('.bun')) throw new Error('base already contains a .bun section; use a pristine exe');
}

Prevention

When it happens

Trigger: Using a previously produced `bun build --compile` executable as the base for another `--compile` build (re-compiling a compiled binary), or pointing `--base`/the cache at an already-injected standalone exe.

Common situations: Teams trying to 're-compile' or 'update' a shipped standalone exe by feeding it back to bun build; build pipelines that accidentally copy the previous build's output into the input path; using a stale compile cache entry that contains an injected binary.

Related errors


AI-assisted analysis of oven-sh/bun@8c5296ac45 (2026-08-16). Data as JSON: /api/errors/12a5b0ad70e8e9db. Report an issue: GitHub.