Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone Java files are not linked to default project unless being opened. Need to scaffold a project without Maven/Gradle #620

Closed
Eskibear opened this issue Aug 28, 2018 · 7 comments

Comments

@Eskibear
Copy link
Contributor

As far as I know, currently whenever a standalone Java file is opened in VS Code, it is linked into "src" folder under the default project "jdt.ls-java-project". So it allows users to start from scratch without importing a project.

But the feature doesn't work for some scenarios. If I open a folder with some existing Java files, the files will not be linked to the default project unless I open each of one by one. Mentioned by @cjbailey in microsoft/vscode-java-debug#344 (comment)

A simplified example:

There is a "Hello.java" file existing in my file system, and its' content is:

/**
 * Hello
 */
public class Hello {
    public static void sayHello() {
        System.out.println("hello");
    }
}

I wanted to write simple Java code from scratch to test this Hello.java, so I created a "new folder" as my project root and a "src" folder to put java files, and copy "Hello.java" to the folder. Then I open this "new folder" with VSCode. Without opening "Hello.java" (this is important), I created a "Main.java", referencing a method in Hello and press F5 to run, error occurred:
image

Some thoughts

  • If it supports to create a project from current workspace root folder, it would be a better experience for users to start from scratch. For a well-structured folder, I think it also makes more sense to create a project for it instead of link all the Java files into an invisible default project. In Eclipse, the "Import Projects from File System" just works fine for the case, I'm not sure if it is possible to have this feature here.

  • Moreover, a potential feature (Need new feature: use as source folder #619) enables user to set the source folder, in case language server fails to recognize source folder correctly.

@Eskibear
Copy link
Contributor Author

Eskibear commented Sep 2, 2018

Any thoughts? Currently there is no way to scaffold/import a simple non-Maven/non-Gradle project, all the standalone Java files (even if they are not related) are directly linked to a default project when being open.

When opening a well-structured simple project, following things are strange:

  • Java classes are not recognized unless I explicitly open the related files in the editor one by one.
  • The classes in the project are messed up with other standalone Java files in other projects (which I temporarily open in VSCode to see the content).

@Eskibear Eskibear changed the title Standalone Java files are not linked to default project unless being opened. Standalone Java files are not linked to default project unless being opened. Need to scaffold a project without Maven/Gradle Sep 5, 2018
@testforstephen
Copy link
Collaborator

One idea is when the user opens a standalone java file, prompt the user whether to add the parent directory as Java Source Folder, this will make all standalone java files under the same directory to be added to the default project classpath.
image

When the user wants to cancel the parent directory as Java Source Folder, he/she can do that through the context menu Unmark as Java Source Folder.
image

In this approach, the user doesn't need create a new eclipse project and get bothered by the generated .classpath and .project files, but directly leverage the default project to manage the standalone java files.

@michael-hawker
Copy link

@testforstephen why would it be the parent folder instead of the current folder? Most basic Java editors (e.g. jGrasp) will just take the current folder for the open file as the classpath.

@testforstephen
Copy link
Collaborator

@michael-hawker The parent folder represents the direct folder containing the open file. Is this same as the current folder you mentioned? right?

@michael-hawker
Copy link

@testforstephen yup, think 'containing folder' is the clearer term to avoid my former confusion. :)

@andxu
Copy link
Collaborator

andxu commented Oct 31, 2018

@testforstephen @fbricon I suggest to use a folder based standalone file strategy, connect different java files under different folder to default project will make the default project difficult to compile (due to naming conflicts) and bring in the performance issue(in case of a lot of java files). I suggest to look into the parent folder when opening standalone java file, decided by the folder structure to import the projects under that folder or create a special project related to that folder takes the folder as the project root and source root if no projects are found.

Whenever a standalone Java file is opened in vscode, we try to follow some rules to find the right folder for that file:

  1. If the file lays under any one of the opened workspace root, open it as common text file if it doesn't belong to any source folder of the projects, or open it as common java file if it belongs to any source folder.
  2. If the file has package directive inside, search for the actual project it belongs to according to package declaration.
  3. f the file does not have any package directive, its parent folder is the folder we want to work with. Try to open that folder and import the existing projects or create a new project( with the hash of that directory as the project name), and add each of the project root to vscode workspace.

The logic to detect the java project system in a folder:

  1. if the folder does not have any projects
    a. Check whether there is an invisible project related to that folder (using the hash of the folder as the project name) already created, use the existing one if there is one
    b. Create an invisible project related to that folder if no existing project was created before
  2. if the folder already have a project inside
    a. Help update project setting to make the standalone file compile
    b. If 2.a is not possible, tell the user why
    The invisible project is associate with a folder by using a hash value from the folder’s absolute path.

@fbricon
Copy link
Collaborator

fbricon commented Dec 17, 2018

Standalone file support has been reworked. Opening a folder should now reference all java files in it, via the "invisible project" concept.

@fbricon fbricon closed this as completed Dec 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants