Skip to content

Commit 077ee8f

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.
1 parent f6f3782 commit 077ee8f

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ export abstract class ArchitectCommandModule
104104
if (projectName) {
105105
return workspace.projects.has(projectName) ? projectName : undefined;
106106
}
107-
108107
const target = this.getArchitectTarget();
109108
const projectFromTarget = this.getProjectNamesByTarget(target);
110109

@@ -114,8 +113,8 @@ export abstract class ArchitectCommandModule
114113
@memoize
115114
private getProjectNamesByTarget(target: string): string[] | undefined {
116115
const workspace = this.getWorkspaceOrThrow();
117-
118116
const allProjectsForTargetName: string[] = [];
117+
119118
for (const [name, project] of workspace.projects) {
120119
if (project.targets.has(target)) {
121120
allProjectsForTargetName.push(name);
@@ -135,8 +134,17 @@ export abstract class ArchitectCommandModule
135134
}
136135

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

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)