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.

Sorting of imports (isort) is different if the file has unsaved modifications

See original GitHub issue

Environment data

  • VS Code version: 1.32.3 (have also seen this on older versions)
  • Extension version: 2019.2.5558 (have also seen this on older versions)
  • OS and version: Ubuntu 16.04.6
  • Python version: 3.5.2 (have also seen this on other versions)
  • Type of virtual environment used: venv
  • Relevant/affected Python packages and their versions: I currently have isort 4.3.15, though I’ve seen this on older versions

Expected behaviour

That when running the “Sort Imports” command on a file, the imports are sorted according to my setup.cfg config regardless of whether the file is currently saved or not.

Actual behaviour

When running the “Sort Imports” command on a file:

  • If the file is saved, my setup.cfg config is respected
  • If the file has modifications, my setup.cfg config is not respected

Steps to reproduce:

Given this config:

# setup.cfg
[isort]
length_sort = True
# demo.py
import os
import logging
import collections
  1. put setup.cfg and demo.py next to each other in a new folder
  2. open that folder as a workspace in VSCode
  3. open demo.py
  4. “Sort imports” within demo.py, it should be unchanged
  5. edit demo.py, without saving it
  6. “Sort imports” within demo.py, note that its imports have changed ordering to ignore the length_sort option

This reproduces with much more complex configs, and other options are also ignored, so I strongly suspect the setup.cfg file isn’t being picked up as a whole if the file is modified.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:10

github_iconTop GitHub Comments

3reactions
danisazacommented, Mar 15, 2020

I believe that I’m experiencing the same issue that Peter originally brought up.

In particular, here’s how the issue arises:

  • I open an existing file in my project
  • I make an edit to the file. For example, I can do the following:
    • I add a newline character between two imports
    • I remove the newline character that I just added
    • Note: At this point, the file contents are exactly the same as when I opened the project.
  • I save the file

Upon saving, my imports are re-ordered. The configurations in my settings.cfg file seems to be ignored.

If I were to commit the changes to git at this point, the diff would be a bit unwieldy, even thought the actual contents of the file were not changed before I saved.

Then, without making any changes to the file, I save it again. This completely reorders the imports to what they previously were. The configurations in my settings.cfg file are used.

So, in summary…

  • I make a trivial change to an existing file in the project
  • Saving completely reorders the imports without taking settings.cfg into account
  • Immediately saving again reorders the imports and takes settings.cfg into account.

My current workaround

To save, I have to press cmd + s twice in a row each time I save a file to ensure that my imports end up in the right order.

The effect on my work

It’s definitely annoying, but it’s not an earth-melting issue. I’m still able to get work done with VS Code. That said, because saving a file is such a frequent action, this annoyance comes up a lot.

2reactions
shughes-ukcommented, Apr 13, 2020

If anyone wants a workaround, setting the location of your settings file (pyproject.toml/setup.cfg) via an argument to isort works well.

 "python.sortImports.args": [
        "--settings-path",
        "somewhere/setup.cfg"
    ]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuration options for isort
Sort imports by their string length. Type: Bool Default: False Config default: false. Python & Config File Name: length_sort. CLI Flags:.
Read more >
VS Code group-aware sort imports on save for TypeScript 4.7
Any ideas what the difference is? Could the on-save organize not be using 4.7, or is this a different organization method? visual-studio-code ...
Read more >
Delete and restore files
Once you are in the Deleted files tab, you can view, sort, and filter any deleted files that you had access to. Here...
Read more >
Use isort to sort your Python module imports automatically
Besides, the --check option of isort will only check the formatting of the modules and won't change them in place. The isort command...
Read more >
Fix List for IBM Rational DOORS Requirements ...
If you have modified this file, please merge the modifications. ... In the "Manage document" window, enable to sort the document list and...
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