Repository navigation
Auto formatting of document+imports racing condition? #2301
Description
Issue Type: Bug
I use the following settings:
"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
},
"python.formatting.provider": "black"
However the imports formatting is very inconsistent. Sometimes it follows black, and most times it doesn't. Any multi-line imports where each imported variable/function should be on it's own line (according to black) often isn't.
If I run Format Document, it formats the imports according to black (one per line):

If I run Organize Imports, it puts everything on as few lines as possible (violates black):

And worst of all, it seems like VSCode is alternating between these two outcomes
- non-black style
- non-black style for half a second, then black style.
Since it sometimes work, it seems as if there is a racing condition between the Format Document command and Organize Imports command when both are auto enabled on save in settings. The right thing to do would seem to first run the ordinary import formatting, then run any custom formatting provider afterwards.
Since this looks like a bug / conflict between the "Format Document" and "Organize Imports" commands., which are both build-in, I think this is a VSCode problem, not vscode-python extension problem.
VS Code version: Code - Insiders 1.26.0-insider (573d538, 2018-08-01T05:17:54.216Z)
OS version: Windows_NT x64 10.0.15063
System Info
| Item | Value |
|---|---|
| CPUs | Intel(R) Core(TM) i7-6700T CPU @ 2.80GHz (8 x 2808) |
| GPU Status | 2d_canvas: enabled checker_imaging: disabled_off flash_3d: enabled flash_stage3d: enabled flash_stage3d_baseline: enabled gpu_compositing: enabled multiple_raster_threads: enabled_on native_gpu_memory_buffers: disabled_software rasterization: enabled video_decode: enabled video_encode: enabled webgl: enabled webgl2: enabled |
| Memory (System) | 15.88GB (7.09GB free) |
| Process Argv | C:\Users\s113093\Dropbox\portable-win\VSCode-win32-x64-1.26.0-insider\Code - Insiders.exe |
| Screen Reader | no |
| VM | 0% |
Extensions (33)
| Extension | Author (truncated) | Version |
|---|---|---|
| Bookmarks | ale | 9.0.3 |
| project-manager | ale | 8.0.0 |
| spellright | ban | 2.5.24 |
| path-intellisense | chr | 1.4.2 |
| vscode-svgviewer | css | 1.4.4 |
| vscode-markdownlint | Dav | 0.19.0 |
| githistory | don | 0.4.2 |
| jupyter | don | 1.1.4 |
| vscode-pandoc | Dou | 0.0.8 |
| gitlens | eam | 8.5.4 |
| vsc-material-theme | Equ | 2.3.0 |
| prettier-vscode | esb | 1.5.0 |
| vscode-diff | fab | 1.3.2 |
| latex-workshop | Jam | 5.7.0 |
| python | ms- | 2018.7.1 |
| PowerShell | ms- | 1.8.2 |
| wordcount | ms- | 0.1.0 |
| vsliveshare | ms- | 0.3.504 |
| gremlins | nho | 0.7.0 |
| vscode-docker | Pet | 0.1.0 |
| quicktype | qui | 12.0.15 |
| vscode-icons | rob | 7.24.0 |
| partial-diff | ryu | 1.3.0 |
| markdown-preview-enhanced | shd | 0.3.5 |
| vscode-spotify | shy | 2.5.1 |
| vscode-fileutils | sle | 2.10.3 |
| slack | soz | 0.0.14 |
| code-spell-checker | str | 1.6.10 |
| tom | 0.3.0 | |
| vscode-terminal-here | Tyr | 0.1.1 |
| vscodeintellicode | Vis | 1.0.1 |
| vscode-todo-highlight | way | 1.0.4 |
| markdown-all-in-one | yzh | 1.6.0 |
(2 theme extensions excluded)
Activity
brettcannon commentedon Aug 1, 2018
This is a side-effect of
"editor.codeActionsOnSave"being done byisortandFormat Documentusing Black and neither of them agreeing on the actual format (by default; isort might have a way to tweak its output, in which case you can set the sorting options). But since we don't control the actual execution of any of this we can't manage this; basically we register callbacks on what to call when these things trigger and VS Code is calling those callbacks independently.gandalfsaxe commentedon Aug 4, 2018
Hi Brett Cannon (@brettcannon),
Since VSCode team think it's not for VSCode to fix, and you're saying it can't be solved here, where should I direct my request / bug report?
brettcannon commentedon Aug 9, 2018
I'm saying I don't think it can necessarily be fixed at all. Basically you either need isort to output in a format that black likes (so black no longer care, have black sort imports (so you don't need isort), or trigger the import sorting manually (so you remove the race condition).
I've also followed up on the VS Code issue to try and get more info on how they handle this situation for JavaScript.
gandalfsaxe commentedon Aug 9, 2018
So it's not possible to say "Sort imports" should finish running before any formatters are allowed to run? VSCode don't give that capability?
Good idea. There could potentially be the same issues between any import organizer they use, and a system like prettier.
brettcannon commentedon Aug 9, 2018
Gandalf Saxe (@gandalfsaxe) Correct, VS Code doesn't let us say "please run this before that". All we get is "sort imports" and "format document" calls and both are done via callbacks which we don't directly trigger.