inversify / InversifyJS

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

Circular dependency on symbols fails too late #772

Closed krzkaczor closed 6 years ago

krzkaczor commented 6 years ago

If there is a circular dependency on symbols and @inject is called with undefined, error is thrown too late — during dependency resolution from container (container.get).

Expected Behavior

Inversify should early realize that undefined is injected and throw an error. There should be a possibility to provide a function that would lazily return symbol which solves the problem of circular dependency.

Current Behavior

The error is thrown but too late. Related: https://github.com/inversify/InversifyJS/issues/685

Possible Solution

https://github.com/inversify/InversifyJS/blob/master/src/annotation/inject.ts#L7 here on before returning function should be a guard check for undefined. I don't believe that anyone would inject undefined on purpose. The final solution would be to allow using function lazily returning symbol.

Steps to Reproduce (for bugs)

repro

Context

My real problem is that the error is thrown too late. This bug slipped through all my tests (both unit and integration) and was triggered only when the user would click the specific button in my application (we use inversify with react) which would kick action with few dependencies resolved by inversify.

Related: https://github.com/inversify/InversifyJS/issues/685

@remojansen I am happy to work on PR, I just want to discuss possible solutions first.

remojansen commented 6 years ago

Closing this issue because it has been fixed by #774 and it has already been released.