question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto formatting of document+imports racing condition?

See original GitHub issue

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): image

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

And worst of all, it seems like VSCode is alternating between these two outcomes

  1. non-black style
  2. 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 (573d53815e74f8e4a52c2b6665f5ba9a92dbd341, 2018-08-01T05:17:54.216Z) OS version: Windows_NT x64 10.0.15063

System Info
Item Value
CPUs Intel® Core™ 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
pdf 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)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
brettcannoncommented, Aug 9, 2018

@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.

0reactions
gandalfsaxecommented, 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).

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?

I’ve also followed up on the VS Code issue to try and get more info on how they handle this situation for JavaScript.

Good idea. There could potentially be the same issues between any import organizer they use, and a system like prettier.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Auto formatting of document+imports racing condition?
Since it sometimes work, it seems as if there is a racing condition between the Format Document command and Organize Imports command when ......
Read more >
VSCode - Disable ALL Auto Formatting on Save
Doing the keyboard shortcut: CTRL + K , S. This lets you save files, bypassing any formatters that may run automatically. Format on...
Read more >
HOW TO SHUT OFF AUTOMATIC FORMATTING
1. Select file, then click “Options.” The following dialog box will open: 2. Click “Proofing” and then “AutoCorrect Options.” Page 2 Current as...
Read more >
Formatting code and log snippets in posts - Getting Started
Well formatted posts will help other users understand and engag… ... Race condition while updating single document multiple times.
Read more >
Importing into the United States A Guide for Commercial ...
consignee) will file entry documents for the goods with the port director at the ... the date of importation for vehicles and craft...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found