-
Notifications
You must be signed in to change notification settings - Fork 4
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] package-lock.json
double-spaced when package.json
has a blank line after the opening brace
#3
Comments
There is something about the simplicity of reproducing and the weirdness of the behavior that made this special. This added a hard-to-explain amount of fun to my work day. Thank you for the gift of this bug 🙇 |
package-lock.json
double-spaced when package.json
has a blank line after the opening brace
It seems like it's not really a bug - it's picking up the first-used empty-line formatting from the package.json and replicating it in the lockfile. |
Is that intended? Are there other things NPM will do to "transfer" formatting from
|
FWIW I just tested with multiple blank lines. Each blank line added to
{
"name": "test-doublespace",
"dependencies": {"left-pad": "latest"}
} produces {
"name": "test-doublespace",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "test-doublespace",
"dependencies": {
"left-pad": "latest"
<snip> |
Yes, npm copies "is there a trailing newline" and also "indentation" - ie, tabs are preserved, as is the number of spaces you used. |
The indentation behavior is intuitive to me, but replicating the newlines certainly wasn't. Is the replication of newlines a new feature in NPM v8? v7 "transfers" indentation from I suppose I don't understand the use case for this behavior! |
I don't claim to understand it entirely either, I just assume it's an artifact of the normal formatting preservation behavior. |
I believe the culprit here is our json parse package, specifically these lines. This behavior looks like an unintended side effect of respecting indentation and newlines, but I think a fix for not double (or triple) spacing these lines would be nice. |
I transferred this issue to the |
Yeah, it's trying to figure out what kind of formatting you're trying to use, and stick to that. It doesn't maintain the context of every token in the json file, making the edit right at the point of change, like a human editing a file would. That's theoretically possible, but not without a much more elaborate setup that would probably be overkill in 99% of cases. I recommend closing this as |
Weird, I received a notification for this ticket 2 hours ago, but I don't see why 😕 Anyway, it's got me thinking about it again, so will dump some thoughts. Apologies if I re-hash anything that's settled.
I think we've established this is an intended feature, but what is the value in attempting to preserve, mimic, or transfer formatting patterns from a human-managed spec to the machine-managed lock of that spec? Does this feature add value, detract from value, or neither? When I first opened this issue, I was looking at the machine-managed lock file with extra blank lines and thinking "oh jeez, this is way different than I expected from my last projects. I've just stumbled upon a really weird bug. Can I trust this lock file?" From a machine-managed file, I expect predictability, and seeing format surprises inconsistent with my previous experience set off the alarm bells in my head. I tested some of the other environment management tools I use to see if they do anything like this. Conda creates lockfiles from environments, not from other files, so not a good comparison. For EDIT: Forgot to test |
Historically, some users have expressed a desire to have their package-lock.json use "the same" line break and indentation style as their package.json file, and to have that indentation and line break style preserved over time. This isn't only preference, it can be a matter of accessibility, and line breaks are OS specific, etc. Rather than add yet another config option, we opted to infer the style from the style that is in place in package.json. If that style isn't consistently applied, ok, well, it's limited in what it can infer, and you'll get the first line break and indentation it encounters in a way that is applied consistently. I would be extremely surprised if this ever changes. Maybe those people complaining about their package-lock not using 4-space or |
Thanks for providing the additional context! :) OK to close this? I only thought about it again because of the mystery notification I got from GitHub yesterday. |
Per npm/json-parse-even-better-errors#3 (comment), npm will implicitly update line endings, spacing, etc of `package-lock.json` so that it matches `package.json` I'd like to add this documentation because it does not seem to be documented anywhere, and as it is an implicit and non-configurable behavior, it took me a long time to figure out the cause.
## What Per npm/json-parse-even-better-errors#3 (comment), npm will implicitly update line endings, spacing, etc of `package-lock.json` so that it matches `package.json` ## Why I'd like to add this documentation because it does not seem to be documented anywhere, and as it is an implicit and non-configurable behavior, it took me a long time to figure out the cause. ## References npm/json-parse-even-better-errors#3 (comment)
Thanks for reporting the issue. This behavior, while weird, is an edge case and is cosmetic. |
Is there an existing issue for this?
This issue exists in the latest npm version
Current Behavior
When running
npm install
, thepackage-lock.json
output is double-spaced when a blank line exists between the opening brace and the first key ofpackage.json
.This
package.json
produces a double-spaced lockfile:and this one doesn't:
The double-spaced lockfile looks like this all the way to the end:
I've tried putting blank lines in other places, but have only been able to reproduce it with a blank line after the opening brace.
Expected Behavior
Blank lines are valid in JSON and are not considered to be "data", so I would expect any blank lines to have no impact on the locked data.
Steps To Reproduce
npm install
package-lock.json
. It should be double-spaced!Environment
The text was updated successfully, but these errors were encountered: