Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConfigurableModuleBuilder "alwaysTransient: false" (@default) doesn't work #13076

Closed
3 of 15 tasks
StiliyanKushev opened this issue Jan 21, 2024 · 20 comments
Closed
3 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@StiliyanKushev
Copy link

StiliyanKushev commented Jan 21, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

The console log is printed only once, unless alwaysTransient is set to true.

// my-custom-module.ts

import { Inject, Module } from '@nestjs/common';

import { ConfigurableModuleBuilder } from '@nestjs/common';

export const {
  ConfigurableModuleClass,
  MODULE_OPTIONS_TOKEN: MY_CUSTOM_MODULE_OPTIONS,
} = new ConfigurableModuleBuilder<{
  baseUrl?: string;
}>().build();

@Module({})
export class MyCustomModule extends ConfigurableModuleClass {
  constructor(
    @Inject(MY_CUSTOM_MODULE_OPTIONS)
    private readonly options,
  ) {
    console.log(options);
    super();
  }
}

// app.module.ts
@Module({
  imports: [
    MyCustomModule.register({ baseUrl: 'https://google.com' }),
    MyCustomModule.register({ baseUrl: 'https://google1.com' }),
    MyCustomModule.register({ baseUrl: 'https://google2.com' }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Steps to reproduce

No response

Expected behavior

Expecting the console log to run 3 times because the options are different in the providers array (as stated in the official nestjs advanced concepts course).

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

10.3.0

Packages versions

[System Information]
OS Version     : Linux 6.1.69-1-MANJARO
NodeJS Version : v20.8.0
NPM Version    : 10.1.0 

[Nest CLI]
Nest CLI Version : 10.3.0 

[Nest Platform Information]
platform-express version : 10.3.0
mapped-types version     : 2.0.4
schematics version       : 10.1.0
testing version          : 10.3.0
common version           : 10.3.0
core version             : 10.3.0
cli version              : 10.3.0

[Warnings]
The following packages are not in the same minor version
This could lead to runtime errors
* Under version 10.3
- @nestjs/platform-express 10.3.0
- @nestjs/common 10.3.0
- @nestjs/core 10.3.0
* Under version 10.1
- @nestjs/schematics 10.1.0

Node.js version

v20.8.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@StiliyanKushev StiliyanKushev added the needs triage This issue has not been looked into label Jan 21, 2024
@micalevisk
Copy link
Member

isn't this pretty much what the docs of alwaysTransient says here:

/**
* Indicates whether module should always be "transient", meaning,
* every time you call the static method to construct a dynamic module,
* regardless of what arguments you pass in, a new "unique" module will be created.
*
* @default false
*/
alwaysTransient?: boolean;

@micalevisk micalevisk closed this as not planned Won't fix, can't repro, duplicate, stale Jan 21, 2024
@StiliyanKushev
Copy link
Author

Well, here's the thing though, the expected behavior I described is not really documented, but is clearly mentioned in one of the official nestjs courses. What I'm saying is that, alwaysTransient set to true actually works as expected, but when set to false (by default), it supposedly (as shown in the course) should still act as if alwaysTransient is set to true for providers that are the same but have different options passed (such as in my example), due to their "internal hash" being different.

@StiliyanKushev
Copy link
Author

StiliyanKushev commented Jan 22, 2024

The version used in the course seems to be v7.1.2 (edit: maybe they upgraded versions between lessons, not entirely sure)

@micalevisk
Copy link
Member

micalevisk commented Jan 22, 2024

@StiliyanKushev yeah, got you

please feel free to improve the docs at https://github.com/nestjs/docs.nestjs.com

the version shouldn't matter here

@StiliyanKushev
Copy link
Author

Well, I'm a bit worried that this is an issue? Either a bug, or outdated course information, but the fact it worked differently before makes me believe it's the former.

Just to recap, the course suggests that the above logs should execute 3 times (due to all provders instantiating because of the different internal hashes or whatever), however (on the latest nest version at least) it only executes once unless you specifically enable alwaysTransient.

@micalevisk
Copy link
Member

Just to recap, the course suggests that the above logs should execute 3 times

are you sure?

at 8:25
image

image

@StiliyanKushev
Copy link
Author

That's the correct video you're referencing, but it's the wrong timestamp.
I can provide the screenshots in a sec.

@StiliyanKushev
Copy link
Author

at 9:08
image
image

@micalevisk
Copy link
Member

micalevisk commented Jan 22, 2024

ok, I just saw what you're talking about

but please share a minimum reproduction as well

@micalevisk micalevisk reopened this Jan 22, 2024
@StiliyanKushev
Copy link
Author

Okay, I'll make a repo in a minute. I can also test with different nest versions and will report up until what version it works as expected.

@micalevisk
Copy link
Member

check out the https://github.com/nestjs/nest/releases

I don't think that that behavior have changed but I can be wrong

@StiliyanKushev
Copy link
Author

Yeah, already searched in the releases, wasn't mentioned. (Another reason I suspect this to be a bug).

@StiliyanKushev
Copy link
Author

Here you go, hope that's enough :)

@StiliyanKushev
Copy link
Author

Notice that it works as expected if I downgrade to version 9 for example.

image

@micalevisk
Copy link
Member

seems to be a regression introduced on @nestjs/common@10.3.0

@StiliyanKushev
Copy link
Author

StiliyanKushev commented Jan 22, 2024

Actually, seems to be a semi-recent bug in the latest version of @nestjs/common@10.3.0.

image

EDIT: yeah

@H4ad
Copy link
Contributor

H4ad commented Jan 22, 2024

@micalevisk Yeah, this is a bug introduced by my PR, alwaysTransient works because of: https://github.com/nestjs/nest/pull/12753/files#diff-22f1ab07f43abbd45f270de55b6cd35da90c02fe92cee99f67d51a37b85002c8R209-R220 which makes the module unique.

That piece of code worked before because we used those objects to differentiate each module, if he use the async version, then it will have the same issue.

We can basically revert the change for that module only, the easiest/quickest fix, the other changes we don't need to revert.

We can also go back to the conversation started at #12898, this issue could be fixed using that feature.

@StiliyanKushev
Copy link
Author

Great! Additionally I'd say this can be better documented for future reference as to me it seems to be a somewhat crucial feature in certain scenarios.

@kamilmysliwiec
Copy link
Member

We can basically revert the change for that module only, the easiest/quickest fix, the other changes we don't need to revert.

Would you like to create a PR for this?

@kamilmysliwiec
Copy link
Member

#13083

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

4 participants