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

Rethink dependency cycles #75

Open
Profpatsch opened this issue Jul 1, 2022 · 0 comments
Open

Rethink dependency cycles #75

Profpatsch opened this issue Jul 1, 2022 · 0 comments

Comments

@Profpatsch
Copy link
Owner

yarn/npm modules can have circular dependencies on each other.

Wtf, you might think, how is that even possible?

When A.js loads B.js, then B.js in turn loads C.js. At this point, C.js tries to load A.js. In order to prevent an infinite loop, an unfinished copy of the A.js exports object is returned to the C.js module. C.js then finishes loading, and its exports object is provided to the A.js module.
(source)

This is incredibly worrying in many ways, but 👁️👄👁️

So my thinking here is:

  • In case both modules import each other directly at the top of each module one module will be empty anyway, so removing the cycle (in the right way) should be semantically equivalent
  • Not sure if that holds for bigger cycles? Probably.
  • In every other case, it sucks.

So there’s multiple ways we can solve for it here:

  1. Unconditionaly remove cycles (current behaviour). Turn the graph into a dag, then work on the dag as usual. This might be ok in many cases, maybe js devs are just lazy and don’t understand how dependencies work, so it somehow works even though it’s not what they wanted to do semantically.
  2. Maybe they actually want a fixpoint over their modules (and who doesn’t love fix points cough nixpkgs cough), and are using them in a ‘smart’ way that doesn’t lead to infinite recursions (or broken code with npm’s resolution mechanism).
    In that case we need to change yarn2nix, to find all strongly connected components in the graph, and merge them together them in a single derivation in a smart way.

At least I have written down the problem now, so I can sleep well again.

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

No branches or pull requests

1 participant