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

Organize imports conflicts with Black formatting #6933

Closed
chancecarey opened this issue Aug 12, 2019 · 7 comments
Closed

Organize imports conflicts with Black formatting #6933

chancecarey opened this issue Aug 12, 2019 · 7 comments
Labels
area-formatting bug Issue identified by VS Code Team member as probable bug

Comments

@chancecarey
Copy link

Environment data

  • VS Code version: 1.37.0
  • Extension version (available under the Extensions sidebar): 2019.8.29288
  • OS and version: Windows 10 Enterprise, 10.0.17763 Build 17763
  • Python version: 3.7.4
  • Type of virtual environment used: Poetry/venv
  • Relevant/affected Python packages and their versions: black: 19.3-beta.0
  • Jedi or Language Server: Language server

Expected behaviour

Saving a file should not toggle between black-formatted and isort-formatted.

Actual behaviour

Saving a file once results in black-formatted, then saving it again results in isort-formatted, saving that results in black-formatted, and so on.

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

@chancecarey chancecarey added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels Aug 12, 2019
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Aug 12, 2019
@DonJayamanne
Copy link

I think I know whats happening, not easy to replicate.
We have two formatters, one formats the code first, then another comes in and formats the code.

At this stage I'd say we'd need a decision on what needs to be done.
Basically its a conflict of using two formatters (sort imports is a type of a formatter).

@DonJayamanne
Copy link

@cmcarey
Thanks for reporting this issue.
Yes, theres a conflict. The reason is simple we are using two formatters 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.

Ideally the solution is to use just one and not two. At the end of the day black will re-format the entire file.

I've left this issue open so we can decide whether such a configuration is supported (using sort imports and a formatter together).

@chancecarey
Copy link
Author

@DonJayamanne

Thanks for reporting this issue.
Yes, theres a conflict. The reason is simple we are using two formatters 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.

Ideally the solution is to use just one and not two. At the end of the day black will re-format the entire file.

I've left this issue open so we can decide whether such a configuration is supported (using sort imports and a formatter together).

I see, thank you. I use isort in combination with black as it offers features that black alone does not.

black, by design, does not touch the order of the imports as this can change code functionality. I don't care about that, hence using isort.

As to being easy to replicate, if you copy the short example I used, it should demonstrate the issue (I've verified myself on a few different machines using vscode-python + latest black).

@max-sixty
Copy link

Are your isort setting consistent with black? There are a specific set of black settings that don't conflict

@max-sixty
Copy link

I had this issue but updated the isort args in vscode settings to match those consistent with black, and that stopped the flickering (but not ideal - ideally it would use those in setup.cfg)

@ovidner
Copy link

ovidner commented Oct 17, 2019

A copy-and-paste-able version of @max-sixty's workaround:

"python.sortImports.args": [
    "--multi-line=3",
    "--trailing-comma",
    "--force-grid-wrap=0",
    "--use-parentheses",
    "--line-width=88",
]

(add this to the settings.json of your own choice)

EDIT: Changed to the exact arguments suggested in Black's docs

@luabud
Copy link
Member

luabud commented Oct 29, 2019

Closing in favour of microsoft/vscode#83586.

@luabud luabud closed this as completed Oct 29, 2019
@ghost ghost removed the needs decision label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Nov 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-formatting bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

6 participants