Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug?]: [email protected] reformats package.json on every execute - which changes the file when there's expected formatting #6656

Open
1 task done
fbartho opened this issue Jan 16, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@fbartho
Copy link

fbartho commented Jan 16, 2025

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/npm

Additional context

Searching found a similar issue: #6282 -- unfortunately, while the title matches my concerns, it was marked as a duplicate of a different, more narrow issue: #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.

@fbartho fbartho added the bug Something isn't working label Jan 16, 2025
@miketheman
Copy link

Reference for historical context: #2636

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants