From ff0f56e0fcf3bbf3ab9482769ae6247e55f0faa1 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 1 Jul 2022 09:14:48 +0000 Subject: [PATCH] fix(@angular/cli): improve error message for project-specific ng commands when run outside of a project With this change we improve the error message when a project for command cannot be determined ``` Error: Cannot determine project for command. This is a multi-project workspace and more than one project supports this command. Run "ng build [project]" to execute the command for a specific project or change the current working directory to a project directory. Available projects are: - project-name-0 - project-name-1 - project-name-2 - project-name-3 - project-name-4 - project-name-5 - project-name-6 - project-name-7 - project-name-8 - project-name-9 ``` Closes #23481 --- .../src/command-builder/architect-command-module.ts | 10 ++++++++-- .../commands/project-cannot-be-determined-by-cwd.ts | 5 ++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/angular/cli/src/command-builder/architect-command-module.ts b/packages/angular/cli/src/command-builder/architect-command-module.ts index abd3d2c41fb4..a57c74f0eeef 100644 --- a/packages/angular/cli/src/command-builder/architect-command-module.ts +++ b/packages/angular/cli/src/command-builder/architect-command-module.ts @@ -144,8 +144,14 @@ export abstract class ArchitectCommandModule 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.', + 'Cannot determine project for command.\n' + + 'This is a multi-project workspace and more than one project supports this command. ' + + `Run "ng ${this.command}" to execute the command for a specific project or change the current ` + + 'working directory to a project directory.\n\n' + + `Available projects are:\n${allProjectsForTargetName + .sort() + .map((p) => `- ${p}`) + .join('\n')}`, ); } } diff --git a/tests/legacy-cli/e2e/tests/commands/project-cannot-be-determined-by-cwd.ts b/tests/legacy-cli/e2e/tests/commands/project-cannot-be-determined-by-cwd.ts index e75e869fcb75..40ccce6640da 100644 --- a/tests/legacy-cli/e2e/tests/commands/project-cannot-be-determined-by-cwd.ts +++ b/tests/legacy-cli/e2e/tests/commands/project-cannot-be-determined-by-cwd.ts @@ -1,13 +1,12 @@ import { join } from 'path'; -import { expectFileNotToExist, expectFileToExist } from '../../utils/fs'; 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'; + 'Cannot determine project for command.\n' + + 'This is a multi-project workspace and more than one project supports this command.'; // Delete root project await updateJsonFile('angular.json', (workspaceJson) => {