Skip to content

Commit e8ff334

Browse files
committed
fix(@angular/cli): fix component not finding closest module
fixes #5127
1 parent 2092ead commit e8ff334

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

packages/@angular/cli/blueprints/component/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ export default Blueprint.extend({
9595
}
9696
} else {
9797
try {
98-
this.pathToModule = findParentModule(
99-
this.project.root, appConfig.root, this.dynamicPath.dir);
98+
this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath);
10099
} catch (e) {
101100
if (!options.skipImport) {
102101
throw `Error locating module for declaration\n\t${e}`;

packages/@angular/cli/utilities/find-parent-module.ts

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export default function findParentModule(
1313
let pathToCheck = path.join(sourceRoot, currentDir);
1414

1515
while (pathToCheck.length >= sourceRoot.length) {
16+
if (!fs.existsSync(pathToCheck)) {
17+
pathToCheck = path.dirname(pathToCheck);
18+
continue;
19+
}
1620
// TODO: refactor to not be based upon file name
1721
const files = fs.readdirSync(pathToCheck)
1822
.filter(fileName => !fileName.endsWith('routing.module.ts'))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { join } from 'path';
2+
import { ng } from '../../../utils/process';
3+
import { expectFileToMatch } from '../../../utils/fs';
4+
5+
6+
export default function () {
7+
const modulePath = join('src', 'app', 'foo', 'foo.module.ts');
8+
9+
return Promise.resolve()
10+
.then(() => ng('generate', 'module', 'foo'))
11+
.then(() => ng('generate', 'component', 'foo'))
12+
.then(() => expectFileToMatch(modulePath, /import { FooComponent } from '.\/foo.component'/));
13+
}

0 commit comments

Comments
 (0)