nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.09k stars 7.56k forks source link

Dependencies are not resolved when using lazy load transient providers #13761

Open thnam1410 opened 3 months ago

thnam1410 commented 3 months ago

Is there an existing issue for this?

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

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?

Other

No response

micalevisk commented 3 months ago

image

somehow it is loosing that reference

repro

I tested with nestjs v9 as well.

KostyaTretyak commented 2 months ago

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.

micalevisk commented 2 months ago

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