nest
nest copied to clipboard
Dependencies are not resolved when using lazy load transient providers
Is there an existing issue for this?
- [X] I have searched the existing issues
Current behavior
When I lazy loading and resolve module/provider with transient provider and some singleton sub providers. The first exec function (API call) it works fine. But the second exec of itself, the injected providers (in this case is "B") is undefined.
When I tried to cache the result of .load() and reuse it. It works, but I'm not sure if there is a issue behind the screen that I'd have not known it yet.
Minimum reproduction code
https://stackblitz.com/edit/nestjs-typescript-starter-nzzyej?file=src%2Fapp.controller.ts
Steps to reproduce
Not-working endpoint: /not-work
Working endpoint: /work
Expected behavior
I expect that when using resolve, it creates a transient provider as my use case and all sub-dependencies as "Singleton" won't be re-created
Package
- [ ] I don't know. Or some 3rd-party package
- [ ]
@nestjs/common - [ ]
@nestjs/core - [ ]
@nestjs/microservices - [ ]
@nestjs/platform-express - [ ]
@nestjs/platform-fastify - [ ]
@nestjs/platform-socket.io - [ ]
@nestjs/platform-ws - [ ]
@nestjs/testing - [ ]
@nestjs/websockets - [ ] Other (see below)
Other package
No response
NestJS version
No response
Packages versions
@nestjs/core: 10.3.0
Node.js version
20.10.0
In which operating systems have you tested?
- [X] macOS
- [ ] Windows
- [ ] Linux
Other
No response
By the way, in repro, the /work route is identical to the /not-work route. Therefore, you can work with any of them. The first request is successful, the second - unsuccessful.
this seems to be a bug with lazyModuleLoader#load when the module is resolved already (cached) because it works as expected when lazyModuleLoader#load returns a new instance every time
Let's track this here https://github.com/nestjs/nest/pull/13804
I discovered in my project that the same issue happens with Scope.REQUEST. Maybe we could apply the same solution for request scoped providers. Can I open a PR for that @kamilmysliwiec?