Skip to content

Bun-ESM codegen: same-named functions in two modules collapse to one, and calls silently go to the wrong one #801

Open

Description

@hyperpolymath

Summary

Suppose two modules each define a function with the same name, and both modules end up in one --bun-esm compile. The output keeps only one JS function of that name, and every caller in both modules calls it. Type-checking passes on every module, and the compiler gives no warning or error. The program runs and gives wrong results.

This is a soundness hole: the compiled program does not do what the typed source says. It affects module-private functions as well as pub ones.

Minimal reproduction

Compiler: hyperpolymath/affinescript at 613e9be293b4040ae4b14f4145a651c7aec6c592. That is metadatastician/kyx's pinned compiler, the merge of #795, built with dune build bin/main.exe. Run with Bun 1.4.4. I have not rebuilt main (70379fe) to check it there.

Lib/A.affine:

module Lib.A;
pub fn label() -> String { "from A" }
pub fn a_label() -> String { label() }

Lib/B.affine:

module Lib.B;
pub fn label() -> String { "from B" }
pub fn b_label() -> String { label() }

Main.affine:

module Main;
use Bun::{ bun_stdout_write };
use Lib.A::{ a_label };
use Lib.B::{ b_label };
pub fn main() -> Unit {
  bun_stdout_write(a_label() ++ "\n");
  bun_stdout_write(b_label() ++ "\n");
}
$ affinescript check Lib/A.affine; affinescript check Lib/B.affine; affinescript check Main.affine
Type checking passed   (x3)
$ affinescript compile Main.affine -o out.js --bun-esm && bun out.js
from B
from B
$ grep -c 'function label' out.js
1

Expected: from A, then from B. Failing that, a compile error. Actual: from B twice. Lib.A.a_label calls Lib.B.label.

The result is the same when label is not pub in either module: the private helper of one module is replaced by the other module's.

How kyX hit it

metadatastician/kyx#9 added a module Kyx.Sexp with a private helper members(…). Kyx.Ecosystem already had its own private members(e, ind). After the change, the ecosystem front end called Sexp's members and crashed at run time with non-exhaustive match. 18 of kyX's acceptance checks failed. Other colliding names went unnoticed until we compared the emitted output: spec, q, at_line, member, spaces, join_lines, at, is_alpha, is_digit. spec() returns different text in the two modules, so one front end's refusal messages would cite the other's mapping spec.

kyX now works around this by prefixing its helpers (sx_, cl_). That is a workaround, not a fix.

Related

Likely fix direction (not attempted here)

Either qualify emitted names by module, e.g. Lib_A$label, or make a cross-module name clash in one compile unit a hard compile error. The fix should come with a regression test, such as the three files above, asserting from A / from B.

Owner ruling (2026-11-19): file this as an issue. A separate fix thread will pick it up.

Activity

  1. coderabbitai commented on Nov 19, 2026

    @coderabbitai
    Contributor
    🔗 Related PRs

    #797 - fix: module flattening, Bun-ESM match lowering and loader follow-ups to #788 [merged]
    #798 - fix(codegen,modules,json): local shadowing, transitive collisions, runtime pruning, stringify escapes; add Url [open]


    📝 Issue Planner

    Check the box below or use the @coderabbitai plan command to generate an implementation plan and prompts that you can use with your favorite coding assistant.

    • Create Plan

    🧪 Issue enrichment is currently in open beta.

    You can configure auto-planning by selecting labels in the issue_enrichment configuration.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions