akaihola / darker

Apply black reformatting to Python files only in regions changed since a given commit. For a practical usage example, see the blog post at https://dev.to/akaihola/improving-python-code-incrementally-3f7a
https://pypi.org/project/darker/
Other
633 stars 55 forks source link

VSCode integration no longer working #527

Open kedhammar opened 11 months ago

kedhammar commented 11 months ago

VSCode issues:


Original description by @kedhammar:

Making a formal issue, referencing discussions #516 and #525.

Both threads now contains a workaround by @Ashblaze but it would be helpful if the issues are properly addressed by Darker.

akaihola commented 8 months ago

Thanks @kedhammar for creating this issue. I think I'll use this one to collect links to related discussions and issues and to form a picture of current issues with VSCode.

On that note, it seems #523 is related.

shane-kearns commented 5 months ago

With "black-formatter.args": ["--stdout", "--isort"] or "black-formatter.args": ["-d"], it almost works. However the file in vscode gains double line breaks when using SHIFT+ALT+F to format the file, and with format on save. This is caused by https://github.com/akaihola/darkgraylib/blob/875dbaf613556f27a4e7949b45fcd798d4ab91f1/src/darkgraylib/utils.py#L17-L22 and commenting out that function to always return "\n" makes it work.

What seems to be happening is that you read a file with \r\n line ending and output a file with \r\r\n line endings.

Debug wrapper script:

import subprocess
import sys

print(sys.argv, file=sys.stderr)
cp = subprocess.run(["bin/darker.exe"] + sys.argv[1:], stdout=subprocess.PIPE)

print(cp.stdout[:400], file=sys.stderr)
sys.stdout.buffer.write(cp.stdout)
sys.exit(cp.returncode)

Sample file to format

import sys
import collections

settings.json

{
    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.formatOnSave": true
    },
    "python.analysis.typeCheckingMode": "off",
    "black-formatter.path": ["py", "-3.11", "./scripts/darkerer.py"],
    "black-formatter.args": ["--stdout", "--isort"],
    "black-formatter.serverTransport": "stdio"
}

The black formatter console in vs code shows this:

2024-05-20 16:14:39.727 [info] ['./scripts/darkerer.py', '--stdout', '--isort', '--stdin-filename', 'c:\\XXX\\new-file.py', '-']
b'import collections\r\r\nimport sys\r\r\n'

2024-05-20 16:14:39.729 [info] [Trace - 4:14:39 PM] Received response 'textDocument/formatting - (7)' in 1037ms.

Running darker on the file normally (bin/darker --isort new-file.py) does not create bad line endings. The problem can be reproduced without VSCode thusly, stdout gets double line endings:

cat new-file.py | bin/darker --stdout --isort --stdin-file new-file.py - | py -3.11 -c "import sys; print(sys.stdin.buffer.read())"
b'import collections\r\n\r\nimport sys\r\n\r\n'
akaihola commented 3 months ago

[...] the file in vscode gains double line breaks when using SHIFT+ALT+F to format the file, and with format on save. [...]

Thanks @shane-kearns, I created a separate issue #604 for this. I'll keep this issue to collect links to related discussions and issues and to form a picture of current issues with VSCode.