From aba87f800e08bb7b26b6fa33ef9a88f03d022a2f Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Tue, 6 Aug 2024 16:33:32 -0400 Subject: [PATCH] fix(core): script-based targets should be able to be modified in a project.json file (#27309) ## Current Behavior We don't infer scripts if any info for that target is present in project.json. This results in the target dissapearing if the user tries to modify the inferred target by providing info in project.json, for example by providing dependsOn or similar. A minimal repro of the issue looks something like this: > packages/foo/package.json ```json { "name": "foo", "scripts": { "build": "echo build" } } ``` > packages/foo/project.json ```json { "name": "foo", "targets": { "build": { "dependsOn": [] } } } ``` Attempting to run `nx build foo` results in "Cannot find configuration for task foo:build", as we remove the target for not having an executor. ## Expected Behavior The target remains as it can run the script, so we have to infer the script to begin with. ## Related Issue(s) Fixes #27258 (cherry picked from commit 25212e3dd08182a1e7662aa37d5054941e87ebfb) --- .../src/plugins/package-json/create-nodes.spec.ts | 15 +++++++++++++++ .../nx/src/plugins/package-json/create-nodes.ts | 11 ++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/nx/src/plugins/package-json/create-nodes.spec.ts b/packages/nx/src/plugins/package-json/create-nodes.spec.ts index 7560f89db2f13..fa6bdda6d70db 100644 --- a/packages/nx/src/plugins/package-json/create-nodes.spec.ts +++ b/packages/nx/src/plugins/package-json/create-nodes.spec.ts @@ -570,6 +570,7 @@ describe('nx package.json workspaces plugin', () => { name: 'root', scripts: { build: 'echo build', + test: 'echo test', }, }), 'packages/a/project.json': JSON.stringify({ @@ -577,6 +578,9 @@ describe('nx package.json workspaces plugin', () => { 'something-other-than-build': { command: 'echo something-other-than-build', }, + test: { + dependsOn: ['build-native'], + }, }, }), }, @@ -597,6 +601,7 @@ describe('nx package.json workspaces plugin', () => { "targetGroups": { "NPM Scripts": [ "build", + "test", ], }, }, @@ -624,6 +629,16 @@ describe('nx package.json workspaces plugin', () => { "executor": "@nx/js:release-publish", "options": {}, }, + "test": { + "executor": "nx:run-script", + "metadata": { + "runCommand": "npm run test", + "scriptContent": "echo test", + }, + "options": { + "script": "test", + }, + }, }, }, }, diff --git a/packages/nx/src/plugins/package-json/create-nodes.ts b/packages/nx/src/plugins/package-json/create-nodes.ts index d722ae90554c9..e983c8f7eaa3d 100644 --- a/packages/nx/src/plugins/package-json/create-nodes.ts +++ b/packages/nx/src/plugins/package-json/create-nodes.ts @@ -151,7 +151,16 @@ export function buildProjectConfigurationFromPackageJson( if (siblingProjectJson) { for (const target of Object.keys(siblingProjectJson?.targets ?? {})) { - delete packageJson.scripts?.[target]; + const { executor, command, options } = siblingProjectJson.targets[target]; + if ( + // will use run-commands, different target + command || + // Either uses a different executor or runs a different script + (executor && + (executor !== 'nx:run-script' || options?.script !== target)) + ) { + delete packageJson.scripts?.[target]; + } } }