Wednesday, September 18, 2013

Needs More Dojo 0.5.1: Cyclic Dependency Detection

So 0.5.1 is a "minor" release. This means that it should be mostly bug-fixes and some minor enhancements. Well in this release I decided to add a pretty big feature (it was a feature request actually). The feature in question is cyclic dependency detection.

In dojo, cyclic dependencies present a problem. If you have a cyclic dependency, one of the modules involved will be resolved to {}, an empty object. Oops. This is to prevent further recursion as the AMD loader goes down the dependency graph. Unfortunately this can lead to subtle bugs and can be difficult to track down. I remember struggling when I was first learning dojo and ran into this problem, luckily a colleague was able to describe the issue.

Anyway, in Needs More Dojo I've added the ability to scan for cyclic dependencies. So now you can run a scan on your project sources, and you get a tool window like this:

Scanning for cyclic dependencies.
All it's doing is navigating the dependency graph and checking for cycles. It also counts the number of times a particular module has been involved in a cycle and displays the count next to it. In my simple example, all of the modules listed appear once. However, if you had one module appearing more than another one, it's more likely the culprit causing the cycle.

This appears as an action in the code menu that you can run. I've also added an inspection that will run in the background and highlight modules that are involved in cyclic dependency graphs like this:

Cyclic dependencies flagged as errors
In this example, the inspection has detected a cyclic dependency and displays it as an error. You can also see that the dependency in question (ProjectData) is flagged as unused. So there's an unnecessary cycle in this project.

The inspection is disabled by default. I did this because I didn't want large projects getting performance hits, as the inspection has to build a graph of dependencies for each file.

Expect 0.5.1 to be released in a couple of weeks.

No comments:

Post a Comment