Circular dependency resulting in infinite loop
See original GitHub issueI am working on a server with roughly 400 files, using GraphQL which lends itself to circular dependencies. I’m trying to use madge to prevent this as best I can, but I’m running into an issue where it appears madge ends up in an infinite loop. I’ve traced the issue, and fixed it for my purposes so I wanted to bring it to light here and see if it’s a bug or if I’m doing something wrong.
My project is structured so that I have a bunch of component
directories, each with an index.js
that exports all the modules in the component. So something like this:
- component
- controller.js
- model.js
- index.js
- graphql
- type.js
- mutation.js
- query.js
- index.js
I’ve traced the issue down to tree.js#L169. It appears in the case of my project depTree
(which is the result of the call to dependency-tree
has in itself circular references. I’m not sure if this is intended by dependency-tree
, but I’m assuming it is. This would be fine if the convertTree
function had a short-circuit in the event that there was a circular reference, but I can’t find where it does that. I’m assuming that it is intended for tree
to be that object. When I change the function to be the following it fixes the issues:
convertTree(depTree, tree, pathCache) {
for (const key in depTree) {
const id = this.processPath(key, pathCache);
if (!tree[id]) {
tree[id] = [];
for (const dep in depTree[key]) {
tree[id].push(this.processPath(dep, pathCache));
}
this.convertTree(depTree[key], tree, pathCache);
}
}
return tree;
}
Basically, I only call to convertTree
in the event that id
isn’t found in the flat tree
. This seems like it might be the intended functionality, but I’m not sure exactly so I thought I would post here rather than make a PR.
Has anyone else run across this same issue?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:10 (3 by maintainers)
Sorry for my inactivity. I’ll take a look at this really soon 😃
I was able to verify that there were multiple circular dependencies. I can try to work up the simplest example I can, as what I have currently is a little bit complex for a gist.