[Bug?]: [email protected] reformats package.json on every execute - which changes the file when there's expected formatting
Self-service
- 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/npmAdditional 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.
Source: yarnpkg/berry