-
Notifications
You must be signed in to change notification settings - Fork 36
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
Black Formatter are hanging when trying to format on save #317
Comments
@maolike would it be possible to share a repro for this. we will need the type of environment, operating system, python versions, and if you can share your project itself it might help. |
Not sure if it's related but Black has stopped formatting on save on my system, seemingly after latest VSCode update. |
@chrisandrewmann Can you share what you see in the logs? Output > Black Formatter for logs. Also, are you using format on save or do you trigger formatting using one of the following shortcuts, if yes then which one: |
@karthiknadig I'm using format on save, not using shortcuts. Will share the logs shortly when I figure out how to. |
@chrisandrewmann Please also share your settings. |
@karthiknadig Thanks, I should have checked the logs before. It's now working. Previously I had: Changed to: |
This problem only reappears in my local environment.My project is just a simple python file Operating system:版本 Windows 10 专业版 Python version: Python 3.11.1VS Code:Version: 1.82.2 (system setup) |
@maolike In the logs do you see a formatting trigger? please share logs form Output > Black Formatter. |
@karthiknadig
|
@maolike I will need more details like the file contents that you were trying to format, virtual environments that you were using, your settings etc. Also, can you verify if You can try this command in windows"
Example:
|
This is my file contents:
What does virtual environment mean?My settings:
The encoding of the file is UTF8.I can format that file using command line. |
I have a similar behavior since the last update (running vscode 1.84.2, black formatter extension v2023.6.0) now. I have "format on save" active and what it does, no matter if I changed something in a file or not, is running black 57 times (at least I see the following block that often in the formatter output):
This takes about 11 seconds which makes saving a file a pain because it really blocks the saving so I need to wait a long to test small code changes. Edit: the time used is not constant. After a "reload window" and saving again, it had 119 of these blocks in the output and took 52s. Some more log output:
Edit 2: I tried running black on the command line, which I assumed to look something like this:
And while it looks like it succeeds, nothing seems to happen and the file remains unchanged. However, when I do this:
I see significant reformatting taking place in the file. |
@tumbl3w33d You cannot use When you run this command:
You are literally passing "roles/myrole/action_plugins/myplugin.py" to You should be using
Note that depending on file sizes and content some times black itself can take a long time to parse. If black seems to show for you , you can try |
Ah, that makes sense. I didn't know what I don't think black is too slow for me, because the command line call is almost instant and does its job. It's just the extension doing something different that makes it run in circles and being called a hundred times without any effect and I can't tell yet why is that. 🤔 |
@tumbl3w33d When you run with
be sure to include the |
Oh right, that's the case, indeed. I just misinterpreted the output. So we know there's no issue with calling black on command line either way. Is there any other info I could provide to let you know why the extension calls this multiple times without effect (although it says it worked)? |
@tumbl3w33d For that I will need the entire log. The one you shared only contains the part where we call |
I don't know what changed, but this looping effect is gone and so is the waiting time. I guess I'm fine then and I will get back in case this returns. In that case I will try to bring the log then. Thanks for your responses! |
I think it is a vscode bug.I open the file in a new vscode window,black formatter works,and output log normally.But when i back to old window,format the same file,there is no any output log about Black Formatter.Then,I use command to format file,it is ok. |
I get this often if I'm editing a python file that isn't in any project. ie shell scripts written in python
There is no response because it never finishes. If I run this manually: bin❯ /opt/homebrew/bin/python3 -m black --stdin-filename /Users/crucialfelix/org/bin/move-inbox.py -
/opt/homebrew/opt/python@3.11/bin/python3.11: No module named black And then I realize that it's using python 3.11 but the black formatter is defaulting to use
My import strategy is the default "useBundled"
black is shipped with the extension, but it's not using that for some reason.
By switching VSCode to use the correct python (the default one used by by script) it works fine, because there is a black installed there. |
@crucialfelix When you are running from terminal, the terminal does not include the bundled packages in its environment. This is intentional, to avoid interference from the packages that are only used for formatting from affecting anything that is production code. If you want to run the bundled formatter all you need to do is Third thing, if you want to manually run black using the mode that we use with the extension then you will need to do this:
In this mode I am not able to repro this locally, unfortunately, there can be several reasons why it appears to hang. Some of which may not be related to this extension at all. It really depends on if there were any other extensions doing something computation heavy. Can you look at Help > Toggle Developer Tools to see if any errors were reported. |
I'm also having this issue. I think it may be a vscode problem. OS: Arch Linux Relevant Config: "[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.black-formatter",
},
"[javascript]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.tabSize": 2,
"editor.insertSpaces": true,
}, This error began for me on VSCode. I tried using VSCodium to see if the issue was the same, and it reoccurred after some time. The issue seems to only occur after the editor has been opened for some time. This issue occurred for me on both 2023.6.0 and 2023.4.1 of vscode-black-formatter. I haven't specifically tested other versions, but I did not have this issue for the years before it started. On save, the This is preceded by messages about the extension host becoming responsive/unresponsive. Manually triggering the "Format Document" action does not work either. When this is done, the loading animation above the code window appears forever and the document is not formatted. No error is logged in this case. I thought this was specific to vscode-black-formatter, but the issue is now happening for me with Javascript as well with the Prettier formatter with format-on-save and manual formatting. After running the "Developer: Reload Window" action, format on save and manual formatting work again. I suspect that after some time, both will stop working again. I'm not familiar with how vscode works, but it seems like the extension host becomes unresponsive, which causes formatter extensions to fail to run. Maybe they are "triggered" to run, but it never gets to the extension, which could explain the hanging popup. There are no errors in the output logs of either formatter, so that could back that theory up. Please let me know if I can provide any more information |
This isn't exactly the same set of observations, but with save on format enabled, changes in the editor never get saved when doing command-S and then switching editor tabs before the black formatting completes. This is not an edge case for me, since I only ever save just before switching tabs as a rule, and do this many times every minute all day. I suspect others do the same. Steps to reproduce:
This also sometimes occurs without switching editor tabs. I see the VSCode - |
Every time I open vscode and run it for a while, and then save the python file, a pop-up window will pop up and will not disappear, causing the python file to fail to format on save. This problem can be solved by restarting vscode.
The text was updated successfully, but these errors were encountered: