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

JSON formatter removes newline at end of file #176246

Closed
Chlumsky-Ceros opened this issue Mar 6, 2023 · 7 comments
Closed

JSON formatter removes newline at end of file #176246

Chlumsky-Ceros opened this issue Mar 6, 2023 · 7 comments
Assignees

Comments

@Chlumsky-Ceros
Copy link

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.76.0
  • OS Version: Windows 10

Steps to Reproduce:

  1. Open a JSON file
  2. Right click the editor window and press "Format Document"
  3. There is no newline at the end of the file

All text files are supposed to end with a newline character.

@aeschli
Copy link
Contributor

aeschli commented May 3, 2023

This is controlled by the setting files.insertFinalNewline. Set it to true if you want text files to end with a newline character.

@aeschli aeschli closed this as completed May 3, 2023
@Chlumsky-Ceros
Copy link
Author

No, that's completely different. I don't want any changes to happen at the time of saving the file. The formatter actually removes the newline if it was previously present. Reopen.

@aeschli
Copy link
Contributor

aeschli commented May 9, 2023

The setting is not only used on save, but also observed by formatters.
If you want the JSON formatter to not remove the new line, enable this setting.

@aeschli
Copy link
Contributor

aeschli commented May 9, 2023

Oh, if you want the JSON formatter to preserve all new lines (including the one at the end of the line) you can also set the setting "json.format.keepLines": true

@Chlumsky-Ceros
Copy link
Author

Ok, it's nice that the formatter observes files.insertFinalNewline but using that option removes my ability to ever generate a file without a final newline if I need to do so.

Enabling json.format.keepLines breaks the formatter for me - it literally doesn't do anything then.

In any case, a text file without a final newline is malformed and I don't believe that in the default configuration, the JSON formatter should output a malformed document.

@aeschli
Copy link
Contributor

aeschli commented May 9, 2023

  • json.format.keepLines will fix the indentation as well as spaces around :
  • a JSON file without a final newline is valid. Check out the JSON spec. Adding a new line is a styling guideline and required by some repositories

@Chlumsky-Ceros
Copy link
Author

Then, the json.format.keepLines setting is irrelevant to this. A JSON being valid without a final newline is also irrelevant, I am not arguing about that (and it's because JSON ignores all whitespace). What I am saying is that all text files are supposed to have a final newline, it's not just a guideline and I don't understand how you can not know that, see for example https://stackoverflow.com/a/729795.

None of the settings you suggested solved the issue, as I explained. Even if we pretended that whether or not there should be newline at the end is a guideline (it's not), the formatter doesn't have to add it, but it definitely shouldn't remove it, which it does. So why are you so insistent on not fixing this bug? It would probably be a one-line fix.

How about this: instead of the formatter deciding whether to add it based on the files.insertFinalNewline setting, it would instead use the inverse of files.trimFinalNewlines, I think that would be a nice compromise.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants