bun icon indicating copy to clipboard operation
bun copied to clipboard

Respect current indentation in package.json when rewriting via `bun` CLI

Open Kruithne opened this issue 1 year ago • 11 comments

What version of Bun is running?

0.6.3

What platform is your computer?

Linux 5.15.90.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

  • Create a package.json manually using tabs or more/less than 2 spaces.
  • Run any bun command that modifies package.json, such as bun add zod

What is the expected behavior?

The indentation already present inside package.json will be respected.

What do you see instead?

The entire file is rewritten as 2-space indentation, regardless of what the file was using prior.

Additional information

I'm aware this is a minor issue, but it should also be fairly trivial to resolve and would be a quality-of-life change. When using Bun to add a package to a project - especially one that is already established - it's not ideal to have to reindent the file manually every single time.

There's an argument for "just leave it as 2-space indentation", but it's not always my own project and I can't make that upstream decision, and comitting a massive reindentation of the package.json upstream isn't a polite thing to do.

Kruithne avatar May 29 '23 10:05 Kruithne

Related: #1979, #2614

Electroid avatar May 30 '23 17:05 Electroid

That would be great. We’re using tabs, so reformatting package.json after each bun update is a little bit excessive.

maximal avatar Sep 15 '23 15:09 maximal

I'm running bun locally in a project that is designed to use node, and although we have prettier set up to autoformat package.json, the diff is useless until after that formatting happens, so this does add an extra (albeit small) step for me on each commit.

SnailCoil avatar Sep 15 '23 22:09 SnailCoil

1+ this would be an advantage over npm

pinkboid avatar Sep 26 '23 06:09 pinkboid

Any expected time, when this gets fixed?

vsnthdev avatar Oct 06 '23 04:10 vsnthdev

Related: #4563

andyexeter avatar Oct 12 '23 11:10 andyexeter

It's annoying having to reformat the package.json after each bun update. Either autodetect the current indentation and respect it, or provide a config switch to reformat or not.

tholu avatar Apr 09 '24 08:04 tholu

Although it's an old post, one thought is to infer these details from a .editorconfig placed at the project root, which other formatters like Prettier do if no configuration file is found.

In Bun's case, a configuration file is too heavy, though I think this would work as a viable, possible IDE-agnostic solution to this. If I knew Zig I would have done this but I do not, wallow wallow

cyrus01337 avatar Apr 09 '24 10:04 cyrus01337

An alternative I've been using is to use aliases for any package management command Bun uses. It's quite absurd but gets the job done for me, something like the following:

alias bunf="bun run format"
alias buni="bun install $@ && bunf"
alias bunr="bun remove $@ && bunf"
...

It's a bit cursed though and a real solution would do the problem it's justice.


EDIT: Pre-commit hooks are a better alternative if you don't mind the idea of having another runtime.

cyrus01337 avatar Apr 09 '24 10:04 cyrus01337

@cyrus01337 I agree, respecting the .editorconfig would be the best solution.

tholu avatar Apr 09 '24 11:04 tholu

It's annoying having to reformat the package.json after each bun update. Either autodetect the current indentation and respect it, or provide a config switch to reformat or not.

@tholu, I can totally relate to this.

maximal avatar Apr 09 '24 11:04 maximal

🎉

maximal avatar Jul 04 '24 09:07 maximal