Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix circular dependency #458

Closed
wants to merge 1 commit into from
Closed

Conversation

eigendude
Copy link

@eigendude eigendude commented Sep 3, 2021

Description

This PR fixes a circular dependency that has been affecting the rollup ecosystem for a long time. The fix is simple, as the circularity comes from a single type test that can be simply replaced with a boolean. (And the boolean already exists, it's just exposed to the Duplex child class now).

The root cause is due to rollup's handling of hoisting. Duplex inherits from Readable and Writable, but Readable and Writable check for Duplex-ity, ergo circular. As a result, when Duplex inherits from Readable/Writable, these two classes are hoisted but not yet defined.

The fix is simple: Replace the Duplex-ity test with a boolean, set by Duplex when it constructs its base classes.

This boolean actually already exists locally in both Readable and Writable, so it makes sense to use it to bust the circular dependency.

Related PRs

Closes #348
Closes calvinmetcalf/rollup-plugin-node-builtins#31

At the time `require('inherits')(Duplex, Readable)` is called,
Readable is hoisted but not yet defined due to a circular dependency.

Because the only uses of Duplex by the base classes are checking for
if they themselves are a Duplex, we can replace typeof with a boolean.

This boolean actually already exists in Writable, so it makes sense
to use it elsewhere to solve the circular dependency.
@eigendude
Copy link
Author

Is there any way to get this reviewed? Thanks.

@fend25
Copy link

fend25 commented Mar 22, 2022

@mcollina hi!
Could you please review this PR? It is unbelievably helpful and long awaited. Thank you.

@mcollina mcollina closed this May 23, 2022
@eigendude
Copy link
Author

Is there a reason for the closure?

@mcollina
Copy link
Member

It's fixed in the upcoming v4 release.

@eigendude
Copy link
Author

Nice!!! Mind linking to the commit?

Also, I ported this patch to rollup-plugin-node-polyfills: ionic-team/rollup-plugin-node-polyfills#36

@eigendude eigendude deleted the fix-circular branch December 23, 2022 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants