-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
organizeImports duplicates and removes code (destroys syntax) #9889
Comments
Hi @Spenhouet 👋 Thank you for reaching out. I will address the points you raised separately:
Thank you! |
Hi @kimadeline, thank you for your time. I did look further into this and want to reiterate and reformulate this issue. First off: My goal is to enforce a consistent formatting and sorted imports without the need of manual action and also without GIT changes with every save. This being said, let us reiterate my problems. Further using the import example from above:
I noticed that I have two context menu entries: Organize Imports:
Sort Imports:
Both commands also work both ways and are always consistent without constantly changing (if run multiple times). I noticed that if I press save, both commands seem to run at the same time. This is what results in the destroyed code:
Always reproduceable.
If I choose Organize Imports 1. I get the following context menu: This leads me to conclude that both 2. and 3. are activated by the following setting:
This is where the destructive behavior is coming from. Result 1:
Should I create a new issue for that? But even with Goal: On save: Code automatically formatted & imports sorted Since the tool to sort imports is only supposed to do exactly that (yes, it will have to reformat to achieve that), we can expect the following workflow / execution:
From here on with the following settings (
With every press on save it will no reformat the import, switching between these two: (1)
and
Again, as with the first issue, let's look at what happens if we only run one of the following actions:
Running only 1. will yield (2). If run independently, both run consistently and do not change on multiple successive runs. This lead me to conclude that 1. and 2. are run either parallel or in random order but not consistently successive. Result 2: Should I create a new issue for that? |
Hi @Spenhouet , Thank you for reformulating your issue. First off, if you encounter the line addition/deletion issue when manually organizing imports with Pyright, it's an issue on their side, so please report it on their repo. This being said, let us address the other points you raised.
The race condition between
We (the Python extension) only use the hook provided by VS Code to register our import sorting command. It would be up to VS Code to offer the possibility to select which extension to use to organize imports, similar to the |
That is not the issue. Pyright on its own does work fine (as does Python Refactor). I will create issues on the VS code repository. |
I created the following issues with respect to the above issues: |
Environment data
Expected behaviour
Already sorted imports should not change.
I would expect
organizeImports
to organize imports and not to format my code (less alone destroying it).Actual behaviour
Import is reformatted (and destroyed).
The following import:
will either be formatted to this (duplicated and destroyed code):
or that (unnecessary change / reformatting):
If it messes up the imports and you keep on saving your file, the rest of the code will be destroyed. I got random deletions and duplication of other lines in the code.
Steps to reproduce:
Logs
Output for
Python
:Note here: why is sort run twice? Maybe this issue is some form of race condition where sort is started twice and overwrites change (still: it should not change anything for the given code).
Output from
Console
:The text was updated successfully, but these errors were encountered: