javagradleneovimeclipse-jdt

JDTLS marks file as non-project file, and throws errors in logs


When I attempt to run jdtls using neovim (nvim-jdtls) in a gradle project, the LSP throws an error message of not finding the jdk17 path specified and marks the file as a non-project file.

Checking the lsp logs shows this:

[START][2024-07-30 15:05:18] LSP logging initiated
[ERROR][2024-07-30 15:05:18] .../vim/lsp/rpc.lua:734    "rpc"   "java"  "stderr"    "WARNING: Using incubator modules: jdk.incubator.vector, jdk.incubator.foreign\r\n"
[WARN][2024-07-30 15:05:21] ...lsp/handlers.lua:537 "Jul 30, 2024, 3:05:21 PM Synchronize project app failed due to an error while importing the root project.\norg.eclipse.buildship.core.internal.GradlePluginsRuntimeException: org.eclipse.core.internal.resources.ResourceException: Invalid project description.\norg.eclipse.buildship.core.internal.ImportRootProjectException: org.eclipse.buildship.core.internal.GradlePluginsRuntimeException: org.eclipse.core.internal.resources.ResourceException: Invalid project description.\r\n\tat org.eclipse.buildship.core.internal.workspace.ImportRootProjectOperation.run(ImportRootProjectOperation.java:53)\r\n\tat org.eclipse.buildship.core.internal.DefaultGradleBuild$SynchronizeOperation.runInToolingApi(DefaultGradleBuild.java:225)\r\n\tat org.eclipse.buildship.core.internal.operation.DefaultToolingApiOperationManager$WorkspaceRunnableAdapter.run(DefaultToolingApiOperationManager.java:58)\r\n\tat org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2313)\r\n\tat org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2338)\r\n\tat org.eclipse.buildship.core.internal.operation.DefaultToolingApiOperationManager.run(DefaultToolingApiOperationManager.java:39)\r\n\tat org.eclipse.buildship.core.internal.DefaultGradleBuild$SynchronizeOperation.run(DefaultGradleBuild.java:192)\r\n\tat org.eclipse.buildship.core.internal.DefaultGradleBuild.synchronize(DefaultGradleBuild.java:100)\r\n\tat org.eclipse.buildship.core.internal.DefaultGradleBuild.synchronize(DefaultGradleBuild.java:86)\r\n\tat org.eclipse.jdt.ls.core.internal.managers.GradleProjectImporter.startSynchronization(GradleProjectImporter.java:352)\r\n\tat org.eclipse.jdt.ls.core.internal.managers.GradleProjectImporter.importDir(GradleProjectImporter.java:240)\r\n\tat org.eclipse.jdt.ls.core.internal.managers.GradleProjectImporter.importToWorkspace(GradleProjectImporter.java:191)\r\n\tat org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.importProjects(ProjectsManager.java:124)\r\n\tat org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.initializeProjects(ProjectsManager.java:107)\r\n\tat org.eclipse.jdt.ls.core.internal.handlers.InitHandler$1.runInWorkspace(InitHandler.java:244)\r\n\tat org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:42)\r\n\tat org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)\r\nCaused by: org.eclipse.buildship.core.internal.GradlePluginsRuntimeException: org.eclipse.core.internal.resources.ResourceException: Invalid project description.\r\n\tat org.eclipse.buildship.core.internal.workspace.DefaultWorkspaceOperations.createProject(DefaultWorkspaceOperations.java:128)\r\n\tat org.eclipse.buildship.core.internal.workspace.ImportRootProjectOperation.importRootProject(ImportRootProjectOperation.java:95)\r\n\tat org.eclipse.buildship.core.internal.workspace.ImportRootProjectOperation.access$200(ImportRootProjectOperation.java:39)\r\n\tat org.eclipse.buildship.core.internal.workspace.ImportRootProjectOperation$1.run(ImportRootProjectOperation.java:68)\r\n\tat org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2313)\r\n\tat org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2333)\r\n\tat org.eclipse.buildship.core.internal.workspace.ImportRootProjectOperation.runInWorkspace(ImportRootProjectOperation.java:58)\r\n\tat org.eclipse.buildship.core.internal.workspace.ImportRootProjectOperation.run(ImportRootProjectOperation.java:51)\r\n\t... 16 more\r\nCaused by: org.eclipse.core.internal.resources.ResourceException: Invalid project description.\r\n\tat org.eclipse.core.internal.resources.Project.checkDescription(Project.java:173)\r\n\tat org.eclipse.core.internal.resources.Project.assertCreateRequirements(Project.java:57)\r\n\tat org.eclipse.core.internal.resources.Project.create(Project.java:266)\r\n\tat org.eclipse.core.internal.resources.Project.create(Project.java:252)\r\n\tat org.eclipse.buildship.core.internal.workspace.DefaultWorkspaceOperations.createProject(DefaultWorkspaceOperations.java:113)\r\n\t... 23 more\r\n"
[WARN][2024-07-30 15:05:22] ...lsp/handlers.lua:137 "The language server jdtls triggers a registerCapability handler despite dynamicRegistration set to false. Report upstream, this warning is harmless"

The LSP seems to start up fine, except only reports syntax errors. I first thought it could be due to the way that my gradle project is structured, which is like so:

app
└───src
    ├───main
    │   ├───java
    │   │   └───org
    │   │       └───example
    │   │           
    │   └───resources
    └───test
        ├───java
        │   └───org
        │       └───example
        └───resources

Where the build.gradle file is in the same level as the src directory, but the project files start with package org.example, which could be confusing the LSP.

This is my configuration with jdtls

    local root_markers = {'.git', 'mvnw', 'gradlew', 'pom.xml', 'build.gradle'}
    local root_dir = require('jdtls.setup').find_root(root_markers)
    local workspace_folder = root_dir .. '/jdtls/'
    local jdtlsconfig = {
        cmd = {
        'java', -- or '/path/to/java17_or_newer/bin/java'
                -- depends on if `java` is in your $PATH env variable and if it points to the right version.

        '-Declipse.application=org.eclipse.jdt.ls.core.id1',
        '-Dosgi.bundles.defaultStartLevel=4',
        '-Declipse.product=org.eclipse.jdt.ls.core.product',
        '-Dlog.protocol=true',
        '-Dlog.level=ALL',
        '-Xmx1g',
        '--add-modules=ALL-SYSTEM',
        '--add-opens', 'java.base/java.util=ALL-UNNAMED',
        '--add-opens', 'java.base/java.lang=ALL-UNNAMED',
        '-jar', 'C:/Users/Owner/AppData/Local/eclipse-jdtls/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar',
        '-configuration', 'C:/Users/Owner/AppData/Local/eclipse-jdtls/config_win/', 
        '-data', workspace_folder},
        root_dir = root_dir ,
        settings = {
            java = {
                configuration = {
                      runtimes = {
                  {name = "JavaSE-17",
                  path = '"C:\\Program Files\\Amazon Corretto\\jdk17.0.7_7\\"'},
                  {name = "JavaSE-11",
                  path = '"C:\\Program Files\\Amazon Corretto\\jdk11.0.15_9\\"'}
                  }
                    },
            import = {
              gradle = {
                enabled = true
              }
            }
            }
        }
    }

    require('jdtls').start_or_attach(jdtlsconfig)

Additionally, there are some errors in the project files that would require context about other files (such as classes not inheriting all the methods from their abstract superclasses), but errors are not marked because of the files not being recognized as project files.

How do I fix this?

EDIT

The problem may be to do with the LSP not being able to access the JAVASE-17 and JAVASE-11 runtimes that I have specified (due to their paths having a space in them). Could this be the reason?


Solution

  • After looking at this answer, the -data variable is not supposed to be in the same directory as the project, causing jdtls to break. I also changed the paths in the runtimes to not use backslashes and added a slash at the end, making each runtime work properly.