Skip to content

Bun-ESM: an imported function aliased as main is auto-run on import #805

Done

Description

@hyperpolymath

Found in review of #802 (CodeRabbit, thread r4701588611). The behaviour predates #802; it was kept there to keep that PR's scope.

What happens. When the root imports a function under the alias main (use A::{entry as main};) and declares no main itself, the flattened program has a top-level fn main. The Bun-ESM emitter treats any top-level main as the entry point and emits await main();, so importing the generated module runs A::entry as a side effect.

Expected. Only a main the root module itself declares is auto-run. An imported binding stays callable under its bound name and is not called on load. The root's own fn main keeps its auto-call.

Likely fix. The flattener records whether the root declares main (or marks bound-name copies), and the emitters (Bun-ESM, JS) emit the auto-call only in that case. Add a regression test for the aliased import and keep the existing root-main tests.

Refs #801, #802.


Generated by Claude Code

Activity

  1. whatproblem-ai commented on Nov 25, 2026

    @whatproblem-ai

    The concrete trigger (use A::{entry as main}; with no root main) plus the stated rule for which main should auto-run makes this easy to act on.

    When the root module only imports a function aliased as main, the flattened program still ends up with a top-level fn main. The Bun-ESM emitter treats that as the entry point and emits await main();, so importing the generated module runs A::entry as a side effect. Only a main the root declares itself should be auto-called. An aliased import should stay callable as main but not run on load.

    One thing to confirm: does the plain JS emitter show the same auto-call today, or is it Bun-ESM only? Your fix mentions both, and that decides how far the regression test needs to reach.

    flowchart TD
        A[📱 Root module] --> B{"Where does 'main' come from?"}
        B -->|Root declares fn main| C[✅ Emit await main()]
        B -->|"use A::{entry as main}"| D[🔄 Flattened top-level fn main]
        D --> E[🔴 Emits await main() today]
        D --> F[✅ Expected: callable, no auto-run]
    
    Loading

    🏆 Clarity Score: 92% - Clear and concise! 🎯

    ⌨️ Commands · 💬 Have feedback? Tell us · 🎯 New here? Add WhatProblem to your repo · 🔒 Privacy

  2. coderabbitai commented on Nov 25, 2026

    @coderabbitai
    Contributor
    🔗 Related PRs

    #797 - fix: module flattening, Bun-ESM match lowering and loader follow-ups to #788 [merged]


    📝 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!

  3. whatproblem-ai commented on Nov 25, 2026

    @whatproblem-ai

    🤖 WhatProblem only responds to issues and comments written by people.

    This one came from coderabbitai[bot], so we skipped it. Automated issues from CI, linters and dependency bots are usually already actionable, and skipping them keeps your monthly allowance for the issues your team writes.

    You'll see this note at most once a month per repository. If you would rather we did respond to automated issues, tell us below.

    ⌨️ Commands · 💬 Have feedback? Tell us · 🔒 Privacy

  4. added
    bug
    priority:p2
    scope:repo
    status:ready
    on Nov 25, 2026
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

    bugpriority:p2scope:repostatus:ready

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions