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

Could not get black formatter working on WSL #522

Open cogerk opened 6 days ago

cogerk commented 6 days ago

Diagnostic Data

Behaviour

Expected vs. Actual Behavior

I expected black to automatically format this test.py file on save, but nothing happens.

'this should be double quotes'

# this should get spaces between characters
x=1+1

Trying to restart the black server yields an error: image

Reproduction Steps:

Here's my user settings json:

{
    "notebook.defaultFormatter": "ms-python.black-formatter",
    "editor.defaultFormatter": "ms-python.black-formatter",
    "emmet.useInlineCompletions": true,
    "editor.formatOnSave": true,
    "notebook.formatOnSave.enabled": true,
    "notebook.formatOnCellExecution": true,
    "python.languageServer": "Pylance",
    "autoDocstring.customTemplatePath": "/mnt/c/Users/coger/Documents/src/venvs",
    "python.analysis.autoImportCompletions": true,
    "python.analysis.completeFunctionParens": true,
    "python.analysis.autoFormatStrings": true,

    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.formatOnSave": true,
        "editor.formatOnType": true
      },
}

And my remote json:

{
    "notebook.defaultFormatter": "ms-python.black-formatter",
    "editor.defaultFormatter": "ms-python.black-formatter",
    "emmet.useInlineCompletions": true,
    "editor.formatOnSave": true,
    "notebook.formatOnSave.enabled": true,
    "notebook.formatOnCellExecution": true,
    "python.languageServer": "Pylance",
    "autoDocstring.customTemplatePath": "/mnt/c/Users/coger/Documents/src/venvs",
    "python.analysis.autoImportCompletions": true,
    "python.analysis.completeFunctionParens": true,
    "python.analysis.autoFormatStrings": true,

    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.formatOnSave": true,
        "editor.formatOnType": true
      },
}

Logs:

Click here for detailed logs ``` 2024-06-26 14:50:15.734 [info] Name: Black Formatter 2024-06-26 14:50:15.734 [info] Module: black 2024-06-26 14:50:15.734 [info] Default formatter is set to ms-python.black-formatter for workspace /home/kc 2024-06-26 14:50:15.734 [info] Python extension loading 2024-06-26 14:50:15.734 [info] Waiting for interpreter from python extension. 2024-06-26 14:50:15.734 [info] No interpreter found from setting black-formatter.interpreter 2024-06-26 14:50:15.734 [info] Getting interpreter from ms-python.python extension for workspace /home/kc 2024-06-26 14:50:15.734 [info] Python extension loaded 2024-06-26 14:50:15.734 [info] Interpreter from ms-python.python extension for /home/kc: /home/kc/src/.venv/bin/python 2024-06-26 14:50:15.734 [info] Workspace settings for /home/kc (client side): { "cwd": "/home/kc", "workspace": "file:///home/kc", "args": [], "path": [], "interpreter": [ "/home/kc/src/.venv/bin/python" ], "importStrategy": "useBundled", "showNotifications": "off" } 2024-06-26 14:50:15.734 [info] No interpreter found from setting black-formatter.interpreter 2024-06-26 14:50:15.734 [info] Getting interpreter from ms-python.python extension for workspace /home/kc 2024-06-26 14:50:15.734 [info] Interpreter from ms-python.python extension for /home/kc: /home/kc/src/.venv/bin/python 2024-06-26 14:50:15.734 [info] Workspace settings for /home/kc (client side): { "cwd": "/home/kc", "workspace": "file:///home/kc", "args": [], "path": [], "interpreter": [ "/home/kc/src/.venv/bin/python" ], "importStrategy": "useBundled", "showNotifications": "off" } 2024-06-26 14:50:15.734 [info] Global settings (client side): { "cwd": "/mnt/c/Users/coger/AppData/Local/Programs/Microsoft VS Code", "workspace": "/mnt/c/Users/coger/AppData/Local/Programs/Microsoft VS Code", "args": [], "path": [], "interpreter": [], "importStrategy": "useBundled", "showNotifications": "off" } 2024-06-26 14:50:21.122 [info] Formatting requested before server has started. ```

Outcome When Attempting Debugging Steps:

ctrl-F "command" and "running" in my trace log output found 0 results

Extra Details

Other Extensions:

I also posted this on stack overflow first: https://stackoverflow.com/questions/78634195/wsl2-vscode-black-extension-isnt-formatting-on-save

Thanks for your insights!

karthiknadig commented 6 days ago

Try this setting: black-formatter.serverTransport to stdio. if it is already stdio then try pipe

cogerk commented 6 days ago

Thanks for the quick response.

I tried both stdio and pipe and it looks like I get the same log message after rebooting/saving a file I want to format: 2024-06-26 15:45:57.945 [info] Formatting requested before server has started.

Here's my new settings file:

{
    "workbench.colorTheme": "Nu Disco Dark",
    "codeium.enableConfig": {
        "*": true,
        "markdown": true
    },
  "remote.autoForwardPortsSource": "hybrid",
  "editor.defaultFormatter": "ms-python.black-formatter",
  "editor.formatOnSave": true,
  "notebook.defaultFormatter": "ms-python.black-formatter",
  "notebook.formatOnSave.enabled": true,
  "python.languageServer": "Pylance",
  "python.analysis.typeCheckingMode": "basic",
  "python.analysis.inlayHints.variableTypes": true,
  "python.analysis.autoImportCompletions": true,
  "python.analysis.inlayHints.pytestParameters": true,
  "python.analysis.inlayHints.functionReturnTypes": true,
  "black-formatter.serverTransport": "pipe",

  "[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter",
    "editor.formatOnSave": true,
    "editor.formatOnType": true
  },

}
karthiknadig commented 6 days ago

I will bed the full log to see exactly what the error is.

cogerk commented 6 days ago

Sure thing, is the log given in the issue post missing information? (under 'Click Here for Detailed Log') How can I get that full log for you?

karthiknadig commented 6 days ago

@cogerk Sorry I missed that. Can you check if there is a bundled/tool directory here: /home/kanadig/.vscode-server/extensions/ms-python.black-formatter-2024.3.11501016/bundled/tool I am using insiders version the version you have might be different.

Typically, there should be a server start line: image

cogerk commented 5 days ago

Alrighty, here's what I have in that dir

image

karthiknadig commented 5 days ago

In a terminal can you run this:

/home/kc/src/.venv/bin/python ~/.vscode-server/extensions/ms-python.black-formatter-2024.2.0/bundled/tool/lsp_server.py

It should just run and not crash.

This is a very strange case where nothing is erroring out in a way that is telling me what is failing. Typically, the lsp_server.py script fails or the communication between VS Code and LSP server fails. When that happens there is a obvious "Error" that you can see in the logs. But in this scenario, it is not running anything.

cogerk commented 5 days ago

It looks like it runs and just hangs, but it doesn't crash! Is that expected behavior? It doesn't auto format on save while running either

karthiknadig commented 5 days ago

That is expected. This means it can run, something else is broken. Can you try installing the pre-release version? you can use this command or from the extension view you can switch to pre-release. image

If that also doesn't work, we can try an equivalent extension `Ruff', and see if that loads.