rushstack
rushstack copied to clipboard
[rush] Rush should normalize package.json fields by default
Summary
In a large repo, it's pretty annoying when package.json
files are not formatted consistently. When you have to do bulk updates across many files, it's annoying that each file seems to be arranged differently.
There already exist tools that can solve this, but since it's a very general problem, it would be great if (1) every Rush repo gets it for free, and (2) we can have the same familiar package.json
layout in every monorepo at work, without someone having to agree on a custom script/config and then roll it out to every team. Nobody has time for that.
Proposal
By default, Rush should normalize package.json fields somehow:
- sort the fields in a standard order (e.g. name/version first)
- sort the dependency lists in alphabetical order
- prettify the JSON indentation
- maybe do some basic linting
Rush should also provide a way to disable this feature completely (#1503), for situations where some other tool is used to format package.json files.
Related issues
This topic came up in some related Rush issues:
- #1503 and #1701 complained that Rush interferes with a custom solution for formatting package.json
- #2115 asked for sorting dependencies in package.json, but didn't consider other normalization
- #1512 proposes to generate a normalized package.json from a package-rush.json source file. If/when that is used, then the normalization would apply to an output file rather than an input file. But the normalization would be the same.
Implementation
Maybe Rush can reuse an existing NPM package (like sort-package-json or normalize-pkg or prettier-package-json) to do the actual sorting, if one of those is sufficient for our needs. On the other hand, the problem itself is pretty simple -- maybe we don't need to take a dependency. (?)
I'm not sure we'd want to provide a ton of options for configuration. If someone is very opinionated about having a package.json layout that is different from Rush's default, probably they will prefer to disable Rush's formatter entirely and do their own thing.
FWIW, I currently have to add "sort": "sort-package-json"
on every package.json
with a custom bulk command and sort-package-json
as a dependency of my rigging package to achieve this, so having it built-in would be awesome - looking forward to seeing how this ends up implemented!
Any news on this? 👀
package.json changes are a constant source of conflicts, having built in sorting would be a very welcome addition to rush update
, if nothing else then at least on the dependencies
when will this be resolved
We recently added this functionality to our monorepo using the prettier plugin, as @octogonz suggested above, and so far it's working out quite well.
I've documented the process for adding a prettier plugin while avoiding some bugbears that you can run into: https://github.com/microsoft/rushstack-websites/pull/79/files -- should be part of the official docs shortly, but if interested try it out and let me know if it works for you.