Skip to content

Commit 35f5404

Browse files
authored
fix(core): default to 'run' target when only project is specified (#31452)
## Current Behavior When running `nx run <project>` without specifying a target, the command always fails with an error message "Both project and target have to be specified", even if the project has a "run" target defined. ## Expected Behavior When running `nx run <project>` without specifying a target, the command should check if the project has a "run" target defined. If it does, use it as the default target. This improves developer experience by allowing simpler commands like `nx run myapp` instead of `nx run myapp:run`. ## Related Issue(s) This change improves the developer experience for projects that have a "run" target defined, making the CLI more intuitive. ## Changes Made - Modified `packages/nx/src/command-line/run/run-one.ts` to check for a "run" target when no target is specified - Added comprehensive test coverage in `e2e/nx/src/run.test.ts` to verify: - Projects with a "run" target default to it when no target is specified - Projects without a "run" target still show the original error message - Maintains full backward compatibility ## Testing - All existing tests pass - Added new e2e tests to verify the behavior - Ran full validation suite (`nx prepush`) successfully
1 parent c49b941 commit 35f5404

File tree

3 files changed

+553
-4
lines changed

3 files changed

+553
-4
lines changed

e2e/nx/src/run.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,33 @@ describe('Nx Running Tests', () => {
453453
);
454454
}, 10000);
455455

456+
it('should default to "run" target when only project is specified and it has a run target', () => {
457+
const myapp = uniq('app');
458+
runCLI(`generate @nx/web:app apps/${myapp}`);
459+
460+
// Add a "run" target to the project
461+
updateJson(`apps/${myapp}/project.json`, (c) => {
462+
c.targets['run'] = {
463+
command: 'echo Running the app',
464+
};
465+
return c;
466+
});
467+
468+
// Running with just the project name should default to the "run" target
469+
const output = runCLI(`run ${myapp}`);
470+
expect(output).toContain('Running the app');
471+
expect(output).toContain(`nx run ${myapp}:run`);
472+
});
473+
474+
it('should still require target when project does not have a run target', () => {
475+
const myapp = uniq('app');
476+
runCLI(`generate @nx/web:app apps/${myapp}`);
477+
478+
// Project has no "run" target, so it should fail
479+
const result = runCLI(`run ${myapp}`, { silenceError: true });
480+
expect(result).toContain('Both project and target have to be specified');
481+
});
482+
456483
describe('target defaults + executor specifications', () => {
457484
it('should be able to run targets with unspecified executor given an appropriate targetDefaults entry', () => {
458485
const target = uniq('target');

0 commit comments

Comments
 (0)