Skip to content

Commit d07e68a

Browse files
alan-agius4Keen Yee Liau
authored and
Keen Yee Liau
committed
fix(@schematics/angular): module imports paths are incorrect when flat option is used
Closes #12169
1 parent b8b36ba commit d07e68a

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

packages/schematics/angular/module/index.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { basename, dirname, normalize, relative, strings } from '@angular-devkit/core';
8+
import { normalize, strings } from '@angular-devkit/core';
99
import {
1010
Rule,
1111
SchematicsException,
@@ -24,7 +24,7 @@ import * as ts from 'typescript';
2424
import { addImportToModule } from '../utility/ast-utils';
2525
import { InsertChange } from '../utility/change';
2626
import { getWorkspace } from '../utility/config';
27-
import { findModuleFromOptions } from '../utility/find-module';
27+
import { buildRelativePath, findModuleFromOptions } from '../utility/find-module';
2828
import { parseName } from '../utility/parse-name';
2929
import { buildDefaultPath } from '../utility/project';
3030
import { Schema as ModuleOptions } from './schema';
@@ -36,7 +36,7 @@ function addDeclarationToNgModule(options: ModuleOptions): Rule {
3636
return host;
3737
}
3838

39-
const modulePath = normalize('/' + options.module);
39+
const modulePath = options.module;
4040

4141
const text = host.read(modulePath);
4242
if (text === null) {
@@ -51,10 +51,9 @@ function addDeclarationToNgModule(options: ModuleOptions): Rule {
5151
+ strings.dasherize(options.name)
5252
+ '.module',
5353
);
54-
const relativeDir = relative(dirname(modulePath), dirname(importModulePath));
55-
const relativePath = (relativeDir.startsWith('.') ? relativeDir : './' + relativeDir)
56-
+ '/' + basename(importModulePath);
57-
const changes = addImportToModule(source, modulePath,
54+
const relativePath = buildRelativePath(modulePath, importModulePath);
55+
const changes = addImportToModule(source,
56+
modulePath,
5857
strings.classify(`${options.name}Module`),
5958
relativePath);
6059

packages/schematics/angular/module/index_spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ describe('Module Schematic', () => {
6363
expect(content).toMatch(/imports: \[[^\]]*FooModule[^\]]*\]/m);
6464
});
6565

66+
it('should import into another module when using flat', () => {
67+
const options = { ...defaultOptions, flat: true, module: 'app.module.ts' };
68+
69+
const tree = schematicRunner.runSchematic('module', options, appTree);
70+
const content = tree.readContent('/projects/bar/src/app/app.module.ts');
71+
expect(content).toMatch(/import { FooModule } from '.\/foo.module'/);
72+
expect(content).toMatch(/imports: \[[^\]]*FooModule[^\]]*\]/m);
73+
});
74+
6675
it('should import into another module (deep)', () => {
6776
let tree = appTree;
6877

0 commit comments

Comments
 (0)