You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Sorting of imports (isort) is different if the file has unsaved modifications #4891
area-intellisenseLSP-related functionality: auto-complete, docstrings, navigation, refactoring, etc.bugIssue identified by VS Code Team member as probable bug
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.pyimportosimportloggingimportcollections
put setup.cfg and demo.py next to each other in a new folder
open that folder as a workspace in VSCode
open demo.py
"Sort imports" within demo.py, it should be unchanged
edit demo.py, without saving it
"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.
Reacted by Ian Chamberlain, Swarnendu Biswas, cory1979 and Samantha Hughes
I'd be up for trying to fix this if that would help. Do you have any suggestions around how to approach this?
From what I can see the issue here is that we're asking isort to operate on a temporary file like /tmp/tmp-19006aGV59gsvAPHQ.py rather than on a file within the project workspace.
Would a suitable fix be to put the temporary file within the project workspace instead of within /tmp?
I don't believe that this is a duplicate of #2980 -- that issue seems to be about new, unsaved files (which presumably don't have a well-defined project nor path on disc). In contrast this issue is about unsaved modifications to existing files. I've updated the title to clarify that.
Interestingly it looks like isort now supports being passed the content of the target file on stdin. While this project has a comment about this not being broken, I'm guessing that that pre-dates it being fixed (PyCQA/isort#384).
In any case, having tested the behaviour using stdin seems to correctly find configuration from the current directory (or ancestors), so I think this is a better solution than moving the temporary file. This would likely also solve #2980 for isort.
My current plan for this is to extend the IProcessService such that it supports passing something usable as the stdin of the underlying process.
I tried doing this using the existing SpawnOptions.stdio and passing essentially [myReadableWithFileCotent, undefined, undefined], but that didn't seem to work. I got errors that the Readable I was passing wasn't suitable; I'm guessing because it didn't have an actual file descriptor. If I'm missing something about this error I think this approach would be the best though.
I'm currently working on passing through a (new) IBufferEncoder to the ProcessService everywhere, with the idea that we can either add an input?: string attribute to the SpawnOptions or to the actual exec function signature. Input on whether it would be preferable to have the input specified in the function signature directly or as part of the options would be welcome.
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.
Reacted by Swarnendu Biswas, Pavlin Angelov and Tomasz Jakubowski
area-intellisenseLSP-related functionality: auto-complete, docstrings, navigation, refactoring, etc.bugIssue identified by VS Code Team member as probable bug
Activity
PeterJCLaw commentedon Dec 11, 2019
I'd be up for trying to fix this if that would help. Do you have any suggestions around how to approach this?
From what I can see the issue here is that we're asking isort to operate on a temporary file like
/tmp/tmp-19006aGV59gsvAPHQ.pyrather than on a file within the project workspace.Would a suitable fix be to put the temporary file within the project workspace instead of within
/tmp?DonJayamanne commentedon Dec 11, 2019
Dup of #2980
[-]Sorting of imports (isort) is different if the file is unsaved[/-]PeterJCLaw commentedon Dec 14, 2019
I don't believe that this is a duplicate of #2980 -- that issue seems to be about new, unsaved files (which presumably don't have a well-defined project nor path on disc). In contrast this issue is about unsaved modifications to existing files. I've updated the title to clarify that.
PeterJCLaw commentedon Dec 14, 2019
Interestingly it looks like
isortnow supports being passed the content of the target file on stdin. While this project has a comment about this not being broken, I'm guessing that that pre-dates it being fixed (PyCQA/isort#384).In any case, having tested the behaviour using stdin seems to correctly find configuration from the current directory (or ancestors), so I think this is a better solution than moving the temporary file. This would likely also solve #2980 for
isort.PeterJCLaw commentedon Dec 15, 2019
My current plan for this is to extend the
IProcessServicesuch that it supports passing something usable as the stdin of the underlying process.I tried doing this using the existing
SpawnOptions.stdioand passing essentially[myReadableWithFileCotent, undefined, undefined], but that didn't seem to work. I got errors that theReadableI was passing wasn't suitable; I'm guessing because it didn't have an actual file descriptor. If I'm missing something about this error I think this approach would be the best though.I'm currently working on passing through a (new)
IBufferEncoderto theProcessServiceeverywhere, with the idea that we can either add aninput?: stringattribute to theSpawnOptionsor to the actualexecfunction signature. Input on whether it would be preferable to have the input specified in the function signature directly or as part of the options would be welcome.2 remaining items
danisaza commentedon 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:
Upon saving, my imports are re-ordered. The configurations in my
settings.cfgfile 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.cfgfile are used.So, in summary...
settings.cfginto accountsettings.cfginto account.My current workaround
To save, I have to press
cmd + stwice 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.
DonJayamanne commentedon Mar 27, 2020
Luciana Abud (@luabud) Kartik Raj (@karrtikr) Accidentally moved this, not sure where this issue belongs
shughes-uk commentedon 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.
simmol commentedon May 5, 2020
I have the same issue and the this workaround save my sanity :)
Thanks a bunch Samantha Hughes (@shughes-uk)