vscode-java icon indicating copy to clipboard operation
vscode-java copied to clipboard

Auto import is not working in a java file when multiple root/subfolder project in one workspace

Open qzi opened this issue 3 years ago • 12 comments

[provide a description of the issue]

Environment
  • Operating System: MacOS
  • JDK version: 17
  • Visual Studio Code version: Version: 1.70.2
  • Java extension version: v1.9.0
Steps To Reproduce

Step 1: delete a necessary import in a java under one project under multiple root workspace Step 2: the extension can not import automatically

[Please attach a sample project reproducing the error] Please attach logs

Current Result

lack of dependency import and build failed

Expected Result

auto import the dependency necessarily

Additional Informations

qzi avatar Aug 25 '22 18:08 qzi

Can you share more details about auto import the dependency necessarily, adding the required import statement automatically?

jdneo avatar Aug 26 '22 06:08 jdneo

expressive_scenarios.zip attached the workspace already and also you can navigate to /Users/leon/Technical Agile/TacLearning/sample-code-cucumber4java/expressive_scenarios/02/src/test/java/tic_tac_toe/BoardSteps.java and comment any function like Pending Exception and then save to see whether it will auto import

// comment the line to import PendingException and then save to see whether it will auto import // import io.cucumber.java.PendingException; // or comment all PendingException and then uncomment later // throw new PendingException();

Can you share more details about auto import the dependency necessarily, adding the required import statement automatically?

qzi avatar Aug 26 '22 13:08 qzi

@qzi I can't reproduce the issue. Could you show your settings.json?

snjeza avatar Aug 26 '22 19:08 snjeza

@qzi I can't reproduce the issue. Could you show your settings.json?

"redhat.telemetry.enabled": true, only this setting relate to redhat only in my user setting.

I think you can call a new function in a new dependency imported by maven, then you can reproduce the issue without any auto import performed in a java file if the project is under multiple root workspace

qzi avatar Aug 26 '22 20:08 qzi

@qzi could you try to add

 "java.saveActions.organizeImports": true

snjeza avatar Aug 26 '22 22:08 snjeza

"editor.formatOnSave": true, "java.saveActions.organizeImports": true, "autoimport.showNotifications": true,

These ones are already added before I raised this issue and also have you tried auto imported in the head of the java file under a multiple root project in a workspace, you can try my compressed zip

qzi avatar Aug 27 '22 06:08 qzi

@qzi could you try the following project expressive_scenarios.zip I have renamed the artifactId property in all your projects.

snjeza avatar Aug 27 '22 17:08 snjeza

@qzi could you try the following project expressive_scenarios.zip I have renamed the artifactId property in all your projects.

Oh, I got your point, thanks for your support. I am more confident to use vs code to program java now.

By the way, even the artifactId is the same, but in IntelliJ idea, it still works well to import automatically, so can we adjust our condition strategy in this plugin to keep it more robust

qzi avatar Aug 27 '22 18:08 qzi

The same issue about auto import

Nagigate to [root]/18/src/test/java/RunCukesTest.java

// import io.cucumber.junit.CucumberOptions.SnippetType; can not auto import ...

18.zip

qzi avatar Sep 09 '22 09:09 qzi

@qzi automatic import (on file save) won't work if there are ambiguous imports. In your file, if you organize imports manually, you'll be presented with a choice to disambiguate imports: Screenshot 2022-09-09 at 12 16 46

fbricon avatar Sep 09 '22 10:09 fbricon

@rgrunber @testforstephen we could improve import on file save and show the disambiguation UI if the current editor matches the file being saved. Nothing we can do with multiple ambiguous imports on multiple files and "Save All".

fbricon avatar Sep 09 '22 10:09 fbricon

@qzi automatic import (on file save) won't work if there are ambiguous imports. In your file, if you organize imports manually, you'll be presented with a choice to disambiguate imports: Screenshot 2022-09-09 at 12 16 46

@fbricon I think the better approach is to show the ambiguous import in the menu of quick fix, maybe it is the bug issue of quick fix

image image

qzi avatar Sep 09 '22 11:09 qzi