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

Bun mangles package.json formatting in some cases #9351

Open
neodon opened this issue Mar 11, 2024 · 0 comments
Open

Bun mangles package.json formatting in some cases #9351

neodon opened this issue Mar 11, 2024 · 0 comments
Labels
bug Something isn't working npm Something that relates to the npm-compatible client

Comments

@neodon
Copy link

neodon commented Mar 11, 2024

What version of Bun is running?

1.0.30+1424a196f

What platform is your computer?

Linux 5.15.133.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

The bug seems to occur when removing a package dependency when it is the last dependency for that JSON key. Instead of removing the section and leaving the rest of the package.json file intact, it completely reformats the entire file, re-arranging the top-level keys in a seemingly random order.

I'm generally in favor of normalizing the formatting in JSON files, but package.json is one exception. I want to keep important information, including scripts, near the top.

I haven't encountered this behavior with any other package managers (npm, yarn, pnpm).

  1. Start with the following package.json contents:
{
  "name": "@neodon/bun-is-awesome",
  "version": "0.1.0",
  "private": true,
  "type": "module",
  "module": "index.ts",
  "scripts": {
    "hello": "echo hello"
  },
  "dependencies": {
    "is-number": "^7.0.0"
  },
  "devDependencies": {
    "@types/bun": "latest"
  }
}
  1. Run bun install for the initial installation of packages.
  2. Run bun remove is-number.

What is the expected behavior?

Since there are no remaining packages under dependencies, I expect that section to be removed without affecting any other sections or top-level key ordering.

{
  "name": "@neodon/bun-is-awesome",
  "version": "0.1.0",
  "private": true,
  "type": "module",
  "module": "index.ts",
  "scripts": {
    "hello": "echo hello"
  },
  "devDependencies": {
    "@types/bun": "latest"
  }
}

What do you see instead?

The top-level keys are re-ordered.

{
  "name": "@neodon/bun-is-awesome",
  "version": "0.1.0",
  "module": "index.ts",
  "devDependencies": {
    "@types/bun": "latest"
  },
  "private": true,
  "scripts": {
    "hello": "echo hello"
  },
  "type": "module"
}

Additional information

The example I gave is fairly small. The reformatting is more disconcerting with a larger package.json file that has many devDependencies entries but maybe only one dependencies entry that needs removed (for example).

I mostly run into this when I have a project with zero dependencies entries and I accidentally add a new package as a regular dependency instead of a dev dependency, then remove it. Or vice versa.

@neodon neodon added the bug Something isn't working label Mar 11, 2024
@Electroid Electroid added the npm Something that relates to the npm-compatible client label Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working npm Something that relates to the npm-compatible client
Projects
None yet
Development

No branches or pull requests

2 participants