Skip to content

Commit

Permalink
fix(@angular/cli): provide actionable error when project cannot be de…
Browse files Browse the repository at this point in the history
…termined

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.

(cherry picked from commit 79ea0f3)
  • Loading branch information
alan-agius4 authored and clydin committed Jun 8, 2022
1 parent 613ba31 commit 17fec13
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Expand Up @@ -104,7 +104,6 @@ export abstract class ArchitectCommandModule
if (projectName) {
return workspace.projects.has(projectName) ? projectName : undefined;
}

const target = this.getArchitectTarget();
const projectFromTarget = this.getProjectNamesByTarget(target);

Expand All @@ -114,8 +113,8 @@ export abstract class ArchitectCommandModule
@memoize
private getProjectNamesByTarget(target: string): string[] | undefined {
const workspace = this.getWorkspaceOrThrow();

const allProjectsForTargetName: string[] = [];

for (const [name, project] of workspace.projects) {
if (project.targets.has(target)) {
allProjectsForTargetName.push(name);
Expand All @@ -135,8 +134,17 @@ export abstract class ArchitectCommandModule
}

const maybeProject = getProjectByCwd(workspace);
if (maybeProject && allProjectsForTargetName.includes(maybeProject)) {
return [maybeProject];
if (maybeProject) {
return allProjectsForTargetName.includes(maybeProject) ? [maybeProject] : undefined;
}

const { getYargsCompletions, help } = this.context.args.options;
if (!getYargsCompletions && !help) {
// Only issue the below error when not in help / completion mode.
throw new CommandModuleError(
'Cannot determine project for command. ' +
'Pass the project name as a command line argument or change the current working directory to a project directory.',
);
}
}

Expand Down
@@ -0,0 +1,36 @@
import { join } from 'path';
import { execAndWaitForOutputToMatch, ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
import { expectToFail } from '../../utils/utils';

export default async function () {
const errorMessage =
'Cannot determine project for command. ' +
'Pass the project name as a command line argument or change the current working directory to a project directory';

// Delete root project
await updateJsonFile('angular.json', (workspaceJson) => {
delete workspaceJson.projects['test-project'];
});

await ng('generate', 'app', 'second-app', '--skip-install');
await ng('generate', 'app', 'third-app', '--skip-install');

const startCwd = process.cwd();

try {
const { message } = await expectToFail(() => ng('build'));
if (!message.includes(errorMessage)) {
throw new Error(`Expected build to fail with: '${errorMessage}'.`);
}

// Help should still work
execAndWaitForOutputToMatch('ng', ['build', '--help'], /--configuration/);

process.chdir(join(startCwd, 'projects/second-app'));
await ng('build', '--configuration=development');
} finally {
// Restore path
process.chdir(startCwd);
}
}

0 comments on commit 17fec13

Please sign in to comment.