fix(core): avoid mutating target defaults during task graph calculation (#27348)

Same copy of target default is shared along projects and is mutated,
leading to wrong task graph. Occur only when running without daemon,
probably because the daemon communication cause cloning by unmarshalling
the object.

Fix by deepcloning `targetDefaults`

Fixes https://github.com/nrwl/nx/issues/27346
This commit is contained in:
Elyahou Ittah 2024-08-09 07:08:32 +02:00 committed by GitHub
parent d3747e020f
commit 7c8bd7c3be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -644,10 +644,12 @@ function normalizeTargets(
const projectSourceMaps = sourceMaps[project.root];
const targetConfig = project.targets[targetName];
const targetDefaults = readTargetDefaultsForTarget(
const targetDefaults = deepClone(
readTargetDefaultsForTarget(
targetName,
nxJsonConfiguration.targetDefaults,
targetConfig.executor
)
);
// We only apply defaults if they exist
@ -718,6 +720,10 @@ function targetDefaultShouldBeApplied(
return !plugin?.startsWith('nx/');
}
function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
export function mergeTargetDefaultWithTargetDefinition(
targetName: string,
project: ProjectConfiguration,
@ -725,7 +731,7 @@ export function mergeTargetDefaultWithTargetDefinition(
sourceMap: Record<string, SourceInformation>
): TargetConfiguration {
const targetDefinition = project.targets[targetName] ?? {};
const result = JSON.parse(JSON.stringify(targetDefinition));
const result = deepClone(targetDefinition);
for (const key in targetDefault) {
switch (key) {