From fa83b36131c03a7c6d93b19f2639b85fae19b6fc Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Tue, 16 Jul 2024 17:25:32 -0500 Subject: [PATCH] fix(core): properly handle reading target defaults (#26959) ## Current Behavior When targets have executors, they cannot be matched with a glob pattern ## Expected Behavior When targets have executors, they can still be matched with a glob if the executor does not have target defaults set. ## Related Issue(s) Fixes # --- .../utils/project-configuration-utils.spec.ts | 12 ++++++++++++ .../utils/project-configuration-utils.ts | 7 +++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/nx/src/project-graph/utils/project-configuration-utils.spec.ts b/packages/nx/src/project-graph/utils/project-configuration-utils.spec.ts index 7da77446b5418..6e008bca2a7e0 100644 --- a/packages/nx/src/project-graph/utils/project-configuration-utils.spec.ts +++ b/packages/nx/src/project-graph/utils/project-configuration-utils.spec.ts @@ -79,6 +79,18 @@ describe('project-configuration-utils', () => { ).toEqual('default-value-for-e2e-ci-file'); }); + it('should return longest matching target even if executor is passed', () => { + expect( + // This uses an executor which does not have settings in target defaults + // thus the target name pattern target defaults are used + readTargetDefaultsForTarget( + 'e2e-ci--file-foo', + targetDefaults, + 'other-executor' + ).options['key'] + ).toEqual('default-value-for-e2e-ci-file'); + }); + it('should not merge top level properties for incompatible targets', () => { expect( mergeTargetConfigurations( diff --git a/packages/nx/src/project-graph/utils/project-configuration-utils.ts b/packages/nx/src/project-graph/utils/project-configuration-utils.ts index 5bc1ca103155c..db54257ac33e6 100644 --- a/packages/nx/src/project-graph/utils/project-configuration-utils.ts +++ b/packages/nx/src/project-graph/utils/project-configuration-utils.ts @@ -1029,14 +1029,13 @@ export function readTargetDefaultsForTarget( targetDefaults: TargetDefaults, executor?: string ): TargetDefaults[string] { - if (executor) { + if (executor && targetDefaults?.[executor]) { // If an executor is defined in project.json, defaults should be read // from the most specific key that matches that executor. // e.g. If executor === run-commands, and the target is named build: // Use, use nx:run-commands if it is present // If not, use build if it is present. - const key = [executor, targetName].find((x) => targetDefaults?.[x]); - return key ? targetDefaults?.[key] : null; + return targetDefaults?.[executor]; } else if (targetDefaults?.[targetName]) { // If the executor is not defined, the only key we have is the target name. return targetDefaults?.[targetName]; @@ -1057,7 +1056,7 @@ export function readTargetDefaultsForTarget( return targetDefaults[matchingTargetDefaultKey]; } - return {}; + return null; } function createRootMap(projectRootMap: Record) {