microsoft / vscode-black-formatter

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

When I provide path to black, the extension adds --version arg that cannot be removed #425

Closed janvorac-solargis closed 5 months ago

janvorac-solargis commented 5 months ago

I wanted to use the newest black already, so I installed it to my venv and set the path for the formatter extension in settings.json:

"black-formatter.path": [
        "~/path/to/.venv/bin/black"
    ]

After I run "format document", the code gets rewritten to

black, 24.1.0 (compiled: yes)
Python (CPython) 3.8.13

When I check the "OUTPUT" panel in VSC, I see that it ran this command:

~/path/to/.venv/bin/black --version --stdin-filename ~/path/to/file.py -

Which makes sense, because when you run black --version, you obtain the two lines I pasted above. Obviously, you don't want to rewrite your code with black's version.

When I set args for black in settings.json

    "black-formatter.args": [
        "--stdin-filename"
    ]

I see in the output, that this gets appended to the two arguments that were already there:

~/path/to/.venv/bin/black --version --stdin-filename --stdin-filename ~/path/to/file.py -

From which I conclude that the extension adds --version --stdin-filename by itself and it cannot be turned off. Could you please verify this and eventually fix it?

Thank you!

karthiknadig commented 5 months ago

@janvorac-solargis Can you share your entire logs and your settings?

We do a --version check once at the beginning to get version but actual formatting run does not include that. We automatically add --stdin-filename to actually perform formatting, this is needed to do formatting with VS Code.

This is how formatting in VS Code works: VS Code gives us the changes as plain string, and we use stdin to pass that to black to format. Return the formatted changes VS Code to save to disk.

So, --version and --stdin-filename (or equivalent) are needed for the extension to operate. The args setting is there for you to configure some CLI switches, like line length, config, skip source first line, etc.

karthiknadig commented 5 months ago

We have a PR https://github.com/microsoft/vscode-black-formatter/pull/426 that updates the version shipped with extension to latest black (24.1.1)

janvorac-solargis commented 5 months ago

Hi, my settings.json:

{
    "workbench.colorTheme": "Default Dark Modern",
    "editor.inlineSuggest.enabled": true,
    "terminal.integrated.fontSize": 15,
    "chat.editor.fontSize": 16,
    "editor.fontSize": 15,
    "terminal.integrated.inheritEnv": false,
    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.formatOnSave": false
    },
    "redhat.telemetry.enabled": true,
    "github.copilot.enable": {
        "*": true,
        "plaintext": true,
        "markdown": false,
        "scminput": false,
        "python": true
    },
    "isort.showNotifications": "onError",
    "isort.check": true,
    "isort.args": [
        "--profile",
        "black",
        "--skip-gitignore",
        "--line-length",
        "88"
    ],
    "window.zoomLevel": 1,
    "black-formatter.showNotifications": "always",
    "black-formatter.path": [
        "/home/anonymized/.venv/bin/black"
    ]
}

The output of the Black Formatter extension

2024-01-29 19:00:26.023 [info] [Trace - 7:00:26 PM] Sending request 'textDocument/formatting - (1)'.
2024-01-29 19:00:26.034 [info] [Trace - 7:00:26 PM] Received notification 'window/logMessage'.
2024-01-29 19:00:26.034 [info] /home/anonymized/.venv/bin/black --version --stdin-filename /home/anonymized/path/to/file/test_lambda.py -
2024-01-29 19:00:26.035 [info] [Trace - 7:00:26 PM] Received notification 'window/logMessage'.
2024-01-29 19:00:26.035 [info] CWD Server: /home/anonymized/code/sg2-eval-all-computation-nodes-and-infra
2024-01-29 19:00:26.213 [info] [Trace - 7:00:26 PM] Received response 'textDocument/formatting - (1)' in 190ms.
2024-01-29 19:00:26.232 [info] [Trace - 7:00:26 PM] Sending notification 'textDocument/didChange'.

I guess I should add that I am using VSC 1.86.0-insider with Black Formatter v2023.9.10291734 (pre-release)

janvorac-solargis commented 5 months ago

Oddly enough, when I try to replicate on a simple file (just a print("A")) in my /home dir, the formatter worked just fine. Perhaps the length of the file plays a role?

karthiknadig commented 5 months ago

@janvorac-solargis Thanks for the details. This is a bug, I am able to reproduce.