Organize imports removes valid imports in Java files with compile errors
See original GitHub issueOrganize imports will remove imports when the references are not valid Java code anymore. This might remove “valid” imports that just happen to be unused at that moment, because there is a compile error before the reference. The references cannot be easily  restored once the compile errors are fixed, e.g. manually created static imports. This is problematic in long files with many imports and "editor.codeActionsOnSave" : ["source.organizeImports", ...]. A typo in the “wrong” place can easily remove all your imports.
Environment
- Operating System: Ubuntu 20.04.3 LTS
- JDK version: 11.0.6+10
- Visual Studio Code version: 1.63.2
- Java extension version: 1.2.0
Steps To Reproduce
- Introduce an import
- Create a compile error that will invalidate the reference

Current Result
All “unused” imports are removed.
Expected Result
Don’t remove imports when the Java file has compile errors.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
 Top Results From Across the Web
Top Results From Across the Web
importing a valid package is causing a java compiler error
I am getting a compiler error when compiling Order.java file even when it contains an import statement for the other packaged class.
Read more >Valid imports marked as errors, but correct import suggested ...
Hi, I've been working on a scala project on IntelliJ for a while. Recently, it started showing me import errors.
Read more >4289 – Organize imports removes required imports (1GKZ7CL)
If I call "Organize Imports" on the following file, it removes two required imports. The resulting file then has compile errors. Organize Imports...
Read more >Eclipse Shortcut to get rid of all unused Imports in a Java file
Here are a couple of ways to remove all unused imports from the Java Eclipse IDE : 1. Go to the line of...
Read more >Using the Eclipse IDE for Java programming - Tutorial
Exercise: Java project, packages and import statements ... Code (Code) from Microsoft uses the Eclipse Java compiler for its Java support.
Read more > Top Related Medium Post
Top Related Medium Post
No results found
 Top Related StackOverflow Question
Top Related StackOverflow Question
No results found
 Troubleshoot Live Code
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free Top Related Reddit Thread
Top Related Reddit Thread
No results found
 Top Related Hackernoon Post
Top Related Hackernoon Post
No results found
 Top Related Tweet
Top Related Tweet
No results found
 Top Related Dev.to Post
Top Related Dev.to Post
No results found
 Top Related Hashnode Post
Top Related Hashnode Post
No results found

Yup same issue, organise imports removes static imports from files and won’t add them back again
You’re right. I’m still able to reproduce. I haven’t investigated, but I would guess some kind of reference check is done against the static import, and if a syntax error happens in such a way as to make the AST unrecoverable, the reference is not discovered, and the import gets flagged for removal.
The modification of
{for;at the method declaration might be particular bad as it may destroy the entire block from being recovered.