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

Circular Path detection falsely blames multi-inject #1515

Open
timvahlbrock opened this issue Jun 9, 2023 · 1 comment
Open

Circular Path detection falsely blames multi-inject #1515

timvahlbrock opened this issue Jun 9, 2023 · 1 comment

Comments

@timvahlbrock
Copy link

When there is a circular dependency, inversify tries to reconstruct the circular path. However, if a multiInject is located in the requests that are evaluated before the actual circular dependencies are reached, inversify assumes the circular dependency there.

Expected Behavior

Inversify correctly identifies the actual circular dependency.

Current Behavior

Inversify blames the multiInject dependency path.

Possible Solution

From my point of view. there seem to be three approaches to tackle this:

  1. Multiple multi-injects appear - for whatever reason - in one dependency chain. Maybe this is how multi-injects are implemented. Either this is a bug that needs to be fixed, or...
  2. ...an alternate implementation is found.
  3. alreadyDependencyChain checks if a dependency is already chain only by the service identifier - which is equal for multi injects.

Steps to Reproduce (for bugs)

  • git clone git@github.com:timvahlbrock/inversify-circular-multi-inject.git
  • cd inversify-circular-multi-inject
  • npm install
  • Run with npc ts-node run.ts and see inversify blame the multi-inject for the circular dependency.
  • Remove the multi-inject from the dependencies, run again, and see inversify show the correct circular path.
  • Add the multi-inject again, remove the circular dependency and see that the script runs through and there is absolutely no problem with the multi inject.

Context

The error message shown by Inversify is misguiding.

Your Environment

Stack trace

Error: Circular dependency found: Top --> multi-inject --> multi-inject
    at /Users/timvahlbrock/Downloads/tmp3/node_modules/inversify/src/utils/serialization.ts:95:13
    at Array.forEach (<anonymous>)
    at circularDependencyToException (/Users/timvahlbrock/Downloads/tmp3/node_modules/inversify/src/utils/serialization.ts:92:25)
    at /Users/timvahlbrock/Downloads/tmp3/node_modules/inversify/src/utils/serialization.ts:97:7
    at Array.forEach (<anonymous>)
    at circularDependencyToException (/Users/timvahlbrock/Downloads/tmp3/node_modules/inversify/src/utils/serialization.ts:92:25)
    at plan (/Users/timvahlbrock/Downloads/tmp3/node_modules/inversify/src/planning/planner.ts:246:36)
    at /Users/timvahlbrock/Downloads/tmp3/node_modules/inversify/src/container/container.ts:623:25
    at Container._get (/Users/timvahlbrock/Downloads/tmp3/node_modules/inversify/src/container/container.ts:574:37)
    at Container._getButThrowIfAsync (/Users/timvahlbrock/Downloads/tmp3/node_modules/inversify/src/container/container.ts:580:25)
@timvahlbrock
Copy link
Author

I just realised: in the sample project you can even just change the order of the dependencies and the error message changes from incorrect to correct.

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