[Bug?]: [email protected] reformats package.json on every execute - which changes the file when there's expected formatting
Self-service
- [x] I'd be willing to implement a fix
Describe the bug
We have standards for our package.json files in our monorepo. We have a tool that executes on postinstall to patch up any accidental omissions or alert the dev to the issues.
For legibility, we sort certain fields (dependencies, devDependencies, peerDependencies), and we insert blank lines to separate certain entries in the scripts hash (to organize them into groups of related commands), and we inject some helper scripts.
This worked great until we switched to yarn-berry, but now our postinstall script only executes when a module was changed, but if the dev calls yarn again the package.json suddenly gets reformatted (first discovered on 3.6.4, but still happens on 4.6.0).
To reproduce
{
"name": "yarn-test",
"packageManager": "[email protected]",
"dependencies": {
"@types/node": "*"
},
"scripts": {
"meep": "echo meep",
"moop": "echo moop"
}
}
$ yarn install
โค YN0000: ยท Yarn 4.6.0
โค YN0000: โ Resolution step
โค YN0000: โ Completed
โค YN0000: โ Fetch step
โค YN0000: โ Completed
โค YN0000: โ Link step
โค YN0000: โ Completed
โค YN0000: ยท Done in 0s 32ms
$ cat package.json
{
"name": "yarn-test",
"packageManager": "[email protected]",
"dependencies": {
"@types/node": "*"
},
"scripts": {
"meep": "echo meep",
"moop": "echo moop"
}
}
Notice the unexpected loss of the newline between "meep" and "moop"
Environment
System:
OS: macOS 15.1.1
CPU: (10) arm64 Apple M1 Max
Binaries:
Node: 23.6.0 - /private/var/folders/01/4rfqmhmn03s0vby9xw8cz6qr0000gp/T/xfs-da12c7f8/node
Yarn: 4.6.0 - /private/var/folders/01/4rfqmhmn03s0vby9xw8cz6qr0000gp/T/xfs-da12c7f8/yarn
npm: 10.9.2 - ~/.nvm/versions/node/v23.6.0/bin/npm
Additional context
Searching found a similar issue: https://github.com/yarnpkg/berry/issues/6282 -- unfortunately, while the title matches my concerns, it was marked as a duplicate of a different, more narrow issue: https://github.com/yarnpkg/berry/issues/6184
The issue I'm filing here demonstrates that the problem is nothing as narrow as something related to a single field, but rather affects package.json more widely.
Please let me know if as a workaround I could hook our formatting/fixup-tool into a different lifecycle script? I couldn't find any that were called in this situation (appropriate or not).
My assertion: yarn shouldn't change package.json unless there was a functional configuration issue (eg. inserting a missing required field). -- If this assertion is incorrect, please do explain.
Reference for historical context: https://github.com/yarnpkg/berry/discussions/2636
To add another case I did not see mentioned yet: when yarn changes the package.json file, it invalidates the Docker build cache.
This behavior is driving me crazy, as yarns idea of formatting is different from my biome config. I have to run biome format --write every time i install something
A simple toggle in .yarnrc.yaml would fix this. I think this is fine default behavior, but in some projects I would like to turn it off.