Skip to content

Commit aba87f8

Browse files
AgentEnderFrozenPandaz
authored andcommitted
fix(core): script-based targets should be able to be modified in a project.json file (#27309)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## 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) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #27258 (cherry picked from commit 25212e3)
1 parent d15fce9 commit aba87f8

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

packages/nx/src/plugins/package-json/create-nodes.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,13 +570,17 @@ describe('nx package.json workspaces plugin', () => {
570570
name: 'root',
571571
scripts: {
572572
build: 'echo build',
573+
test: 'echo test',
573574
},
574575
}),
575576
'packages/a/project.json': JSON.stringify({
576577
targets: {
577578
'something-other-than-build': {
578579
command: 'echo something-other-than-build',
579580
},
581+
test: {
582+
dependsOn: ['build-native'],
583+
},
580584
},
581585
}),
582586
},
@@ -597,6 +601,7 @@ describe('nx package.json workspaces plugin', () => {
597601
"targetGroups": {
598602
"NPM Scripts": [
599603
"build",
604+
"test",
600605
],
601606
},
602607
},
@@ -624,6 +629,16 @@ describe('nx package.json workspaces plugin', () => {
624629
"executor": "@nx/js:release-publish",
625630
"options": {},
626631
},
632+
"test": {
633+
"executor": "nx:run-script",
634+
"metadata": {
635+
"runCommand": "npm run test",
636+
"scriptContent": "echo test",
637+
},
638+
"options": {
639+
"script": "test",
640+
},
641+
},
627642
},
628643
},
629644
},

packages/nx/src/plugins/package-json/create-nodes.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,16 @@ export function buildProjectConfigurationFromPackageJson(
151151

152152
if (siblingProjectJson) {
153153
for (const target of Object.keys(siblingProjectJson?.targets ?? {})) {
154-
delete packageJson.scripts?.[target];
154+
const { executor, command, options } = siblingProjectJson.targets[target];
155+
if (
156+
// will use run-commands, different target
157+
command ||
158+
// Either uses a different executor or runs a different script
159+
(executor &&
160+
(executor !== 'nx:run-script' || options?.script !== target))
161+
) {
162+
delete packageJson.scripts?.[target];
163+
}
155164
}
156165
}
157166

0 commit comments

Comments
 (0)