Skip to content

bun build --target bun and bun build --compile can emit a re-exported namespace binding without the underlying definition #28137

@eyenalxai

Description

@eyenalxai

What version of Bun is running?

1.3.10+30e609e08

What platform is your computer?

Linux 6.19.6-arch1-1 x86_64 unknown

What steps can reproduce the bug?

https://github.com/eyenalxai/bun-bundle-bug-mre

This repository is a minimal workspace repro with three packages:

  • @mini-bug/utils
  • @mini-bug/codec
  • @mini-bug/repro

The relevant code shape is:

// packages/mini-bug-utils/src/index.ts
import * as typed from './arrays/typed/index.js'
import * as u8 from './arrays/u8/pool.js'

export { typed, u8 }
// packages/mini-bug-utils/src/arrays/typed/index.ts
export { toDataView } from './misc.js'
// packages/mini-bug-codec/src/network/transports/intermediate.ts
import { typed } from '@mini-bug/utils'

const view = typed.toDataView(output)
// packages/mini-bug-repro/src/index.ts
import { u8 } from "@mini-bug/utils"
import { PaddedIntermediatePacketCodec } from "@mini-bug/codec"

Commands:

bun install
bun run source
bun run bundle:run
bun run compile:run

What they do:

"scripts": {
    "source": "bun run ./src/index.ts",
    "bundle:run": "bun build ./src/index.ts --outfile ./dist/index.js --target bun && bun run ./dist/index.js",
    "compile:run": "bun build --compile --target=bun-linux-x64-modern ./src/index.ts --outfile ./dist/index && ./dist/index"
  },

Current results:

  • bun run source succeeds
  • bun run bundle:run fails
  • bun run compile:run fails

What is the expected behavior?

bun build --target bun and bun build --compile should preserve the same behavior as the source run.

Both generated outputs should run successfully and print:

14
8

What do you see instead?

ReferenceError: toDataView is not defined

bun run bundle:run:

Bundled 6 modules in 2ms

  index.js  1.61 KB  (entry point)

15 | };
16 | 
17 | // ../mini-bug-utils/src/arrays/typed/index.ts
18 | var exports_typed = {};
19 | __export(exports_typed, {
ReferenceError: toDataView is not defined
      at toDataView (/home/ulezot/Projects/js/bun-bundle-bug-mre/packages/mini-bug-repro/dist/index.js:20:31)
      at encode (/home/ulezot/Projects/js/bun-bundle-bug-mre/packages/mini-bug-repro/dist/index.js:41:31)
      at /home/ulezot/Projects/js/bun-bundle-bug-mre/packages/mini-bug-repro/dist/index.js:56:13
      at loadAndEvaluateModule (2:1)

bun run compile:run:

   [2ms]  bundle  6 modules
   [1ms] compile  ./dist/index
15 | };
16 | 
17 | // ../mini-bug-utils/src/arrays/typed/index.ts
18 | var exports_typed = {};
19 | __export(exports_typed, {
ReferenceError: toDataView is not defined
      at toDataView (/$bunfs/root/index:20:31)
      at encode (/$bunfs/root/index:41:31)
      at /$bunfs/root/index:56:13
      at loadAndEvaluateModule (2:1)

Additional information

The failure appears to depend on this combination:

  • a namespace export from @mini-bug/utils
  • a re-exported binding in packages/mini-bug-utils/src/arrays/typed/index.ts
  • runtime access through typed.toDataView(...)

Two changes both make the repro stop failing:

  1. Replace export { toDataView } from './misc.js' with a direct local definition in typed/index.ts.
  2. Bypass typed/index.ts and import from ./arrays/typed/misc.js instead.

That suggests the bundled output is creating a getter for the re-exported binding without preserving the matching binding definition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions