microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
150 stars 35 forks source link

Format document garbles file if black's `--color` option is left at default. #344

Closed StarLightPL closed 1 year ago

StarLightPL commented 1 year ago

black, 23.3.0 (compiled: yes) Python (CPython) 3.7.16 vscode and extension up to date.

When I use "Format document", the file gets garbled with diff output. If I add --no-color option to black args in extension settings, everything works normally.

test file:

a = [1,    2]

Result:

--- STDIN   2023-10-05 12:30:29.405426 +0000
+++ STDOUT  2023-10-05 12:30:29.406698 +0000
@@ -1 +1 @@
-a = [1,   2]
+a = [1, 2]

Black extension log output:

[info] Settings used to run Server:
[
    {
        "cwd": "/home/test-user/projects/test-project",
        "workspace": "file:///home/test-user/projects/test-project",
        "args": [
            "--diff"
        ],
        "path": [
            "black"
        ],
        "interpreter": [
            "/home/test-user/.pyenv/versions/3.7.16/envs/test-project/bin/python"
        ],
        "importStrategy": "fromEnvironment",
        "showNotifications": "off"
    }
]

2023-10-05 14:26:08.960 [info] Global settings:
{
    "cwd": "/mnt/c/Users/test-user/AppData/Local/Programs/Microsoft VS Code",
    "workspace": "/mnt/c/Users/test-user/AppData/Local/Programs/Microsoft VS Code",
    "args": [],
    "path": [
        "black"
    ],
    "interpreter": [],
    "importStrategy": "fromEnvironment",
    "showNotifications": "off"
}

2023-10-05 14:26:08.960 [info] sys.path used to run Server:
   /home/test-user/.vscode-server/extensions/ms-python.black-formatter-2023.4.1/bundled/tool
   /home/test-user/.pyenv/versions/3.7.16/lib/python37.zip
   /home/test-user/.pyenv/versions/3.7.16/lib/python3.7
   /home/test-user/.pyenv/versions/3.7.16/lib/python3.7/lib-dynload
   /home/test-user/.pyenv/versions/3.7.16/envs/test-project/lib/python3.7/site-packages
   /home/test-user/.vscode-server/extensions/ms-python.black-formatter-2023.4.1/bundled/libs
2023-10-05 14:26:08.962 [info] black --version
2023-10-05 14:26:08.962 [info] CWD Server: /home/test-user/projects/test-project
2023-10-05 14:26:09.106 [info] Version info for formatter running for /home/test-user/projects/test-project:
black, 23.3.0 (compiled: yes)
Python (CPython) 3.7.16

Command used: 2023-10-05 14:33:05.821 [info] black --diff --stdin-filename /home/test-user/projects/test-project/black_issue.py -

And if I add --no-color to black args in extension settings, it works as expected.

karthiknadig commented 1 year ago

@StarLightPL Do not use --diff with this extension. The extension handles this for you, we use black in a mode that allows us to get formatted content directly without using diff.

StarLightPL commented 1 year ago

@karthiknadig --diff is set by the extension itself (these are black log outputs), so I'd suggest reopening this issue. The only one added by me is --no-color. Diff is also not being set in pyproject.toml either. VSCode should then add both --no-diff and --no-color by default, otherwise it breaks on current versions of the code and editor.

StarLightPL commented 1 year ago

image

karthiknadig commented 1 year ago

@StarLightPL Extension does not set --diff, see below args are empty by default: https://github.com/microsoft/vscode-black-formatter/blob/b19ba19cd7fa77489d9341ce9a09768eeb8202eb/package.json#L80-L88

In your screen shot you only checked user settings. Please also check workspace and remote settings: image

StarLightPL commented 12 months ago

@karthiknadig checked the two, there's nothing in the arguments there. However, I also went through settings.json, and it looks like there was a leftover from previous config (before installing the extension):

    "python.formatting.blackArgs": [
        "--diff"
    ],

As these arguments are not available in the UI anymore, it went unnoticed. Removed it and it now works as expected.