question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Classes exported under the same name and aliased are confused as the same token by the dependency injection mechanism

See original GitHub issue

Bug Report

Input Code

Consider the following minimal project:

// provider1.ts
export class Token {};
// provider2.ts
export class Token {};
// main.module.ts
import {Module} from "@nestjs/common";
import {Token as Token1} from "./provider1";
import {Token as Token2} from "./provider2";

@Module({
    providers: [
        Token1,
        Token2,
        {
            provide: 'foo',
            useFactory: (injectedToken1, injectedToken2) => {
                console.warn(Token1 === Token2); // false, expected
                console.warn(injectedToken1 === injectedToken2); // true, unexpected
                console.warn(injectedToken1 instanceof Token2); // true, unexpected
                console.warn(injectedToken2 instanceof Token2); // true, expected
            },
            inject: [
                Token1,
                Token2
            ]
        }
    ]

})
export class MainModule {}

Here, provider1.ts and provider2.ts both export a class under the same name - Token. When imported by the module, they are aliased respectively to Token1 and Token2. These aliases are then used as provider keys.

When both tokens are injected into the foo provider factory, both arguments of the factory are strictly equals: they are both the same instance of Token2. It means that the dependency injection mechanism instanciate a singleton from Token2 and then assign this singleton to both the tokens Token1 and Token2.

Expected behavior

Token1 and Token2 are different classes. They are different tokens and are not equal, nor loosely, neither strictly.

It is expected that they are not confused by the dependency injection mechanism of Nest.

Possible Solution

None.

And this is a big issue because there is no way for a developer to control how a class is exported by a module the application depends on. When two different modules export the same class, it becomes impossible to alias them and use these aliases as provider keys.

Note that this also impacts functions used as factory.

Environment

Nest version: 7.6.17

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kamilmysliwieccommented, May 19, 2021

This has been fixed in v8 https://github.com/nestjs/nest/pull/6349 (which I’m hoping to release within ~1 month).

0reactions
ericmorandcommented, May 19, 2021

@kamilmysliwiec

core: use class references as providers/controllers/injectables keys instead of their names

👍

Amazing, thanks a lot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Learning the Basics - Gradle User Manual
This process is called dependency resolution. You can find a detailed discussion in How Gradle downloads dependencies. Once resolved, the resolution mechanism ......
Read more >
Core Technologies - Spring
These names can be equivalent aliases to the same bean and are useful for some ... To use this mechanism, leave the class...
Read more >
XSL Transformations (XSLT) Version 3.0 - W3C
Abstract. This specification defines the syntax and semantics of XSLT 3.0, a language designed primarily for transforming XML documents into ...
Read more >
Angular Interview Questions — You Must Know (2022) - Medium
Angular comes with its own dependency injection framework for resolving dependencies( services or objects that a class needs to perform its ...
Read more >
Hibernate ORM 5.2.18.Final User Guide - Red Hat on GitHub
Hibernate not only takes care of the mapping from Java classes to database ... To avoid any confusion with the annotation that marks...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found