eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
164 stars 130 forks source link

[23] Setting project to latest SE disabled the preview option #2945

Closed jarthana closed 1 month ago

jarthana commented 1 month ago

This was originally reported on #2939. The issue was that the default module import of java.base was not happening. It turned out to be caused by the preview flag being disabled when the project build path is set with a JavaSE-23 rather than a specific JDK 23. I nailed this down to the preview flag being reset to false when request is coming from the build path config page. The code in question is in org.eclipse.jdt.core.JavaCore.setComplianceOptions(String, Map), line number 6436 and was introduce by commit (664939cf47c16a86089a9b3db66881fc786ac539).

@noopur2507 I am not sure it's a good idea to reset this to false. May be OK when the compliance is not latest, but not otherwise. I will alter this code. Let me know if you still think we should reset this.

jarthana commented 1 month ago

There could be more to this. I see that the UI may have a hand in this as well (via Bug 542579). Assigning to @noopur2507

noopur2507 commented 1 month ago

Can you provide step-by-step instructions on how to reproduce it on UI?

jarthana commented 1 month ago

Steps:

  1. Set the workspace default JRE to a recent JDK 23.
  2. Create a Java project with JavaSE-23 on its buildpath.
  3. Edit the project build path, go to Libraries tab, Edit the JRE System Library and select Workspace default JRE
  4. Open the org.eclipse.jdt.core.prefs file of this project and set enablePreviewFeatures=enabled
  5. Repeat Step 3, but this time select Execution Environment and JavaSE-23
  6. Apply and close to find that enablePreviewFeatureshas been reset to disabled.
noopur2507 commented 1 month ago

Between steps 5 and 6, if we go to the project's Java Compiler page after just clicking Apply on the Java Build Path page, then clicking Apply and Close preserves the preview state i.e. it stays enabled.

noopur2507 commented 1 month ago

I see that when selecting an EE, the settings from EE are being applied.

See BuildPathSupport.getEEOptions(IExecutionEnvironment ee) where the IExecutionEnvironment.getComplianceOptions() is returning disabled for preview features in JavaSE-23 EE and the same is being set at line 422.

Is it correct to get preview features as disabled via JavaSE-23 EE profile?

stephan-herrmann commented 1 month ago

I see two code locations basically implementing the same logic: when setting compliance > 10 initially disable preview features:

To change the behavior resulting from BuildPathSupport.getEEOptions() both locations would need to be changed.

Generally it's good to disable previews when switching the EE as you can't carry over preview from one compliance to another. Only when switching from a JRE 23 to JavaSE-23 the flag could possibly be preserved, but that may not be the most frequent operation. I wouldn't give much priority to keeping the preview flag in this one specific scenario.

noopur2507 commented 1 month ago

Thanks, Stephan.

I am closing this issue based on the above comment.