microsoft / vscode-java-pack

VS Code extensions for Java developers.
Other
291 stars 133 forks source link

After opening a Java file, another file from a different project with same name becomes current file #410

Open WillJDunn opened 4 years ago

WillJDunn commented 4 years ago

After creating two new projects using "Java: Create New Project", opening the default "App.java" of the second project and running it from the CodeLens options results in the "App.java" from the first project being run. The first project shows as the "current file" in the explorer (it is in yellow text) even though the second project was the one opened from Explorer. I have attached a GIF demonstrating the issue, with reproduction steps below.

I am using Visual Studio Code 1.45.1 + WSL Ubuntu (20.04 LTS build 20200423) with the Remote - WSL and the Java Extension Packs installed.

Reproduced with the following steps: 1) CTRL+SHIFT+P and selected "Java: Create New Project" 2) Selected directory /home/username/Java 3) Chose project name "HelloWorld" (Directory /home/username/Java/HelloWorld was created with default subdirectories and files, including ./src/App.java) 4) CTRL+SHIFT+P and selected "Java: Create New Project" 5) Selected directory /home/username/Java 6) Chose project name "GoodbyeWorld" (Directory /home/username/Java/GoodbyeWorld was created with default subdirectories and files, including ./src/App.java) 6) Modified file /home/username/Java/GoodbyeWorld/src/App.java to print "Goodbye, World!" to the console (instead of "Hello, World!") public class App { public static void main(String[] args) throws Exception { System.out.println("Goodbye, World!"); } } 7) Exited and reopened Visual Studio Code 8) Navigated to the "GoodbyeWorld" project in the Explorer and opened "App.java" 9) Confirmed the code should print "Goodbye, World!" and clicked the "Run" CodeLens option Output: "Hello, World!" Explorer now shows the HelloWorld project is in yellow text, and navigating the subdirectories shows /home/username/Java/HelloWorld/src/App.java is highlighted

I am new to Visual Studio Code, I apologize if this is simply a configuration error on my part. I followed the https://code.visualstudio.com/docs/remote/wsl and https://code.visualstudio.com/docs/java/java-tutorial for my setup.

Video demonstration:

another-file-becomes-current-file

akaroml commented 4 years ago

@willjdunn Thank you so much for the detailed instructions.

@testforstephen could you take a look and help @WillJDunn with this?

testforstephen commented 4 years ago

As you see, the command "Java: Create New Project" just scaffold a folder for you, it doesn't contain any project tag there. And as a convention, only when you open this folder as a VS Code root, then the extension will treat it as a project. The reason is the Java extension will initialize one invisible project for each workspace root by default.

But if you put the generated result in a subfolder of your current root, the Java extension will just take it as a normal folder instead of project.

//cc: @jdneo, Looks like this is a limitation of creating invisible project.

In your sample, you put the results into subfolders of your current root, and Java extension won't recognize subfolder as an invisible project. That means App.java files under these generated folders will be managed at a same default namespace. And one namespace won't allow the same class name exist. That's why you see App.java always jump to the same file.

Current workaround is to open these two projects separately as workspace root.

akaroml commented 4 years ago

Thanks for the analysis @testforstephen. Now it looks more like a project experience thing. @jdneo can we put this in the big picture and it's yet another use case.