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.69k stars 7.63k forks source link

Silently crashing(?) when using promises to create a locking mechanism between independent modules via factory provider #13679

Closed micalevisk closed 5 months ago

micalevisk commented 5 months ago

Is there an existing issue for this?

Current behavior

demo

I found that it is getting stuck at await metatype bellow in this case. I've no clue on why is this nor if this is the expected behavior due to some limitation.

https://github.com/nestjs/nest/blob/9b1efbc823f01737ed0abe0291632db18272e646/packages/core/injector/compiler.ts#L17-L20

I'd like this to work because I'm trying to conditionally loading a module depending on a provider's value, which is why I need to use factory providers

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-nwqhob?file=src%2Fapp.module.ts

Steps to reproduce

  1. go to https://stackblitz.com/edit/nestjs-typescript-starter-nwqhob?file=src%2Fapp.module.ts
  2. run npm i on their terminal
  3. run npm start on their terminal and see that the process just crash right away

Expected behavior

I expect it to work just like it works when using a value provider instead of a factory provider. So I should see the following output:

image

Or, if this is limitation, we should at least see some error (if there's any) instead of stopping the process silently

Package

Other package

No response

NestJS version

10.3.9

Packages versions

[System Information]
OS Version     : Linux 5.0 (X11; Ubuntu)
NodeJS Version : v18.20.3
NPM Version    : 10.2.3 

[Nest CLI]
Nest CLI Version : 10.3.2 

[Nest Platform Information]
platform-express version : 10.3.9
schematics version       : 10.1.1
testing version          : 10.3.9
common version           : 10.3.9
core version             : 10.3.9
cli version              : 10.3.2

Node.js version

No response

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 5 months ago

I'm not sure if this is related to NestJS tbh - this doesn't sound like a pattern we'd like to support at the framework level. My hunch is that your process crashes because you're entering a circular loop that never exists and runtime seeing that just terminates it, but I might be wrong - I'd have to spend more time looking into this

I'd like this to work because I'm trying to conditionally loading a module depending on a provider's value, which is why I need to use factory providers

This doesn't sound like something you should be doing. If you want to load a module dynamically, use lazy modules. Otherwise, the modules graph is constructed before providers are resolved so this won't ever work without magic tricks that your app shouldn't rely on