diff --git a/packages/nx/src/config/workspaces.spec.ts b/packages/nx/src/config/workspaces.spec.ts index 99d55af978040..b85882b2eba6c 100644 --- a/packages/nx/src/config/workspaces.spec.ts +++ b/packages/nx/src/config/workspaces.spec.ts @@ -59,6 +59,7 @@ describe('Workspaces', () => { projectType: 'library', targets: { 'nx-release-publish': { + configurations: {}, dependsOn: ['^nx-release-publish'], executor: '@nx/js:release-publish', options: {}, diff --git a/packages/nx/src/project-graph/utils/normalize-project-nodes.spec.ts b/packages/nx/src/project-graph/utils/normalize-project-nodes.spec.ts index 86d4c3f8d0157..007572e279c2c 100644 --- a/packages/nx/src/project-graph/utils/normalize-project-nodes.spec.ts +++ b/packages/nx/src/project-graph/utils/normalize-project-nodes.spec.ts @@ -78,28 +78,4 @@ describe('workspace-projects', () => { ).toEqual(['b', 'b-1', 'b-2']); }); }); - - describe('normalizeTargets', () => { - it('should support {projectRoot}, {workspaceRoot}, and {projectName} tokens', () => { - expect( - normalizeProjectTargets( - { - name: 'project', - root: 'my/project', - targets: { - build: { - executor: 'target', - options: { - a: '{projectRoot}', - b: '{workspaceRoot}', - c: '{projectName}', - }, - }, - }, - }, - 'build' - ).build.options - ).toEqual({ a: 'my/project', b: '', c: 'project' }); - }); - }); }); diff --git a/packages/nx/src/project-graph/utils/normalize-project-nodes.ts b/packages/nx/src/project-graph/utils/normalize-project-nodes.ts index f01ff9768dcbb..92143b55913fa 100644 --- a/packages/nx/src/project-graph/utils/normalize-project-nodes.ts +++ b/packages/nx/src/project-graph/utils/normalize-project-nodes.ts @@ -99,21 +99,6 @@ export function normalizeProjectTargets( delete targets[target]; continue; } - - targets[target].options = resolveNxTokensInOptions( - targets[target].options, - project, - `${projectName}:${target}` - ); - - targets[target].configurations ??= {}; - for (const configuration in targets[target].configurations) { - targets[target].configurations[configuration] = resolveNxTokensInOptions( - targets[target].configurations[configuration], - project, - `${projectName}:${target}:${configuration}` - ); - } } return targets; } 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 b54ed81af526a..06ddaa8a95e38 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 @@ -9,6 +9,7 @@ import { isCompatibleTarget, mergeProjectConfigurationIntoRootMap, mergeTargetConfigurations, + normalizeTarget, readProjectConfigurationsFromRootMap, readTargetDefaultsForTarget, } from './project-configuration-utils'; @@ -572,6 +573,7 @@ describe('project-configuration-utils', () => { "root": "libs/lib-a", "targets": { "build": { + "configurations": {}, "executor": "nx:run-commands", "options": { "command": "tsc", @@ -725,8 +727,9 @@ describe('project-configuration-utils', () => { expect(targets.echo).toMatchInlineSnapshot(` { "command": "echo lib-a", + "configurations": {}, "options": { - "cwd": "{projectRoot}", + "cwd": "libs/lib-a", }, } `); @@ -1548,6 +1551,28 @@ describe('project-configuration-utils', () => { }); }); + describe('normalizeTarget', () => { + it('should support {projectRoot}, {workspaceRoot}, and {projectName} tokens', () => { + const config = { + name: 'project', + root: 'libs/project', + targets: { + foo: { command: 'echo {projectRoot}' }, + }, + }; + expect(normalizeTarget(config.targets.foo, config)) + .toMatchInlineSnapshot(` + { + "configurations": {}, + "executor": "nx:run-commands", + "options": { + "command": "echo libs/project", + }, + } + `); + }); + }); + describe('createProjectConfigurations', () => { /* A fake plugin that sets `fake-lib` tag to libs. */ const fakeTagPlugin: NxPluginV2 = { 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 12029fc8fb077..d1fcff5cc4ed0 100644 --- a/packages/nx/src/project-graph/utils/project-configuration-utils.ts +++ b/packages/nx/src/project-graph/utils/project-configuration-utils.ts @@ -50,7 +50,7 @@ export function mergeProjectConfigurationIntoRootMap( sourceInformation?: SourceInformation, // This function is used when reading project configuration // in generators, where we don't want to do this. - skipCommandNormalization?: boolean + skipTargetNormalization?: boolean ): void { if (configurationSourceMaps && !configurationSourceMaps[project.root]) { configurationSourceMaps[project.root] = {}; @@ -202,10 +202,12 @@ export function mergeProjectConfigurationIntoRootMap( continue; } + const normalizedTarget = skipTargetNormalization + ? target + : normalizeTarget(target, project); + const mergedTarget = mergeTargetConfigurations( - skipCommandNormalization - ? target - : resolveCommandSyntacticSugar(target, project.root), + normalizedTarget, matchingProject.targets?.[targetName], sourceMap, sourceInformation, @@ -670,8 +672,8 @@ export function isCompatibleTarget( const isRunCommands = a.executor === 'nx:run-commands'; if (isRunCommands) { - const aCommand = a.options?.command ?? a.options?.commands.join(' && '); - const bCommand = b.options?.command ?? b.options?.commands.join(' && '); + const aCommand = a.options?.command ?? a.options?.commands?.join(' && '); + const bCommand = b.options?.command ?? b.options?.commands?.join(' && '); const oneHasNoCommand = !aCommand || !bCommand; const hasSameCommand = aCommand === bCommand; @@ -836,3 +838,27 @@ function resolveCommandSyntacticSugar( }; } } + +export function normalizeTarget( + target: TargetConfiguration, + project: ProjectConfiguration +) { + target = resolveCommandSyntacticSugar(target, project.root); + + target.options = resolveNxTokensInOptions( + target.options, + project, + `${project.root}:${target}` + ); + + target.configurations ??= {}; + for (const configuration in target.configurations) { + target.configurations[configuration] = resolveNxTokensInOptions( + target.configurations[configuration], + project, + `${project.root}:${target}:${configuration}` + ); + } + + return target; +}