Skip to content

Commit f56e0a3

Browse files
authored
fix(devkit): do not write back to package.json when adding plugin and there are no changes (#28846)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> Generators adding plugins to `nx.json` can modify projects' `package.json` files with some JSON serialization changes even though there are no actual changes and prettier is not installed, so they shouldn't be formatted. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> Generators adding plugins to `nx.json` should not modify projects' `package.json` files with JSON serialization changes when there are no actual changes to make and prettier is not installed. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent 77d6704 commit f56e0a3

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

packages/devkit/src/utils/add-plugin.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,32 @@ describe('addPlugin', () => {
431431
'echo "Typechecking..." && nx build -p tsconfig.lib.json && nx build -p tsconfig.spec.json && echo "Done"'
432432
);
433433
});
434+
435+
it('should not touch the package.json when there are no changes to make', async () => {
436+
// package.json with mixed/bad indentation and array value in a single line
437+
// JSON serialization would have a standard indentation and would expand the array value into multiple lines
438+
const packageJsonContent = `{
439+
"name": "app1",
440+
"scripts": {
441+
"build": "tsc --build"
442+
},
443+
"keywords": ["foo", "bar", "baz"]
444+
}`;
445+
tree.write('app1/package.json', packageJsonContent);
446+
447+
await addPlugin(
448+
tree,
449+
graph,
450+
'@nx/next/plugin',
451+
createNodes,
452+
{
453+
targetName: ['build'],
454+
},
455+
true
456+
);
457+
458+
expect(tree.read('app1/package.json', 'utf-8')).toBe(packageJsonContent);
459+
});
434460
});
435461
});
436462

packages/devkit/src/utils/add-plugin.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function processProject(
233233
return;
234234
}
235235

236-
const replacedTargets = new Set<string>();
236+
let hasChanges = false;
237237
for (const targetCommand of targetCommands) {
238238
const { command, target, configuration } = targetCommand;
239239
const targetCommandRegex = new RegExp(
@@ -250,7 +250,7 @@ function processProject(
250250
? `$1nx ${target} --configuration=${configuration}$3`
251251
: `$1nx ${target}$3`
252252
);
253-
replacedTargets.add(target);
253+
hasChanges = true;
254254
} else {
255255
/**
256256
* Parse script and command to handle the following:
@@ -327,7 +327,7 @@ function processProject(
327327
: `$1nx ${target}$4`
328328
)
329329
);
330-
replacedTargets.add(target);
330+
hasChanges = true;
331331
} else {
332332
// there are different args or the script has extra args, replace with the command leaving the args
333333
packageJson.scripts[scriptName] = packageJson.scripts[
@@ -341,14 +341,16 @@ function processProject(
341341
: `$1nx ${target}$3`
342342
)
343343
);
344-
replacedTargets.add(target);
344+
hasChanges = true;
345345
}
346346
}
347347
}
348348
}
349349
}
350350

351-
writeJson(tree, packageJsonPath, packageJson);
351+
if (hasChanges) {
352+
writeJson(tree, packageJsonPath, packageJson);
353+
}
352354
}
353355

354356
function getInferredTargetCommands(

0 commit comments

Comments
 (0)