inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.02k stars 712 forks source link

Circular Path detection falsely blames multi-inject #1515

Open timvahlbrock opened 1 year ago

timvahlbrock commented 1 year ago

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)

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 commented 1 year ago

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.