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] npm v7 does not preserve indentation style #1662

Closed
valtlai opened this issue Aug 12, 2020 · 13 comments
Closed

[BUG] npm v7 does not preserve indentation style #1662

valtlai opened this issue Aug 12, 2020 · 13 comments
Assignees
Labels
Bug thing that needs fixing Release 7.x work is associated with a specific npm 7 release

Comments

@valtlai
Copy link

valtlai commented Aug 12, 2020

Current Behavior:

The indentation style in package.json and package-lock.json is not preserved.

Expected Behavior:

For example, if package.json is indented using tabs, the tabs should be preserved and package-lock.json should use tabs too. Similarly, the number of the spaces used for indentation should be preserved.

This is the behavior in npm v6.

Steps To Reproduce:

  1. Create a package.json with tab indentation:
{
	"private": true
}
  1. Install a package:
npm install globals
  1. See how the tabs have gone:
{
  "private": true,
  "dependencies": {
    "globals": "^13.2.0"
  }
}

Environment:

  • OS: macOS 10.15.6
  • Node: 14.7.0
  • NPM: 7.0.0-beta.4
@valtlai valtlai changed the title [BUG] npm v7 does not reserve tab indentation [BUG] npm v7 does not preserve tab indentation Aug 12, 2020
@valtlai valtlai changed the title [BUG] npm v7 does not preserve tab indentation [BUG] npm v7 does not preserve indentation Aug 12, 2020
@valtlai valtlai changed the title [BUG] npm v7 does not preserve indentation [BUG] npm v7 does not preserve indentation style Aug 12, 2020
@ljharb
Copy link
Contributor

ljharb commented Aug 12, 2020

oof, this is a critical showstopper regression :-/

@MylesBorins
Copy link
Contributor

I've seen this reproduce locally

@MylesBorins MylesBorins added Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release labels Aug 12, 2020
@isaacs
Copy link
Contributor

isaacs commented Aug 12, 2020

Yep, that's a bug.

@darcyclarke darcyclarke added Bug thing that needs fixing beta-bug labels Aug 14, 2020
@darcyclarke darcyclarke removed the Needs Triage needs review for next steps label Aug 14, 2020
isaacs added a commit that referenced this issue Aug 20, 2020
- Load root project `package.json` when running loadVirtual.
  Fix: #1690
  Fix: #1693

- Fetch metadata from registry when loading tree from outdated
  package-lock.json file.  This avoids a situation where a lockfile or
  shrinkwrap from npm v5 would result in deleting dependencies on
  install.

- Preserve `package.json` and `package-lock.json` formatting in all
  places where these files are written.
  Fix: #1662
@valtlai
Copy link
Author

valtlai commented Sep 5, 2020

The indentation style of the existing files is now preserved, but a newly created package-lock.json does not use the indentation style of package.json.

ruyadorno added a commit to npm/arborist that referenced this issue Sep 24, 2020
package-lock.json files should follow the same indentation used on
package.json files.

This commit fixes the issue by forwarding the proper indent selected for
package.json to Shrinkwarp.save method. Also, added tests asserting
appropriate tab-indentation for both files.

Fixes: npm/cli#1662
@ruyadorno
Copy link
Contributor

hi @valtlai thanks for reporting these issues 😄 the package-lock.json auto-format using same indentation style from package.json have now landed in the latest beta release v7.0.0-beta.13 - as usual you can try it out with:

npm i -g npm@next-7

Let us know if you find more issues 😊 Thanks again!

@9oelM
Copy link

9oelM commented Jun 21, 2021

I am using npm@7.18.1 and able to reproduce this issue. For example: 9oelM/youtube-lite@7104320. This is actually a commit made by Renovate bot, and it uses npm 7. You can see that package-lock.json has the same number of lines added and deleted.. which just means that it does not preserve indentation.

image

If you are in doubt of the version Renovate uses, there are also other commits that I made; for example: 9oelM/youtube-lite@f3d1460
This commit was made by myself and it still does not respect existing indent.
image

@gjoseph
Copy link

gjoseph commented Oct 27, 2021

@ruyadorno This issue seems to have popped back up (I'm using 7.24.2)

@gjoseph
Copy link

gjoseph commented Oct 27, 2021

... and reverting to 7.24.1 fixes that problem

@gjoseph
Copy link

gjoseph commented Oct 27, 2021

(I've also tried 8.1.1 without success)

@isaacs
Copy link
Contributor

isaacs commented Oct 29, 2021

@gjoseph In npm v8, it will format package-lock.json to match package.json.

I'm not saying that's the right thing to do, and I somewhat agree that, in lieu of an explicit format option, is should preserve whatever is there. But that's what's happening now. If you want your package-lock.json to be indented a particular way, then you have to have your package.json indented in the same fashion, and it will keep them in sync.

If you'd like it to do a different thing, please open a new issue. The OP here is fixed, and what you're proposing (while imo reasonable) is a new change from that design.

@gjoseph
Copy link

gjoseph commented Oct 29, 2021

Thanks for the response @isaacs - the odd thing is, both my package-lock.json and package.json are formatted by the same prettier rules, as far as I know (I have a prettier-check in CI, which is how I noticed this change)

I did open #3948 btw 😬

@gjoseph
Copy link

gjoseph commented Oct 30, 2021

Well, I don't know what I was doing differently the other day, because I'm not able to reproduce now -- this may have been related to Lerna, which I had (probably wrongly) assumed was only causing different lock issues in the sub-packages lock files (i.e after reverting to 7.24.1, only the issues in sub-packages remained, so I assumed npm itself was causing the issues I saw in the root look file)

I've now gotten rid of Lerna altogether and don't see this issue again.

@ephemer
Copy link

ephemer commented Nov 13, 2021

I am experiencing this bug with 8.1.0.

Normally our package-lock.json is indented with 2 spaces (no idea why) and package.json is indented with 4 spaces. If I add a package npm i some-package then npm will rewrite package-lock.json with 4 spaces (presumably to match package.json) but if I run npm install by itself then it will re-format to the 2 spaces version. I don't really mind which it stays with, but it's quite annoying to have to catch this in every PR before merge.

@isaacs FYI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Release 7.x work is associated with a specific npm 7 release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants