nest icon indicating copy to clipboard operation
nest copied to clipboard

Dependencies are not resolved when using lazy load transient providers

Open thnam1410 opened this issue 1 year ago • 3 comments
trafficstars

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

thnam1410 avatar Jul 06 '24 14:07 thnam1410

image

somehow it is loosing that reference

repro

I tested with nestjs v9 as well.

micalevisk avatar Jul 06 '24 16:07 micalevisk

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.

KostyaTretyak avatar Jul 07 '24 13:07 KostyaTretyak

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

micalevisk avatar Jul 07 '24 20:07 micalevisk

Let's track this here https://github.com/nestjs/nest/pull/13804

kamilmysliwiec avatar Nov 12 '24 13:11 kamilmysliwiec

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?

mathulbrich avatar Dec 16 '24 23:12 mathulbrich