From 3c33c3a40cbb2a7bc328331be237c49f79cb28f0 Mon Sep 17 00:00:00 2001 From: FrozenPandaz Date: Fri, 26 Jan 2024 14:56:23 -0500 Subject: [PATCH] fix(core): do not create new targets from target defaults when package.json script is not included --- .../target-defaults-plugin.spec.ts | 158 +++++++++++++++++- .../target-defaults/target-defaults-plugin.ts | 16 +- 2 files changed, 167 insertions(+), 7 deletions(-) diff --git a/packages/nx/src/plugins/target-defaults/target-defaults-plugin.spec.ts b/packages/nx/src/plugins/target-defaults/target-defaults-plugin.spec.ts index 97576e723d6ba..504982bcd38f2 100644 --- a/packages/nx/src/plugins/target-defaults/target-defaults-plugin.spec.ts +++ b/packages/nx/src/plugins/target-defaults/target-defaults-plugin.spec.ts @@ -8,7 +8,7 @@ const { createNodes: [, createNodesFn], } = TargetDefaultsPlugin; -describe('nx project.json plugin', () => { +describe('target-defaults plugin', () => { let context: CreateNodesContext; beforeEach(() => { context = { @@ -23,6 +23,10 @@ describe('nx project.json plugin', () => { }; }); + afterEach(() => { + memfs.vol.reset(); + }); + it('should add target default info to project json projects', () => { memfs.vol.fromJSON( { @@ -77,4 +81,156 @@ describe('nx project.json plugin', () => { } `); }); + + it('should add target if package.json has nx but no includedScripts', () => { + memfs.vol.fromJSON( + { + 'package.json': JSON.stringify({ + name: 'lib-a', + scripts: { + test: 'nx affected:test', + }, + nx: {}, + }), + }, + '/root' + ); + + expect( + createNodesFn('package.json', undefined, { + nxJsonConfiguration: { + targetDefaults: { + test: { + command: 'jest', + }, + }, + }, + workspaceRoot: '/root', + }) + ).toMatchInlineSnapshot(` + { + "projects": { + ".": { + "targets": { + "test": { + "command": "jest", + }, + }, + }, + }, + } + `); + }); + + it('should add target if package.json has nx and includes the script in includedScripts', () => { + memfs.vol.fromJSON( + { + 'package.json': JSON.stringify({ + name: 'lib-a', + scripts: { + test: 'nx affected:test', + }, + nx: { + includedScripts: ['test'], + }, + }), + }, + '/root' + ); + + expect( + createNodesFn('package.json', undefined, { + nxJsonConfiguration: { + targetDefaults: { + test: { + command: 'jest', + }, + }, + }, + workspaceRoot: '/root', + }) + ).toMatchInlineSnapshot(` + { + "projects": { + ".": { + "targets": { + "test": { + "command": "jest", + }, + }, + }, + }, + } + `); + }); + + it('should not add target if package.json does not have nx', () => { + memfs.vol.fromJSON( + { + 'package.json': JSON.stringify({ + name: 'lib-a', + scripts: { + test: 'nx affected:test', + }, + }), + }, + '/root' + ); + + expect( + createNodesFn('package.json', undefined, { + nxJsonConfiguration: { + targetDefaults: { + test: { + command: 'jest', + }, + }, + }, + workspaceRoot: '/root', + }) + ).toMatchInlineSnapshot(`{}`); + }); + + it('should not add target if project does not define target', () => { + memfs.vol.fromJSON( + { + 'package.json': JSON.stringify({ + name: 'lib-a', + scripts: { + test: 'nx affected:test', + }, + nx: { + includedScripts: [], + }, + }), + }, + '/root' + ); + + expect( + createNodesFn('package.json', undefined, { + nxJsonConfiguration: { + targetDefaults: { + test: { + command: 'jest', + }, + }, + }, + workspaceRoot: '/root', + }) + ).toMatchInlineSnapshot(` + { + "projects": { + ".": { + "targets": { + "test": { + "command": "jest", + Symbol(ONLY_MODIFIES_EXISTING_TARGET): true, + }, + }, + }, + }, + } + `); + }); }); diff --git a/packages/nx/src/plugins/target-defaults/target-defaults-plugin.ts b/packages/nx/src/plugins/target-defaults/target-defaults-plugin.ts index 1f1cfe1d0bb11..57fac4355abb3 100644 --- a/packages/nx/src/plugins/target-defaults/target-defaults-plugin.ts +++ b/packages/nx/src/plugins/target-defaults/target-defaults-plugin.ts @@ -58,12 +58,16 @@ export const TargetDefaultsPlugin: NxPluginV2 = { const packageJson = readJsonOrNull( join(ctx.workspaceRoot, root, 'package.json') ); - const projectDefinedTargets = new Set( - Object.keys({ - ...packageJson?.scripts, - ...projectJson?.targets, - }) - ); + const includedScripts = packageJson?.nx?.includedScripts; + const projectDefinedTargets = new Set([ + ...Object.keys(packageJson?.scripts ?? {}).filter((script) => { + if (includedScripts) { + return includedScripts.includes(script); + } + return true; + }), + ...Object.keys(projectJson?.targets ?? {}), + ]); const executorToTargetMap = getExecutorToTargetMap( packageJson,