Skip to content

fix(@angular/cli): provide actionable error when project cannot be determined #23302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,16 @@ export abstract class ArchitectCommandModule
}

private getArchitectProject(): string | undefined {
const workspace = this.context.workspace;
if (!workspace) {
return undefined;
}

const [, projectName] = this.context.args.positional;
const { options, positional } = this.context.args;
const [, projectName] = positional;

if (projectName) {
return workspace.projects.has(projectName) ? projectName : undefined;
return projectName;
}

// Yargs allows positional args to be used as flags.
if (typeof options['project'] === 'string') {
return options['project'];
}

const target = this.getArchitectTarget();
Expand All @@ -114,8 +115,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 +136,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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';

// 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/);

// Yargs allows positional args to be passed as flags. Verify that in this case the project can be determined.
await ng('build', '--project=third-app', '--configuration=development');

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