You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Black formatting leaves an extra line at the end of the file #1877
Extension version (available under the Extensions sidebar): 2018.5.0
OS and version: macOs High Sierra
Python version (& distribution if applicable, e.g. Anaconda): CPython 3.6.5
Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
Relevant/affected Python packages and their versions: black-18.6b1
Actual behavior
Formatting using black leaves two empty lines at the end of the file, if the file has more than 1 empty line already.
Expected behavior
Formatting should leave just one line, and this is what happens when I run black manually from the terminal.
Steps to reproduce:
Open Python file
Insert 10 empty lines at the end
Activate "Format document" with black set as the formatter
Logs
Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)
##########Linting Output - flake8##########
20,1,W,W391:blank line at end of file
Output from Console under the Developer Tools panel (toggle Developer Tools on under Help)
Reacted by Tom Milligan, uriva, dylan-kerr, Dror Atariah, Michael Copland, Ashley Bartlett, Quentin Chateau, Diego Alloza González, Lauri Kajan, Phil Winder and 8 more
Don Jayamanne (@DonJayamanne) so when I tested this I added the blank lines, we just weren't stripping them out. So I don't think it's a case of adding as much as not removing.
FYI, I've also come across cases where this will add an extra newline (and not just fail to remove it):
If there's no newline at the end of the file yet and the last line contains something that will be reformatted by black then auto-formatting will add two blank lines.
example:
a=0
will be reformatted to:
a=0
Reacted by Jason Williams, Poiappoi and alextat120
Dror Atariah (@drorata) not specifically. Other bugs have taken precedence. As always, if the 👍 gets high enough we will prioritize fixing this appropriately (this issue currently sits at #23on that list).
Otherwise we will happily review a PR that tries to fix this if someone is motivated enough to provide one.
I have just run into this issue. From the cli, black will change the file correctly. When saving, It will format the rest of the document, and leaves the extra blank line/ I even see the same flake8 picking this up as a formatting error.
If I have no blank lines, it adds one, if I have multiple it removes all but 2. I tried this out with a new file and found the same result.
This caught me out, as our CI pipeline runs black --check and fails. Which it did.
Reacted by Wei Zhang, Diego Alloza González, Spencer Tollefson, Holger Hoefling, Norm Ross and roberthubblehq
Activity
brettcannon commentedon Jun 6, 2018
I can reproduce and the expected behaviour that Tin Tvrtković (@Tinche) found and I can verify that using
black --diffdoes what is expected:brettcannon commentedon Jun 13, 2018
Don Jayamanne (@DonJayamanne) so when I tested this I added the blank lines, we just weren't stripping them out. So I don't think it's a case of adding as much as not removing.
DonJayamanne commentedon Jun 13, 2018
I think its best to use an npm package (e.g. https://www.npmjs.com/package/diff) rather than parsing diff patches manually.
Moving to July
This will need to be done as part of #1950 as parsing of diff patches is a shared block of code (used by refactoring and sorting of imports).
marko-knoebl commentedon Aug 6, 2018
FYI, I've also come across cases where this will add an extra newline (and not just fail to remove it):
If there's no newline at the end of the file yet and the last line contains something that will be reformatted by black then auto-formatting will add two blank lines.
example:
will be reformatted to:
jaswilli commentedon Oct 6, 2018
Since #1950 has been closed is there another issue that can be tracked to see progress on this?
brettcannon commentedon Oct 9, 2018
Jason Williams (@jaswilli) this issue is tracking the overall issue. If you're curious about the work in the language server to potentially take this functionality over, then microsoft/python-language-server#143 has that request.
drorata commentedon Aug 22, 2019
Is there any update on this issue? I am still having it with Vscode 1.37.1 and vscode-python 2019.8.30787.
brettcannon commentedon Aug 22, 2019
Dror Atariah (@drorata) not specifically. Other bugs have taken precedence. As always, if the 👍 gets high enough we will prioritize fixing this appropriately (this issue currently sits at #23 on that list).
Otherwise we will happily review a PR that tries to fix this if someone is motivated enough to provide one.
ashleybartlett commentedon Aug 28, 2019
I have just run into this issue. From the cli, black will change the file correctly. When saving, It will format the rest of the document, and leaves the extra blank line/ I even see the same flake8 picking this up as a formatting error.
If I have no blank lines, it adds one, if I have multiple it removes all but 2. I tried this out with a new file and found the same result.
This caught me out, as our CI pipeline runs
black --checkand fails. Which it did.roberthubblehq commentedon Jan 10, 2020
{ "files.trimFinalNewlines": true }in settings.json seems to fix this.