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.

Organize imports removes valid imports in Java files with compile errors

See original GitHub issue

Organize 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
  1. Introduce an import
  2. Create a compile error that will invalidate the reference

java_compile_error_organize_import

Current Result

All “unused” imports are removed.

Expected Result

Don’t remove imports when the Java file has compile errors.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
RobMaskellcommented, Nov 8, 2022

Yup same issue, organise imports removes static imports from files and won’t add them back again

0reactions
rgrunbercommented, Jul 22, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

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