Skip to content

Conversation

@robobun
Copy link
Collaborator

@robobun robobun commented Aug 15, 2025

Summary

Adds a regression test for issue #21642 where the bundler generates circular async dependencies that cause runtime errors.

Problem

The bundler currently generates invalid JavaScript when modules have circular dependencies with async imports. Specifically:

  1. e0.ts and e1.ts are entry points
  2. e2.ts has circular dependencies with async imports from e0
  3. The bundler generates code where init_e0() is called before init_e0 is defined
  4. This results in "TypeError: undefined is not an object (evaluating 'init_e0().then')" at runtime

Test Plan

  • ✅ Added comprehensive regression test that reproduces the exact issue
  • ✅ Test is marked as TODO since the issue is not yet fixed
  • ✅ Test validates both bundling success and runtime execution
  • ✅ Includes specific checks for initialization order errors

Next Steps

The proper fix requires implementing the TODO comment in LinkerContext.zig:1162-1164:

"This should be changed to store a promise and await all stored promises after all imports but before any code."

This is a significant architectural change that needs careful consideration of:

  • Promise storage and resolution order
  • Circular dependency detection and handling
  • Backward compatibility with existing bundling behavior

Related

🤖 Generated with Claude Code

This adds a test case that reproduces issue #21642 where the bundler
generates circular async dependencies that cause init functions to be
called before they are defined, resulting in "undefined is not an object"
runtime errors.

The test is marked as TODO since the issue is not yet fixed. The proper
fix requires implementing the TODO comment in LinkerContext.zig to store
promises and await them after all imports but before module code execution.

Test case reproduces the exact scenario from the issue:
- e0.ts and e1.ts are entry points
- e2.ts has circular dependencies with async imports
- Results in generated code where init_e0() is called before init_e0 is defined

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@robobun
Copy link
Collaborator Author

robobun commented Aug 15, 2025

Updated 10:55 PM PT - Aug 14th, 2025

❌ Your commit 84b17065 has some failures in Build #23061


🧪   To try this PR locally:

bunx bun-pr 21886

That installs a local version of the PR into your bun-21886 executable, so you can run:

bun-21886 --bun

@RiskyMH
Copy link
Member

RiskyMH commented Aug 15, 2025

lol you didnt even fix anything

@RiskyMH RiskyMH closed this Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bundler: Bundler can emit self importing modules

3 participants