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

Improve displaying of circular dependency error #11299

Merged
merged 4 commits into from
Nov 4, 2024

Conversation

andriy-dmytruk
Copy link
Contributor

@andriy-dmytruk andriy-dmytruk commented Nov 1, 2024

For some classes ClassA, ClassB, ClassC and ClassD that have a dependency cycle, the current message is:

Failed to inject value for field [propA] of class: io.micronaut.inject.failures.ctorcirculardependency.MyClassB
 
Message: Circular dependency detected
Path Taken: 
new MyClassD(MyClassB propB) --> new MyClassD([MyClassB propB]) --> MyClassB.propA --> new MyClassA([MyClassC propC]) --> new MyClassC([MyClassB propB])
^                                                                                                                                                     |
|                                                                                                                                                     |
|                                                                                                                                                     |
+-----------------------------------------------------------------------------------------------------------------------------------------------------+

After the change it would be:

Failed to inject value for field [propA] of class: io.micronaut.inject.failures.ctorcirculardependency.MyClassB
 
Message: Circular dependency detected
Path Taken: 
new MyClassD(MyClassB propB)
      \---> new MyClassD([MyClassB propB])
            \---> MyClassB.propA
                  ^  \---> new MyClassA([MyClassC propC])
                  |        \---> new MyClassC([MyClassB propB])
                  |              |
                  +--------------+

This improves it by:

  • Showing the actual cycle, as the cycle does not always include the first element in the path. In this case it is ClassB->ClassA->ClassC->ClassB, so ClassD is ommitted
  • Displaying each entry on a new line to improve readability. This is important for classes with long class names and multiple injection parameters. I changed the test class names to longer, as A, B, C did not seem realistic.

Since in most cases classnames are quite long, it seems better to show each on a new line. This is also better for classes that have many constructor arguments that need to be displayed.
Since circles are not guaranteed to return to the first path element, actual last circle element needs to be found
@andriy-dmytruk
Copy link
Contributor Author

The non-cyclic path display could be changed to a similar, as well, but I am not sure if that would be an improvement there.

@graemerocher
Copy link
Contributor

looks great! Just have to adjust the original tests

@graemerocher
Copy link
Contributor

Another rendering anomaly that may be worth looking at is when you have a Factory bean in the middle that causes the loop I am not sure we are rendering the factory name correctly

@andriy-dmytruk
Copy link
Contributor Author

Another rendering anomaly that may be worth looking at is when you have a Factory bean in the middle that causes the loop I am not sure we are rendering the factory name correctly

The display in the test I added looks good. But that's a great idea to add the test, I caught an edge case in my implementation where the circle did not point to correct element.

Copy link

sonarcloud bot commented Nov 4, 2024

@graemerocher graemerocher added the type: improvement A minor improvement to an existing feature label Nov 4, 2024
@graemerocher graemerocher merged commit 01dea19 into 4.7.x Nov 4, 2024
21 checks passed
@graemerocher graemerocher deleted the andriy/circular-dependency-printing branch November 4, 2024 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants