Skip to content

Commit 8ac7f57

Browse files
committed
fix(@angular/cli): provide actionable error when project cannot be determined
When the workspace has multiple projects and we the project to use cannot be determined from the current working directory, we now issue an actionable error message. Closes angular#23291
1 parent f6f3782 commit 8ac7f57

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

packages/angular/cli/src/command-builder/architect-command-module.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,10 @@ export abstract class ArchitectCommandModule
9494
}
9595

9696
private getArchitectProject(): string | undefined {
97-
const workspace = this.context.workspace;
98-
if (!workspace) {
99-
return undefined;
100-
}
101-
10297
const [, projectName] = this.context.args.positional;
10398

10499
if (projectName) {
105-
return workspace.projects.has(projectName) ? projectName : undefined;
100+
return projectName;
106101
}
107102

108103
const target = this.getArchitectTarget();
@@ -114,8 +109,8 @@ export abstract class ArchitectCommandModule
114109
@memoize
115110
private getProjectNamesByTarget(target: string): string[] | undefined {
116111
const workspace = this.getWorkspaceOrThrow();
117-
118112
const allProjectsForTargetName: string[] = [];
113+
119114
for (const [name, project] of workspace.projects) {
120115
if (project.targets.has(target)) {
121116
allProjectsForTargetName.push(name);
@@ -135,8 +130,17 @@ export abstract class ArchitectCommandModule
135130
}
136131

137132
const maybeProject = getProjectByCwd(workspace);
138-
if (maybeProject && allProjectsForTargetName.includes(maybeProject)) {
139-
return [maybeProject];
133+
if (maybeProject) {
134+
return allProjectsForTargetName.includes(maybeProject) ? [maybeProject] : undefined;
135+
}
136+
137+
const { getYargsCompletions, help } = this.context.args.options;
138+
if (!getYargsCompletions || !help) {
139+
// Only issue the below error when not in help / completion mode.
140+
throw new CommandModuleError(
141+
'Cannot determine project for command. ' +
142+
'Pass the project name as a command line argument or change the current working directory to a project directory.',
143+
);
140144
}
141145
}
142146

0 commit comments

Comments
 (0)