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

fix(core): always bind global modules when scan for modules #12880

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export class LazyModuleLoader {
const moduleClassOrDynamicDefinition = await loaderFn();
const moduleInstances = await this.dependenciesScanner.scanForModules(
moduleClassOrDynamicDefinition,
[],
[],
true,
);
if (moduleInstances.length === 0) {
// The module has been loaded already. In this case, we must
Expand Down
41 changes: 21 additions & 20 deletions packages/core/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,55 +83,56 @@ export class DependenciesScanner {
}

public async scanForModules(
moduleDefinition:
moduleDef:
| ForwardReference
| Type<unknown>
| DynamicModule
| Promise<DynamicModule>,
scope: Type<unknown>[] = [],
ctxRegistry: (ForwardReference | DynamicModule | Type<unknown>)[] = [],
bindGlobal = false,
): Promise<Module[]> {
const moduleInstance = await this.insertModule(moduleDefinition, scope);
moduleDefinition =
moduleDefinition instanceof Promise
? await moduleDefinition
: moduleDefinition;
ctxRegistry.push(moduleDefinition);

if (this.isForwardReference(moduleDefinition)) {
moduleDefinition = (moduleDefinition as ForwardReference).forwardRef();
const moduleInstance = await this.insertModule(moduleDef, scope);

if (bindGlobal) {
this.container.bindGlobalsToImports(moduleInstance);
}

ctxRegistry.push(await moduleDef);

if (this.isForwardReference(await moduleDef)) {
moduleDef = (moduleDef as ForwardReference).forwardRef();
}

const modules = !this.isDynamicModule(
moduleDefinition as Type<any> | DynamicModule,
moduleDef as Type<any> | DynamicModule,
)
? this.reflectMetadata(
MODULE_METADATA.IMPORTS,
moduleDefinition as Type<any>,
)
? this.reflectMetadata(MODULE_METADATA.IMPORTS, moduleDef as Type<any>)
: [
...this.reflectMetadata(
MODULE_METADATA.IMPORTS,
(moduleDefinition as DynamicModule).module,
(moduleDef as DynamicModule).module,
),
...((moduleDefinition as DynamicModule).imports || []),
...((moduleDef as DynamicModule).imports || []),
];

let registeredModuleRefs = [];
for (const [index, innerModule] of modules.entries()) {
// In case of a circular dependency (ES module system), JavaScript will resolve the type to `undefined`.
if (innerModule === undefined) {
throw new UndefinedModuleException(moduleDefinition, index, scope);
throw new UndefinedModuleException(moduleDef, index, scope);
}
if (!innerModule) {
throw new InvalidModuleException(moduleDefinition, index, scope);
throw new InvalidModuleException(moduleDef, index, scope);
}
if (ctxRegistry.includes(innerModule)) {
continue;
}
const moduleRefs = await this.scanForModules(
innerModule,
[].concat(scope, moduleDefinition),
[].concat(scope, moduleDef),
ctxRegistry,
bindGlobal,
);
registeredModuleRefs = registeredModuleRefs.concat(moduleRefs);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { Global, Module } from '@nestjs/common';
import { expect } from 'chai';
import {
LazyModuleLoader,
Expand All @@ -24,6 +24,18 @@ describe('LazyModuleLoader', () => {
warn() {}
}

@Global()
@Module({
providers: [
{
provide: 'GLOBAL',
useValue: 'GLOBAL',
},
],
exports: ['GLOBAL'],
})
class SomeGlobalModule {}

beforeEach(() => {
const nestContainer = new NestContainer();
const graphInspector = new GraphInspector(nestContainer);
Expand All @@ -32,6 +44,7 @@ describe('LazyModuleLoader', () => {
new MetadataScanner(),
graphInspector,
);
dependenciesScanner.scan(SomeGlobalModule);

const injector = new Injector();
instanceLoader = new InstanceLoader(
Expand All @@ -41,6 +54,7 @@ describe('LazyModuleLoader', () => {
new NoopLogger(),
);
modulesContainer = nestContainer.getModules();

lazyModuleLoader = new LazyModuleLoader(
dependenciesScanner,
instanceLoader,
Expand All @@ -51,6 +65,18 @@ describe('LazyModuleLoader', () => {
describe('load', () => {
const bProvider = { provide: 'B', useValue: 'B' };

@Module({
providers: [
{
provide: 'C',
useFactory: (message: string) => message,
inject: ['GLOBAL'],
},
],
exports: ['C'],
})
class ModuleC {}

@Module({ providers: [bProvider], exports: [bProvider] })
class ModuleB {}

Expand All @@ -76,5 +102,11 @@ describe('LazyModuleLoader', () => {
expect(moduleRef).to.equal(moduleRef2);
});
});
describe('when global modules are defined', () => {
it('should providers of global modules are injected', async () => {
const moduleRef = await lazyModuleLoader.load(() => ModuleC);
expect(moduleRef.get('C')).to.be.string('GLOBAL');
});
});
});
});