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

Could not get black formatter working on WSL #522

Closed
cogerk opened this issue Jun 26, 2024 · 15 comments
Closed

Could not get black formatter working on WSL #522

cogerk opened this issue Jun 26, 2024 · 15 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug triage-needed Issue is not triaged.

Comments

@cogerk
Copy link

cogerk commented Jun 26, 2024

Diagnostic Data

  • Python version (& distribution if applicable, e.g., Anaconda): 3.10.12
  • Type of virtual environment used (e.g., conda, venv, virtualenv, etc.): virtualenv
  • Operating system (and version): wsl2 ubuntu 22.04.3 via windows 11
  • VSCode 1.90.1
  • Version of tool extension you are using: extension v2024.2, pip freeze shows black==24.4.2,

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:

  • autoDocstring
  • Codeium
  • Django
  • Excel Viewer
  • IntelliCode
  • Jupyter
  • Jupyter Cell Tags
  • Jupyter Notebook
  • Jupyter Slide Show
  • Pylance
  • Pylint
  • Python
  • Python Debugger
  • Python Environment Manger
  • Rainbow CSV
  • WSL
  • Python Indent
  • Jinja

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!

@cogerk cogerk added the bug Issue identified by VS Code Team member as probable bug label Jun 26, 2024
@github-actions github-actions bot added the triage-needed Issue is not triaged. label Jun 26, 2024
@karthiknadig
Copy link
Member

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

@karthiknadig karthiknadig self-assigned this Jun 26, 2024
@cogerk
Copy link
Author

cogerk commented Jun 26, 2024

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
Copy link
Member

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

@cogerk
Copy link
Author

cogerk commented Jun 26, 2024

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
Copy link
Member

@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
Copy link
Author

cogerk commented Jun 27, 2024

Alrighty, here's what I have in that dir

image

@karthiknadig
Copy link
Member

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
Copy link
Author

cogerk commented Jun 27, 2024

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
Copy link
Member

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.

@cogerk
Copy link
Author

cogerk commented Jul 8, 2024

Thanks for your patience with me getting back to you on this!

I ended up switching to the pre-release version from the extension view, couldn't find it from the Install Specific Version command, not sure if that is a clue?

Regardless, it looks like neither the pre-release version nor Ruff were able to run the server. I also noticed that pylint is having trouble running as well:

image

Is there anyway that this could be a firewall issue?

@karthiknadig
Copy link
Member

Is there anyway that this could be a firewall issue?

Not likely. There is really nothing trying to access the network. It seems like the server also starts fine when you run it manually. Something else if failing.

Can you try this:

image

And see if you can find any error call stacks in the console log.

@cogerk
Copy link
Author

cogerk commented Jul 9, 2024

I am not sure what changed between yesterday and today, but it's running great suddenly! I opened the developer tools and saved my 'test.py' file to get the error call stacks, and it formatted. After a reboot, it's still formatting fine! Happy to do any legwork that would be helpful in tracking down what might have been going on, but I'm also OK to close if you are.

Screenshot 2024-07-09 080243
Screenshot 2024-07-09 080230

@cogerk
Copy link
Author

cogerk commented Jul 9, 2024

Yesterday after switching to the pre-release version, I only restarted vscode, not my computer, so that might have been it?

@karthiknadig
Copy link
Member

Thank you for letting me know. We can close, but if this occurs again, What I would need is the calls stacks from the the developer tools , if there are no errors reported in the tool logs.

FYI. If you have Ruff you can actually just remove black, and use ruff as your formatter. It implements black as the underlying formatter and is much faster than black as it is implemented in rust. You can also configure Ruff as your linter, and it implements linters rules beyond pylint and flake8

@karthiknadig karthiknadig closed this as not planned Won't fix, can't repro, duplicate, stale Jul 9, 2024
@cogerk
Copy link
Author

cogerk commented Jul 9, 2024

great tip, thanks very much for all your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug triage-needed Issue is not triaged.
Projects
None yet
Development

No branches or pull requests

2 participants