question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Circular dependency resulting in infinite loop

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
pahencommented, May 15, 2018

Sorry for my inactivity. I’ll take a look at this really soon 😃

1reaction
wjohnstocommented, May 21, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Solving Circular Dependency infinite loop using counter?
It looks like you are repeatedly calling cancelChildTree(Parent parent) on the same object. If so, what prevents you from using some ...
Read more >
An infinite loop occurs in a hierarchy that contains a circular ...
Fixes an issue in which an infinite loop occurs when you load a batch of attributes' values to a hierarchy in SQL Server...
Read more >
[#RUNTIME-7] Circular dependency causes infinite loop. - ASF JIRA
This results in the establishment of an assembled model that may contain circular references. This introduces additional requirements in the activation ...
Read more >
Dependency loops and how to avoid them – Support
In the event that you inadvertently create a dependency loop, there's no need to worry. Simply go into the automations center and turn...
Read more >
How to Solve the Infinite Loop of React.useEffect()
The infinite loop is fixed by correct management of the useEffect(callback, dependencies) dependencies argument.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found