redhat-developer / redhat-developer/vscode-java

Organize imports removes valid imports in Java files with compile errors

Open
#2,294 8 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug code action
Dominant language
TypeScript
Stars
2.3k
Forks
546
Avg merge
20h 1m
Merged PRs (30d)
11

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with a Java file containing an import and a compile error, then run the Organize Imports code action or source.organizeImports on save. Trace the code-action handling to determine how compile errors affect unused-import detection; done means valid imports are retained whenever the Java file has compile errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, vscode
Domain
developer-experience, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.