Skip to content

fix: resolve named exports through cyclic barrels - #10

Merged
webpro merged 4 commits into
webpro-nl:mainfrom
everton-dgn:fix/investigate-small-bug-27a4db04
Nov 23, 2026
Merged

webpro merged 4 commits into
webpro-nl:mainfrom
everton-dgn:fix/investigate-small-bug-27a4db04

Conversation

@everton-dgn

Copy link
Copy Markdown
Contributor

Stop revisiting active export states when tracing a barrel cycle, so a later branch can still resolve the named export. Add a regression for two barrels that re-export each other before the leaf export.

Validated with 182 tests, build, lint, formatting and installed-check.

Stop revisiting active export states when tracing a barrel cycle, so a later branch can still resolve the named export. Add a regression for two barrels that re-export each other before the leaf export.

Validated with 182 tests, build, lint, formatting and installed-check.

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Éverton! This fixes a real crash. On main, two barrels that star re-export each other send traceExport into unbounded recursion, and the process dies with a heap out-of-memory error when the cycle is reached before the export is found. With this change every cyclic layout I tried resolves to the leaf file, including a self cycle, a three-file cycle, and aliased and default exports behind a cycle. Projects without cycles come out unchanged. The new test fits in with its neighbors and fails on main.

One optional simplification is noted inline.

Comment thread src/rewriter.ts Outdated
Comment on lines +224 to +225
const found = await traceExport(effectiveName, importItem, targetFile.exports, rewrites, ctx, added, visiting);
visiting.delete(visitKey);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: without the delete, the set records every state already tried, which is enough to stop the cycle and visits each file once per name. As written, a state that failed is tried again from every other path, so densely connected cycles get expensive. Eleven barrels that all star re-export each other take about 5 seconds here, against 0.2 seconds without the line and with the same output. canFlattenStarExports and traceStarExports further down keep their visited entries too. The tests pass either way.

Suggested change
const found = await traceExport(effectiveName, importItem, targetFile.exports, rewrites, ctx, added, visiting);
visiting.delete(visitKey);
const found = await traceExport(effectiveName, importItem, targetFile.exports, rewrites, ctx, added, visiting);

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up the suggestion. The set is now a plain visited set, and the dense cycle that took about 5 seconds runs in 0.2. I pushed one commit to your branch that replaces the second test: its fixture did not type-check, and the new one does and covers the alternate route, a barrel reached again under another name. Ready to merge.

@webpro
webpro merged commit 7189aaf into webpro-nl:main Nov 23, 2026
5 checks passed
@webpro

webpro commented Nov 24, 2026

Copy link
Copy Markdown
Member

🚀 This pull request is included in v1.2.0. See Release 1.2.0 for release notes.

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.

2 participants