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

Error in Visual Studio when formatting with no newline at end of file #2542

Open
AdamYoblick opened this issue Oct 15, 2021 · 5 comments
Open
Labels
T: bug Something isn't working

Comments

@AdamYoblick
Copy link

AdamYoblick commented Oct 15, 2021

Describe the bug

When trying to format a document in Visual Studio using the black formatter, an error is thrown when there is no newline at the end of the file. Please see microsoft/PTVS#6752 for details.

To Reproduce

  1. Create a python application in Visual Studio
  2. Paste the following code (remove newline at end!)
# this is a comment
import os
import sys
spam(ham[1], {eggs: 2})
def foo(): pass


x = 1
y = 2
y = 2
# comment no newline
  1. Ensure black is the selected formatter (Tools -> Options -> Text Editor -> Python -> Formatting) is black
  2. Go to Edit -> Advanced -> Format Document
  3. Observe the following error:

image

Expected behavior

The file should be formatted correctly

Environment (please complete the following information):

  • Version: 21.9b0
  • OS and Python version: Windows, Python 3.9.7
@JelleZijlstra
Copy link
Collaborator

Is Visual Studio invoking black --diff? I think we print a special diff message saying "No newline at end of file".

@AdamYoblick
Copy link
Author

I'm not sure, let me dig into the exact command VS is running and I'll report back.

@AdamYoblick
Copy link
Author

Looks like the Python Tools extension invokes it, and indeed with --diff.

https://github.com/microsoft/PTVS/blob/main/Python/Product/PythonTools/PythonTools/Editor/Formatting/PythonFormatterBlack.cs#L30

@JelleZijlstra
Copy link
Collaborator

Thanks, then we're probably hitting this:

diff_lines.append("\\ No newline at end of file\n")
. We added that in #1897 for some fairly involved reasons.

@TBBle
Copy link
Contributor

TBBle commented Oct 17, 2023

This looks like a PTVS bug to me. \ is a valid patch mode, per the GNU diffutils docs.

Anyway, looks like this was resolved on the PTVS side (by ignoring that line of the diff and then manually doing newline fixups) so I think this should be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants