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

No new line at file end #2328

Closed
mironnn opened this issue Jun 11, 2021 · 12 comments
Closed

No new line at file end #2328

mironnn opened this issue Jun 11, 2021 · 12 comments
Labels
F: empty lines Wasting vertical space efficiently. S: awaiting response Waiting for futher information from OP S: needs repro Needs a functional reproduction T: bug Something isn't working

Comments

@mironnn
Copy link

mironnn commented Jun 11, 2021

Add a new line at the end of python file or not to remove it

Currently black removes extra blank line after python code at the end of file.
Tested inside the docker build from the repository.

root@8c79a371a51b:/# black test.py --check --diff --verbose
--- test.py	2021-06-11 09:53:10.665099 +0000
+++ test.py	2021-06-11 09:53:15.809714 +0000
@@ -1,6 +1,5 @@
 def main():
     print(11)


 main()
-
would reformat test.py
Oh no! 💥 💔 💥
1 file would be reformatted.
root@8c79a371a51b:/#

Desired style

Add a new line at the end of the file.

Flake8 example
https://www.flake8rules.com/rules/W292.html

@felix-hilden
Copy link
Collaborator

Hi, thanks for reporting! However, I can't reproduce this on the playground. I'm not familiar with the docker build though. Do you have the latest version?

@felix-hilden felix-hilden added F: empty lines Wasting vertical space efficiently. T: bug Something isn't working labels Jun 11, 2021
@mironnn
Copy link
Author

mironnn commented Jun 11, 2021

@felix-hilden thank you for the quick response!

Steps for reproduce:

  1. git clone https://github.com/psf/black.git
  2. cd black
  3. docker build -t test_black .
  4. docker run -it black_test bash
  5. apt update
  6. apt install vim
  7. make any python file and check with black

Could you please check with such steps?

I checked with docker, that builds and installs inside latest black.
Also tested with 21.6b0 from pypi

@mironnn
Copy link
Author

mironnn commented Jun 11, 2021

Or use old true python venv module

Steps:

  1. python3 -m venv .venv
  2. source .venv/bin/activate
  3. pip install black
  4. make a python code example with a blank line at the end
  5. run black to this file. The file will be modified and a blank line at the end of the file will be removed.

@felix-hilden
Copy link
Collaborator

felix-hilden commented Jun 11, 2021

Given that you are able to reproduce it with an ordinary virtual environment, I think we have something weird going on here. I have the current main (which is equivalent to the latest release) pulled and I created a file like you described:

def main():
    print(11)


main()

It does have the line ending despite what GitHub shows 😅 Both black test.py and black test.py --check --diff --verbose produce no modifications on the file. The same thing happens in a virtual environment with no configuration and a fresh install of Black.


I'm not trying to discredit your finding or say that it's invalid by the way, I hope we can figure out what's going on 😄

@felix-hilden felix-hilden added the S: needs repro Needs a functional reproduction label Jun 11, 2021
@ichard26 ichard26 added the S: awaiting response Waiting for futher information from OP label Jun 12, 2021
@ichard26
Copy link
Collaborator

I cannot reproduce using the docker container from main (aa31a11). After format, vim doesn't tell give me the noeol indicator as your bug report would suggest. There was a bug with the diff output not adding a missing terminating newline: #1662, but that was fixed a long time ago.

@mironnn
Copy link
Author

mironnn commented Jul 16, 2021

Please advise, what I'm doing wrong?

black_last_line.mp4

@TomFryers
Copy link
Contributor

TomFryers commented Feb 11, 2022

It looks to me like your file ends with two new‐line characters, and Black is removing the extra one.

@felix-hilden
Copy link
Collaborator

Indeed, could you verify @mironnn by perhaps reading the file in Python or using some other editor?

@ichard26
Copy link
Collaborator

ichard26 commented Feb 1, 2023

Given no one else has reported this bug and we haven't heard anything from the OP in nearly a year, I'm closing this. I'd be happy to reopen this if someone (else) could provide reproduction steps that work.

@ichard26 ichard26 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2023
@mironnn
Copy link
Author

mironnn commented Feb 1, 2023

@ichard26 you're kidding

image

@TomFryers
Copy link
Contributor

@mironnn I'm not quite sure what you're getting at. It seems to me like Black is functioning as intended.
annotated

@itsdani
Copy link

itsdani commented Apr 17, 2023

I have came across the same thing and I also thought black was removing the new line, but it isn't.
The explanation is that vim/neovim doesn't show the empty line at EOF, and always appends it by default. For more details check out this question and :h eol in vim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: empty lines Wasting vertical space efficiently. S: awaiting response Waiting for futher information from OP S: needs repro Needs a functional reproduction T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants