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

"The source path must be contained in the project root folder"

Open cyberphone opened this issue 1 year ago • 4 comments

I'm new to VS Code and now trying to convert an Eclipse project to VS Code. I have multiple Java projects stored in a common folder. Each project have its own .vscode/settings.json file. The projects refer to each other on source level as which requires cross-project references. The projects are unmanaged using VS Code terminology.

Environment
  • Operating System: Mac Sonoma 14.4.1
  • JDK version: 21
  • Visual Studio Code version: 1.88.1
  • Java extension version: 1.29
Steps To Reproduce
  1. Try to add a source path from another project in the folder. Then you get the error message from the issue.

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

Current Result
Expected Result
Additional Informations

If this will be addressed, I think "sourcePaths" should permit relative and absolute paths to any folder.

cyberphone avatar Apr 18 '24 08:04 cyberphone

It turned out that a working solution is to use Eclipse .project and .classpath files as suggested in: https://github.com/redhat-developer/vscode-java/issues/3582

Sample project: multi-xref.zip

For somebody with no previous experience with Eclipse, this seems a bit complex.

Is this documented somewhere?

cyberphone avatar Apr 19 '24 19:04 cyberphone

.classpath file of project p2 which refers to source in project common:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="src" path="common"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

.project file of project p2:

<projectDescription>
    <name>p2</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.jdt.ls.unmanagedFolderNature</nature>
    </natures>
    <filteredResources>
        <filter>
            <id>1713454214439</id>
            <name></name>
            <type>30</type>
            <matcher>
                <id>org.eclipse.core.resources.regexFilterMatcher</id>
                <arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
            </matcher>
        </filter>
    </filteredResources>
    <linkedResources>
        <link>
            <name>common</name>
            <type>2</type>
            <locationURI>copy_PARENT/common/src</locationURI>
        </link>
    </linkedResources>
    <variableList>
        <variable>
            <name>copy_PARENT</name>
            <value>${PROJECT_LOC}/..</value>
        </variable>
    </variableList>
</projectDescription>

cyberphone avatar Apr 20 '24 04:04 cyberphone

Hi. I'm also new to VS Code and encountered this problem. Maybe you should add your folder to workspace. The details are in Add Folder to Workspace.

After that, try to reload your projects using the command >java: Reload Projects.

KyleCoCo avatar May 05 '24 12:05 KyleCoCo

Hi @KyleCoCo Thanx! I will though keep the published solution (for now...) because I need the ${PROJECT} macro capability as well

cyberphone avatar May 05 '24 15:05 cyberphone

I get this error even when trying to select a folder inside my project. When I view the Project Settings it tells me

No source paths are configured.

Then when I press the 'Add Source Root...' button and browse to the src/main/java folder inside my project, once I select it, it gives me this error message:

The source path must be contained in the project root folder.

But it is!

At least print the 'project root folder' you insist does not contain the path I just selected. That way I have a chance of figuring out what the wrong folder is that it is obviously using to check this constraint... And then maybe I can use Search to find the config file or whatever it is getting this value from and maybe fix it. But the error gives no such details so all I can do is come here and hope someone else has figured this out :(

Image

UPDATE

I closed all folders in the workspace. Then I closed VSCode. Then I created a new, blank workspace and used Add folder to workspace to add just the folder with the project that has Java code in it. That solved it. My conclusion is that somewhere in the workspace settings it stores a path that is not actually the 'project root folder', possibly a path to another folder in the same workspace. But even after removing all other folders from the workspace that did not resolve it. The only thing that helped was creating a new workspace and adding the folder to that.

It would be really helpful if this error message would tell us what it thinks the 'project root folder' actually is. And maybe even tell us which setting it has read that from so that we can check that setting and possibly fix it.

Personally I think there is a bug in there somewhere. But that may be very hard to find. But just updating the error message to give some more details should be a lot simpler and then at least we will be getting more details whenever this happens to someone else in the future.

Download avatar Aug 12 '25 14:08 Download