vscode: Organize imports and formatters are conflicting due to a race condition

https://github.com/microsoft/vscode-python/issues/6933

This is not an extension issue though.

  • VSCode Version: 1.39.1
  • OS Version: Windows 10

Steps to Reproduce:

  1. Have Black installed
  2. Set config options as such:
{
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
    "source.organizeImports": true
  }
}
  1. Create a python file with the following contents:
from something import (
    aaaaa,
    bbbbb,
    ccccc,
    ddddd,
    eeeee,
    fffff,
    ggggg,
    hhhhh,
    iiiii,
    jjjjj,
    kkkkk,
)
  1. Save the file multiple times and watch it dance

Does this issue occur when all extensions are disabled?: No

The problem is that there’s a race condition since there are two formatters being used when saving changes to a file. The results vary based on which formatter completes first. I.e. if black formats first, then you see one result, then isort formats the imports and changes the imports and you see another set of changes. As you keep hitting save both trip on one another.

The Python extension wants to organize formatters and isort to be ran consistently, but we don’t have control over that as VS Code is doing the excutions.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 15 (5 by maintainers)

Most upvoted comments

@pydolan’s suggestion did seem to make the behavior consistently correct for me. I recommend using the ${workspaceFolder} var like so instead of hard coding a path.

"python.sortImports.args": [
    "--settings-path=${workspaceFolder}/setup.cfg"
]

As an aside, I also had to add "editor.codeActionsOnSaveTimeout": 2000 to my config in order to extend the timeout beyond the default 750ms. Otherwise, my file was showing as unsaved after isort had sorted the imports. Seems like isort is taking more than 750ms to complete. See issue #72496 for a better description of the problem. This problem may only occur on larger projects.