Skip to content

Organize imports conflicts with Black formatting #6933

Closed

Description

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

Activity

  1. added
    triage-neededNeeds assignment to the proper sub-team
    bugIssue identified by VS Code Team member as probable bug
    on Aug 12, 2019
  2. ghost removed
    triage-neededNeeds assignment to the proper sub-team
    on Aug 12, 2019
  3. DonJayamanne commented on Aug 12, 2019

    @DonJayamanne

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

  4. DonJayamanne commented on Aug 12, 2019

    @DonJayamanne

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

  5. chancecarey commented on Aug 12, 2019

    @chancecarey
    Author

    Don Jayamanne (@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).

  6. max-sixty commented on Aug 13, 2019

    @max-sixty

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

  7. max-sixty commented on Aug 14, 2019

    @max-sixty

    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)

  8. ovidner commented on Oct 17, 2019

    @ovidner

    A copy-and-paste-able version of Maximilian Roos (@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

  9. luabud commented on Oct 29, 2019

    @luabud
    Member

    Closing in favour of microsoft/vscode#83586.

  10. locked as resolved and limited conversation to collaborators on Nov 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-formattingbugIssue identified by VS Code Team member as probable bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions