Skip to content

Commit f126f8d

Browse files
clydindgp1130
authored andcommittedMar 14, 2025
fix(@schematics/angular): ensure module discovery checks for an NgModule decorator
The Angular module discovery logic previously did not check for the presence of a potential `@NgModule` decorator. This can cause false positive results for files with a module-like naming pattern.
1 parent 29a58e5 commit f126f8d

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed
 

‎packages/schematics/angular/utility/find-module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path
5959
);
6060

6161
for (const sc of candidateFiles) {
62-
if (host.exists(sc)) {
62+
if (host.exists(sc) && host.readText(sc).includes('@NgModule')) {
6363
return normalize(sc);
6464
}
6565
}

‎packages/schematics/angular/utility/find-module_spec.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -92,32 +92,41 @@ describe('find-module', () => {
9292
options = { name: 'foo' };
9393
});
9494

95-
it('should find a module', () => {
96-
tree.create('/projects/my-proj/src/app.module.ts', '');
95+
it('should find a module with a decorator', () => {
96+
tree.create('/projects/my-proj/src/app.module.ts', '@NgModule');
9797
options.module = 'app.module.ts';
9898
options.path = '/projects/my-proj/src';
9999
const modPath = findModuleFromOptions(tree, options) as string;
100100
expect(modPath).toEqual('/projects/my-proj/src/app.module.ts');
101101
});
102102

103+
it('should not find module-like file without a decorator', () => {
104+
tree.create('/projects/my-proj/src/app.module.ts', '');
105+
options.module = 'app.module.ts';
106+
options.path = '/projects/my-proj/src';
107+
expect(() => findModuleFromOptions(tree, options) as string).toThrowError(
108+
/Specified module 'app.module.ts' does not exist/,
109+
);
110+
});
111+
103112
it('should find a module when name has underscore', () => {
104-
tree.create('/projects/my-proj/src/feature_module/app_test.module.ts', '');
113+
tree.create('/projects/my-proj/src/feature_module/app_test.module.ts', '@NgModule');
105114
options.path = '/projects/my-proj/src';
106115
options.name = 'feature_module/new_component';
107116
const modPath = findModuleFromOptions(tree, options) as string;
108117
expect(modPath).toEqual('/projects/my-proj/src/feature_module/app_test.module.ts');
109118
});
110119

111120
it('should find a module when name has uppercase', () => {
112-
tree.create('/projects/my-proj/src/featureModule/appTest.module.ts', '');
121+
tree.create('/projects/my-proj/src/featureModule/appTest.module.ts', '@NgModule');
113122
options.path = '/projects/my-proj/src';
114123
options.name = 'featureModule/newComponent';
115124
const modPath = findModuleFromOptions(tree, options) as string;
116125
expect(modPath).toEqual('/projects/my-proj/src/featureModule/appTest.module.ts');
117126
});
118127

119128
it('should find a module if flat is true', () => {
120-
tree.create('/projects/my-proj/src/module/app_test.module.ts', '');
129+
tree.create('/projects/my-proj/src/module/app_test.module.ts', '@NgModule');
121130
options.path = '/projects/my-proj/src';
122131
options.flat = true;
123132
options.name = '/module/directive';
@@ -126,7 +135,7 @@ describe('find-module', () => {
126135
});
127136

128137
it('should find a module in a sub dir', () => {
129-
tree.create('/projects/my-proj/src/admin/foo.module.ts', '');
138+
tree.create('/projects/my-proj/src/admin/foo.module.ts', '@NgModule');
130139
options.name = 'other/test';
131140
options.module = 'admin/foo';
132141
options.path = '/projects/my-proj/src';
@@ -135,7 +144,7 @@ describe('find-module', () => {
135144
});
136145

137146
it('should find a module in a sub dir (2)', () => {
138-
tree.create('/projects/my-proj/src/admin/foo.module.ts', '');
147+
tree.create('/projects/my-proj/src/admin/foo.module.ts', '@NgModule');
139148
options.name = 'admin/hello';
140149
options.module = 'foo';
141150
options.path = '/projects/my-proj/src';
@@ -144,7 +153,7 @@ describe('find-module', () => {
144153
});
145154

146155
it('should find a module using custom ext', () => {
147-
tree.create('/projects/my-proj/src/app_module.ts', '');
156+
tree.create('/projects/my-proj/src/app_module.ts', '@NgModule');
148157
options.module = 'app';
149158
options.path = '/projects/my-proj/src';
150159
options.moduleExt = '_module.ts';
@@ -164,7 +173,7 @@ describe('find-module', () => {
164173
});
165174

166175
it('should ignore custom ext if module or ${module}.ts exists', () => {
167-
tree.create('/projects/my-proj/src/app.module.ts', '');
176+
tree.create('/projects/my-proj/src/app.module.ts', '@NgModule');
168177
options.path = '/projects/my-proj/src';
169178
options.moduleExt = '_module.ts';
170179
let modPath;

0 commit comments

Comments
 (0)
Please sign in to comment.