Skip to content

Commit b7b643d

Browse files
committed
fix(@schematics/angular): set explicit type for the module --route component
`ng generate module --route` generates a component and references it from the routing module template by its default file and class name (`./foo`, `Foo`). The component inherited the workspace `type` option, so in workspaces that set it (e.g. after updating to v20) the component was generated as `foo.component.ts` and the routing module import failed to resolve. Pass an empty `type`, as the library and app-shell schematics do. Fixes #34449
1 parent dc65268 commit b7b643d

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎packages/schematics/angular/module/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ const moduleSchematic: RuleFactory<ModuleOptions> = createProjectSchematic(
183183
path: options.path,
184184
project: options.project,
185185
standalone: false,
186+
// The route templates reference the component by its default file and class name, so the
187+
// component must not inherit a `type` from the workspace.
188+
type: '',
186189
};
187190

188191
return chain([

‎packages/schematics/angular/module/index_spec.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,28 @@ describe('Module Schematic', () => {
165165
);
166166
});
167167

168+
it('should not inherit the component `type` from the workspace defaults', async () => {
169+
// The CLI applies the `schematics` defaults of `angular.json` to nested schematics as well.
170+
// Emulate it with a fresh runner so that other tests are not affected.
171+
const runner = new SchematicTestRunner(
172+
'@schematics/angular',
173+
require.resolve('../collection.json'),
174+
);
175+
runner['_engineHost'].registerOptionsTransform(
176+
(schematic: { name: string }, options: Record<string, unknown>) =>
177+
schematic.name === 'component' && options['type'] === undefined
178+
? { ...options, type: 'component' }
179+
: options,
180+
);
181+
182+
const tree = await runner.runSchematic('module', options, appTree);
183+
184+
expect(tree.exists('/projects/bar/src/app/foo/foo.ts')).toBe(true);
185+
expect(tree.exists('/projects/bar/src/app/foo/foo.component.ts')).toBe(false);
186+
const content = tree.readContent('/projects/bar/src/app/foo/foo-routing-module.ts');
187+
expect(content).toContain(`import { Foo } from './foo';`);
188+
});
189+
168190
it('should generate a lazy loaded module with embedded route declarations', async () => {
169191
appTree.overwrite(
170192
'/projects/bar/src/app/app-module.ts',

0 commit comments

Comments
 (0)