Bug 357795 - [organize imports] Organize imports discards unresolvable imports
Summary: [organize imports] Organize imports discards unresolvable imports
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.8   Edit
Hardware: All All
: P3 normal with 3 votes (vote)
Target Milestone: 4.6 M4   Edit
Assignee: John Glassmyer CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 376353 395538 (view as bug list)
Depends on: 483887
Blocks: 483948
  Show dependency tree
 
Reported: 2011-09-15 09:15 EDT by Erhard Weinell CLA
Modified: 2016-08-18 08:12 EDT (History)
14 users (show)

See Also:
markus.kell.r: review+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Erhard Weinell CLA 2011-09-15 09:15:06 EDT
Build Identifier: I20110613-1736 (3.7.0)

If the Organize Imports functionality cannot resolve an import statement, it simply discards it. I consider this as plain wrong - preferably Eclipse should leave code it cannot process untouched.

This one is especially annoying when "organize imports" is / has to be used as 
safe action due to project requirements, as such inconsistent states might very well be worth saving from time to time.


Reproducible: Always

Steps to Reproduce:
1. Create a class with some unresolvable, non-qualified type reference and an import for this type
2. Call Organize-Imports
Comment 1 Dani Megert CLA 2011-09-16 02:07:36 EDT
Organize Imports cleans up your import list: it adds new ones and removes outdated ones. This is as designed and won't be changed.
Comment 2 Pavel CLA 2011-11-09 04:24:13 EST
(In reply to comment #1)
> Organize Imports cleans up your import list: it adds new ones and removes
> outdated ones. This is as designed and won't be changed.

I know that this is already resolved, and I agree that this is working as designed, but I think it would be really useful to have an option to disable organizing imports if there are some compilation errors in the file. This would be handy especially in situations where you use some tools like Maven or Ivy to resolve dependencies, and this organize imports feature removes imports that are necessary if e.g. Ivy fails to download some library temporarily.
Comment 3 Dani Megert CLA 2011-11-09 04:35:38 EST
(In reply to comment #2)
> (In reply to comment #1)
> > Organize Imports cleans up your import list: it adds new ones and removes
> > outdated ones. This is as designed and won't be changed.
> 
> I know that this is already resolved, and I agree that this is working as
> designed, but I think it would be really useful to have an option to disable
> organizing imports if there are some compilation errors in the file. This would
> be handy especially in situations where you use some tools like Maven or Ivy to
> resolve dependencies, and this organize imports feature removes imports that
> are necessary if e.g. Ivy fails to download some library temporarily.

Why do you call 'Organize Imports' then? And why can't you call 'Organize Imports' again later, when the build path is correct?
Comment 4 Pavel CLA 2011-11-09 11:33:47 EST
(In reply to comment #3)
> Why do you call 'Organize Imports' then? And why can't you call 'Organize
> Imports' again later, when the build path is correct?

Because I've got it enabled as a "save action", which is really useful, except in this case. But I should correct my suggestion - I think it would be useful only not to remove imports if there are some compilation issues, because I realized that organize imports needs to be triggered to add missing imports, even if there are compilation errors. My suggestion is basically to be on the safe side and remove stuff only if we can be 100% sure that it is really not needed.

Thanks, Pavel
Comment 5 Dani Megert CLA 2011-11-14 07:01:33 EST
> My suggestion is basically to be on the
> safe side and remove stuff only if we can be 100% sure that it is really not
> needed.

Makes sense. We should add a preference on the 'Organize Imports' page which by default doesn't remove unused imports if there's an unresolved type error in the CU.
Comment 6 Dani Megert CLA 2011-11-14 07:02:17 EST
Fix goes into org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.
Comment 7 Pavel CLA 2011-11-14 17:31:22 EST
(In reply to comment #5)
> Makes sense. We should add a preference on the 'Organize Imports' page which by
> default doesn't remove unused imports if there's an unresolved type error in
> the CU.
Thanks Dani. This is exactly the option we are missing in our team.

Pavel
Comment 8 Deepak Azad CLA 2012-04-10 00:40:14 EDT
*** Bug 376353 has been marked as a duplicate of this bug. ***
Comment 9 Dani Megert CLA 2012-12-03 02:56:21 EST
*** Bug 395538 has been marked as a duplicate of this bug. ***
Comment 10 Eclipse Genie CLA 2015-02-25 13:30:33 EST
New Gerrit change created: https://git.eclipse.org/r/42682
Comment 11 John Glassmyer CLA 2015-02-25 13:36:10 EST
I have uploaded a change which addresses this bug by preserving unresolvable imports matching unresolved names in the compilation unit. For each unresolved name in the CU, it first attempts to preserve an unresolvable single import of the same name and, failing that, preserves all on-demand imports. It does this separately for static and for non-static unresolvable names and imports.
Comment 12 Markus Keller CLA 2015-03-17 11:31:50 EDT
(In reply to John Glassmyer from comment #11)
Thanks, that sounds good to me and avoids another preference.
Comment 13 Sergey Prigogin CLA 2015-04-15 22:21:57 EDT
Jay, could you please review the Gerrit patch. Thank you.
Comment 14 Manoj N Palat CLA 2015-04-20 01:29:19 EDT
Jay: moving the review back to Markus as the changes are all in jdt.ui (there is no jdt.core change in the patch).
Comment 15 Sergey Prigogin CLA 2015-10-26 19:47:33 EDT
The Gerrit patch is waiting for review since February. Any chance to get it reviewed soon?
Comment 16 Markus Keller CLA 2015-10-26 20:00:55 EDT
(In reply to Sergey Prigogin from comment #15)
> The Gerrit patch is waiting for review since February. Any chance to get it
> reviewed soon?

Yes. I'm really sorry about the repeated moves. In M4, I'll finally be around for the whole milestone again, and I've reserved time for this and other reviews waiting in my queue.
Comment 18 Markus Keller CLA 2015-12-07 14:30:09 EST
Thanks for the clean patch.

Released with a small fix for this case (doubly-broken static import):

package xy;
import static Broken;
public class BreakIt {
	int i = Broken;
}
Comment 19 Sergey Prigogin CLA 2015-12-07 14:32:35 EST
Thank you, Markus.
Comment 20 Dani Megert CLA 2015-12-08 08:48:27 EST
Verified in eclipse-SDK-I20151207-2000-win32-x86_64.
Comment 21 Kris De Volder CLA 2016-06-03 20:02:56 EDT
Hmm... the fix for this bug... causes another bug:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=495424

Reading through the comments here it sounds like this fix comes with a preference on the "Organize imports" page to enable or disable it. But looking at that page I don't see it. Am I looking in the wrong place maybe?
Comment 22 Noopur Gupta CLA 2016-08-18 08:12:54 EDT
(In reply to Kris De Volder from comment #21)
> Reading through the comments here it sounds like this fix comes with a
> preference on the "Organize imports" page to enable or disable it. But
> looking at that page I don't see it. Am I looking in the wrong place maybe?

No preference was added with this. See comment #12.