Skip to content

Commit

Permalink
fix(core): target defaults from specifier should not be clobbered by …
Browse files Browse the repository at this point in the history
…name based target defaults (#21539)
  • Loading branch information
AgentEnder authored Feb 2, 2024
1 parent 727e15b commit 4a5a92f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,5 +295,67 @@ describe('target-defaults plugin', () => {
}
`);
});

it('should not be overridden by target name based default', () => {
memfs.vol.fromJSON(
{
'project.json': JSON.stringify({
name: 'root',
targets: {
echo: {
executor: 'nx:run-commands',
options: {
command: 'echo 1',
},
},
echo2: {
executor: 'nx:run-commands',
options: {
command: 'echo 2',
},
},
},
}),
},
'/root'
);

context.nxJsonConfiguration.targetDefaults = {
'nx:run-commands': {
options: {
cwd: '{projectRoot}',
},
},
echo: {},
};

expect(createNodesFn('project.json', undefined, context))
.toMatchInlineSnapshot(`
{
"projects": {
".": {
"targets": {
"echo": {
"options": {
"cwd": "{projectRoot}",
},
},
"echo2": {
"options": {
"cwd": "{projectRoot}",
},
},
"nx:run-commands": {
"options": {
"cwd": "{projectRoot}",
},
Symbol(ONLY_MODIFIES_EXISTING_TARGET): true,
},
},
},
},
}
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ export const TargetDefaultsPlugin: NxPluginV2 = {
targetNames.add(defaultSpecifier);

for (const targetName of targetNames) {
modifiedTargets[targetName] = structuredClone(
targetDefaults[defaultSpecifier]
);
// Prevents `build` from overwriting `@nx/js:tsc` if both are present
// and build is specified later in the ordering.
if (!modifiedTargets[targetName] || targetName !== defaultSpecifier) {
modifiedTargets[targetName] = JSON.parse(
JSON.stringify(targetDefaults[defaultSpecifier])
);
}
// TODO: Remove this after we figure out a way to define new targets
// in target defaults
if (!projectDefinedTargets.has(targetName)) {
Expand Down

0 comments on commit 4a5a92f

Please sign in to comment.