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

--stdout doubles newlines #604

Closed
Tracked by #527
akaihola opened this issue Aug 1, 2024 · 0 comments · Fixed by #605
Closed
Tracked by #527

--stdout doubles newlines #604

akaihola opened this issue Aug 1, 2024 · 0 comments · Fixed by #605

Comments

@akaihola
Copy link
Owner

akaihola commented Aug 1, 2024

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'

Originally posted by @shane-kearns in #527 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant