Skip to content

Commit

Permalink
fix(core): project graph should reuse methods to combine target defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Oct 6, 2022
1 parent efd32b7 commit d78feda
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 49 deletions.
14 changes: 9 additions & 5 deletions e2e/nx-run/src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,14 @@ describe('Nx Running Tests', () => {
JSON.stringify({
name: lib,
targets: {
target: {},
[target]: {},
},
})
);

expect(runCLI(`${target} ${lib}`)).toContain(`Hello from ${target}`);
expect(runCLI(`${target} ${lib} --verbose`)).toContain(
`Hello from ${target}`
);
});

it('should be able to pull options from targetDefaults based on executor', () => {
Expand All @@ -247,7 +249,7 @@ describe('Nx Running Tests', () => {

updateJson('nx.json', (nxJson) => {
nxJson.targetDefaults ??= {};
nxJson.targetDefaults[`*|nx:run-commands}`] = {
nxJson.targetDefaults[`*|nx:run-commands`] = {
options: {
command: `echo Hello from ${target}`,
},
Expand All @@ -260,14 +262,16 @@ describe('Nx Running Tests', () => {
JSON.stringify({
name: lib,
targets: {
target: {
[target]: {
executor: 'nx:run-commands',
},
},
})
);

expect(runCLI(`${target} ${lib}`)).toContain(`Hello from ${target}`);
expect(runCLI(`${target} ${lib} --verbose`)).toContain(
`Hello from ${target}`
);
});
});

Expand Down
49 changes: 26 additions & 23 deletions packages/nx/src/config/workspaces.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,52 +173,55 @@ describe('Workspaces', () => {
});

describe('target defaults', () => {
const nxJson = {
targetDefaults: {
'build|nx:run-commands': {
options: {
key: 't:e',
},
const targetDefaults = {
'build|nx:run-commands': {
options: {
key: 't:e',
},
'*|nx:run-commands': {
options: {
key: '*:e',
},
},
'*|nx:run-commands': {
options: {
key: '*:e',
},
build: {
options: {
key: 't',
},
},
build: {
options: {
key: 't',
},
},
};

it('should prefer target|executor key', () => {
expect(
readTargetDefaultsForTarget('build', nxJson, 'run-commands').options[
'key'
]
readTargetDefaultsForTarget('build', targetDefaults, 'nx:run-commands')
.options['key']
).toEqual('t:e');
});

it('should prefer *|executor key', () => {
expect(
readTargetDefaultsForTarget('other-target', nxJson, 'run-commands')
.options['key']
readTargetDefaultsForTarget(
'other-target',
targetDefaults,
'nx:run-commands'
).options['key']
).toEqual('*:e');
});

it('should fallback to target key', () => {
expect(
readTargetDefaultsForTarget('build', nxJson, 'other-executor').options[
'key'
]
readTargetDefaultsForTarget('build', targetDefaults, 'other-executor')
.options['key']
).toEqual('t');
});

it('should return undefined if not found', () => {
expect(
readTargetDefaultsForTarget('other-target', nxJson, 'other-executor')
readTargetDefaultsForTarget(
'other-target',
targetDefaults,
'other-executor'
)
).toBeNull();
});

Expand Down
11 changes: 5 additions & 6 deletions packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ export class Workspaces {
const projectTargetDefinition = proj.targets[targetName];
const defaults = readTargetDefaultsForTarget(
targetName,
nxJson,
nxJson.targetDefaults,
projectTargetDefinition.executor
);

if (defaults) {
const defaults = nxJson.targetDefaults[targetName];
proj.targets[targetName] = mergeTargetConfigurations(
projectTargetDefinition,
defaults
Expand Down Expand Up @@ -872,7 +871,7 @@ export function mergeTargetConfigurations(

export function readTargetDefaultsForTarget(
targetName: string,
nxJson: NxJsonConfiguration<'*' | string[]>,
targetDefaults: TargetDefaults,
executor?: string
): TargetDefaults[string] {
if (executor) {
Expand All @@ -887,11 +886,11 @@ export function readTargetDefaultsForTarget(
`${targetName}${separator}${executor}`,
`*${separator}${executor}`,
targetName,
].find((x) => nxJson.targetDefaults[x]);
return key ? nxJson.targetDefaults[key] : null;
].find((x) => targetDefaults?.[x]);
return key ? targetDefaults?.[key] : null;
} else {
// If the executor is not defined, the only key we have is the target name.
return nxJson.targetDefaults[targetName];
return targetDefaults?.[targetName];
}
}

Expand Down
31 changes: 16 additions & 15 deletions packages/nx/src/project-graph/build-nodes/workspace-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { PackageJson } from 'nx/src/utils/package-json';
import { readJsonFile } from 'nx/src/utils/fileutils';
import { NxJsonConfiguration } from 'nx/src/config/nx-json';
import { TargetConfiguration } from 'nx/src/config/workspace-json-project-json';
import {
mergeTargetConfigurations,
readTargetDefaultsForTarget,
} from 'nx/src/config/workspaces';

export function buildWorkspaceProjectNodes(
ctx: ProjectGraphProcessorContext,
Expand Down Expand Up @@ -42,7 +46,7 @@ export function buildWorkspaceProjectNodes(
}
}

p.targets = mergeNxDefaultTargetsWithNxTargets(
p.targets = mergeNxTargetDefaultsWithNxTargets(
p.targets,
nxJson.targetDefaults
);
Expand Down Expand Up @@ -91,24 +95,21 @@ export function buildWorkspaceProjectNodes(
});
}

function mergeNxDefaultTargetsWithNxTargets(
function mergeNxTargetDefaultsWithNxTargets(
targets: Record<string, TargetConfiguration>,
defaultTargets: NxJsonConfiguration['targetDefaults']
targetDefaults: NxJsonConfiguration['targetDefaults']
) {
for (const targetName in defaultTargets) {
const target = targets?.[targetName];
if (!target) {
for (const target in targets) {
const defaults = readTargetDefaultsForTarget(
target,
targetDefaults,
targets[target].executor
);

if (!defaults) {
continue;
}
if (defaultTargets[targetName].inputs && !target.inputs) {
target.inputs = defaultTargets[targetName].inputs;
}
if (defaultTargets[targetName].dependsOn && !target.dependsOn) {
target.dependsOn = defaultTargets[targetName].dependsOn;
}
if (defaultTargets[targetName].outputs && !target.outputs) {
target.outputs = defaultTargets[targetName].outputs;
}
targets[target] = mergeTargetConfigurations(targets[target], defaults);
}
return targets;
}

0 comments on commit d78feda

Please sign in to comment.