Skip to content

Black formatting leaves an extra line at the end of the file #1877

Closed

Description

@Tinche

Environment data

  • VS Code version: 1.23.1
  • 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:

  1. Open Python file
  2. Insert 10 empty lines at the end
  3. 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)

Activity

  1. brettcannon commented on Jun 6, 2018

    @brettcannon
    Member

    I can reproduce and the expected behaviour that Tin Tvrtković (@Tinche) found and I can verify that using black --diff does what is expected:

    --- tpn\tpn\tests\test_npm.py   2018-06-06 22:04:15.383923 +0000
    +++ tpn\tpn\tests\test_npm.py   2018-06-06 22:05:29.227218 +0000
    @@ -71,16 +71,5 @@
    
     def test_find_license():
         # XXX
         pass
    
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    
  2. brettcannon commented on Jun 13, 2018

    @brettcannon
    Member

    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.

  3. DonJayamanne commented on Jun 13, 2018

    @DonJayamanne

    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).

  4. removed this from the Jul 2018 milestone on Jul 19, 2018
  5. marko-knoebl commented on Aug 6, 2018

    @marko-knoebl

    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
    
    
  6. jaswilli commented on Oct 6, 2018

    @jaswilli

    Since #1950 has been closed is there another issue that can be tracked to see progress on this?

  7. brettcannon commented on Oct 9, 2018

    @brettcannon
    Member

    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.

  8. drorata commented on Aug 22, 2019

    @drorata

    Is there any update on this issue? I am still having it with Vscode 1.37.1 and vscode-python 2019.8.30787.

  9. brettcannon commented on Aug 22, 2019

    @brettcannon
    Member

    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.

  10. ashleybartlett commented on Aug 28, 2019

    @ashleybartlett

    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.

  11. roberthubblehq commented on Jan 10, 2020

    @roberthubblehq

    { "files.trimFinalNewlines": true } in settings.json seems to fix this.

  12. ghost removed on May 7, 2020
  13. locked as resolved and limited conversation to collaborators on May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-editor-*User-facing catch-allarea-formattingbugIssue identified by VS Code Team member as probable bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions