Skip to content

fix(modules): do not auto-run an imported function bound as main (#805) - #806

Merged
hyperpolymath merged 1 commit into
mainfrom
claude/issue-805-main-alias
Nov 25, 2026
Merged

hyperpolymath merged 1 commit into
mainfrom
claude/issue-805-main-alias

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

Closes #805

What was wrong

Suppose the root imports a function under the alias main (use A::{entry as main};) and declares no main itself. The flattener then made a bound-name copy called main. Bun-ESM (await main();) and JS (main();) call any top-level main when the module loads, so importing the compiled module ran A::entry.

Fix

In lib/module_loader.ml, the flattener no longer makes a copy for a function bound as main. The root's references to that name are rewritten to the function's module-qualified name, the same way imported mutable constants are already handled. Only a main that the root module declares is an entry point. The emitters are unchanged.

For example, with pub fn go() -> Int { main() } the output is now export function go() { return __mod_1A__entry(); }, with no auto-call.

Tests

New test: test_805_imported_main_alias in test/test_module_root_flatten.ml:

  • With the aliased import, neither Bun-ESM nor JS emits the auto-call.
  • With a root main, both backends still emit it.
  • Under node, go() still returns the imported value.

The test fails without the loader change. Locally, the full suite (606 tests), the Bun harness and the soundness ledger pass.

Refs #801, #802.


Generated by Claude Code

A root that imports a function as `main` (`use A::{entry as main};`) got a
bound-name copy named `main`, and the JS backends call any top-level `main`
on load, so importing the compiled module ran `A::entry`. The flattener now
makes no copy for a function bound as `main`: the root's references to it are
rewritten to its qualified name. Only a `main` the root declares is an entry
point. Regression test for Bun-ESM and JS, plus the root-main auto-call.

Closes #805

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0136P7Zo5h4xmXVEeSeQEDvw
@gitnotebooks

gitnotebooks Bot commented Nov 25, 2026

Copy link
Copy Markdown

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Imported functions aliased as main are no longer automatically run as the module’s entry point in Bun-ESM and JavaScript builds. They remain callable from the root module, and a main function declared there continues to run automatically.

The flattener now keeps imported functions aliased as main under their qualified names rather than copying them as root main. Regression tests cover load-time auto-call behaviour for Bun-ESM and JS, root-defined main, and calls through the imported alias.

Changes: Imported main alias handling

Layer / File(s) Summary
Flattening and entry-point regression coverage
lib/module_loader.ml, test/test_module_root_flatten.ml, CHANGELOG.adoc
The flattener renames an imported function bound as main to its qualified name. Tests check that Bun-ESM and JS do not auto-call the alias, while root-defined main is auto-called. A Node check covers calls through the alias when Node is available. The changelog records the behaviour.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

Suggested reviewers: metadatastician

Merge Risk: Minimal · up to 5567c

An imported function aliased as main no longer runs when the compiled module is loaded, and it stays callable from root code under that name. A root-declared main still runs as the entry point. No concrete merge-blocking risk was established.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: an imported function bound as main no longer runs automatically. It matches the changeset in lib/module_loader.ml, the regression test, and the changelog entry. It i…
Description check Passed The description states the bug, the fix in lib/module_loader.ml, the new test test_805_imported_main_alias, and the verification steps. It relates directly to the changeset.
Linked Issues check Passed Directly linked issue #805 requires three things. (1) An imported function aliased as main must stay callable under that name and must not run on module load. (2) A root-declared main must keep it…
Out of Scope Changes check Passed The changed files are CHANGELOG.adoc, lib/module_loader.ml, and test/test_module_root_flatten.ml. The loader change is the fix for #805. Its documentation update describes the same rule. The test file…
Full details: Title check

Explanation

The title names the main change: an imported function bound as main no longer runs automatically. It matches the changeset in lib/module_loader.ml, the regression test, and the changelog entry. It includes the issue number, which is acceptable.

Full details: Linked Issues check

Explanation

Directly linked issue #805 requires three things. (1) An imported function aliased as main must stay callable under that name and must not run on module load. (2) A root-declared main must keep its auto-call. (3) A regression test must cover the aliased import, and the existing root-main tests must stay. The PR summary and the reviewed file summaries state that lib/module_loader.ml no longer makes a bound-name main copy. Root references to the alias are rewritten to the module-qualified name. Only a root-declared main stays an entry point. The file at head confirms the surrounding design: declares_main checks only the root program's own TopFn declarations, and the flatten notes say the root's own declarations keep their source names. The test test_805_imported_main_alias covers the alias for Bun-ESM and JS, covers a root main for both backends, and covers Node behaviour for go(). Limitation: the text diff could not be read, because the changed blobs are missing from the checkout. The loader and test behaviour rests on the PR summary and the file read at head, not on a direct diff read.

Full details: Out of Scope Changes check

Explanation

The changed files are CHANGELOG.adoc, lib/module_loader.ml, and test/test_module_root_flatten.ml. The loader change is the fix for #805. Its documentation update describes the same rule. The test file adds the regression test that #805 asks for. The CHANGELOG entry describes the same fix. No change appears to address unrelated behaviour. Limitation: the text diff could not be read, so the exact hunks were not checked line by line. The judgement rests on the PR summary and the changed-file summaries.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 6eb23aa8-0b80-40c0-a162-366037fcc25c
Commits

Reviewing files that changed from the base of the PR and between 83ea829 and 5567c4c.

Files selected for processing: 3

  • CHANGELOG.adoc
  • lib/module_loader.ml
  • test/test_module_root_flatten.ml

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Recent review details

Context from checks skipped due to timeout: 6

  • GitHub Check: CodeRabbit
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: build
  • GitHub Check: coverage-visibility
  • GitHub Check: lint
  • GitHub Check: bench-visibility
Additional comments: 1

lib/module_loader.ml: 1

759-759: Data Integrity & Integration

No supported host contract depends on an imported main alias being exported.

The concern assumes that host code can import or look up an imported function under the name main. The repository does not establish that contract. The only documented host use of a WASM main export treats main as the program entry. Issue #805 restricts that entry point to the root module's own main.

The change still removes the WASM main export that an imported alias previously produced, because pub and game-hook exports apply to the flattened declarations. If a host relied on that export, the change would break it. No inspected consumer does so. The alias stays callable from root source, as the regression test checks.

@sonarqubecloud

Copy link
Copy Markdown

@linear-code
linear-code Bot marked this pull request as ready for review November 25, 2026 23:22
@hyperpolymath hyperpolymath assigned Copilot and unassigned Copilot Nov 25, 2026
@hyperpolymath
hyperpolymath enabled auto-merge (squash) November 25, 2026 23:23
@hyperpolymath
hyperpolymath merged commit e9151de into main Nov 25, 2026
18 checks passed
@hyperpolymath
hyperpolymath deleted the claude/issue-805-main-alias branch November 25, 2026 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants