Skip to content

Commit cfdac4f

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 cfdac4f

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { join } from 'path';
2+
import { execAndWaitForOutputToMatch, ng } from '../../utils/process';
3+
import { updateJsonFile } from '../../utils/project';
4+
import { expectToFail } from '../../utils/utils';
5+
6+
export default async function () {
7+
const errorMessage =
8+
'Cannot determine project for command. ' +
9+
'Pass the project name as a command line argument or change the current working directory to a project directory';
10+
11+
// Delete root project
12+
await updateJsonFile('angular.json', (workspaceJson) => {
13+
delete workspaceJson.projects['test-project'];
14+
});
15+
16+
await ng('generate', 'app', 'second-app', '--skip-install');
17+
await ng('generate', 'app', 'third-app', '--skip-install');
18+
19+
const startCwd = process.cwd();
20+
21+
try {
22+
const { message } = await expectToFail(() => ng('build'));
23+
if (!message.includes(errorMessage)) {
24+
throw new Error(`Expected build to fail with: '${errorMessage}'.`);
25+
}
26+
27+
// Help should still work
28+
execAndWaitForOutputToMatch('ng', ['build', '--help'], /--configuration/);
29+
30+
process.chdir(join(startCwd, 'projects/second-app'));
31+
await ng('build', '--configuration=development');
32+
} finally {
33+
// Restore path
34+
process.chdir(startCwd);
35+
}
36+
}

0 commit comments

Comments
 (0)