Skip to content

Commit

Permalink
fix(@angular/cli): favor project in cwd when running architect commands
Browse files Browse the repository at this point in the history
When running architect command such as `ng build`, `ng test`, `ng lint`... and no project is provided as a positional argument. The project in the current working directory is favored instead of the configured as default project.
  • Loading branch information
alan-agius4 authored and clydin committed Mar 14, 2022
1 parent ca40125 commit 054ae02
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { Argv } from 'yargs';
import { getProjectByCwd } from '../utilities/config';
import { ArchitectBaseCommandModule } from './architect-base-command-module';
import {
CommandModuleError,
Expand Down Expand Up @@ -127,14 +128,9 @@ export abstract class ArchitectCommandModule
// For multi target commands, we always list all projects that have the target.
return allProjectsForTargetName;
} else {
// For single target commands, we try the default project first,
// then the full list if it has a single project, then error out.
const maybeDefaultProject = workspace.extensions['defaultProject'];
if (
typeof maybeDefaultProject === 'string' &&
allProjectsForTargetName.includes(maybeDefaultProject)
) {
return [maybeDefaultProject];
const maybeProject = getProjectByCwd(workspace);
if (maybeProject && allProjectsForTargetName.includes(maybeProject)) {
return [maybeProject];
}

if (allProjectsForTargetName.length === 1) {
Expand Down
21 changes: 21 additions & 0 deletions tests/legacy-cli/e2e/tests/commands/builder-project-by-cwd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { join } from 'path';
import { expectFileToExist } from '../../utils/fs';
import { ng } from '../../utils/process';

export default async function () {
await ng('generate', 'app', 'second-app', '--skip-install');
await ng('generate', 'app', 'third-app', '--skip-install');
const startCwd = process.cwd();

try {
// When no project is provided it should favor the project that is located in the current working directory.
process.chdir(join(startCwd, 'projects/second-app'));
await ng('build', '--configuration=development');

process.chdir(startCwd);
await expectFileToExist('dist/second-app');
} finally {
// restore path
process.chdir(startCwd);
}
}

0 comments on commit 054ae02

Please sign in to comment.