mfussenegger / nvim-jdtls

Extensions for the built-in LSP support in Neovim for eclipse.jdt.ls
GNU General Public License v3.0
1.05k stars 61 forks source link

Document sample Windows configurations for cmd #479

Closed gbroques closed 1 year ago

gbroques commented 1 year ago

Problem Statement

Hello Mathias and others.

I found it difficult to get this working on my Windows machine (using Cygwin).

I currently develop Java 11, and need that to be in my PATH.

As you know, JDTLS needs Java 17 to work though.

What I did to try and work around this was provide a direct path to my java 17 executable:

cmd = {
    "/cygdrive/c/Program\\ Files/Java/jdk-17.0.4.1/bin/java",  -- raises "LSP cmd must be an executable" ❌
    "-Declipse.application=org.eclipse.jdt.ls.core.id1",
    -- .. include other arguments
},

However, that raises the following error:

LSP cmd must be an executable: ...

See the following assert statement in setup: https://github.com/mfussenegger/nvim-jdtls/blob/0.2.0/lua/jdtls/setup.lua#L218-L219

vim.fn.executable is not reliable on Windows. Even when I added the JDK 17 bin directory to my PATH (after my JDK 11 bin directory), vim.fn.executable still returned 0.

See https://github.com/wbthomason/packer.nvim/issues/49 for another report of this issue.

To workaround this, I found a solution from the lspconfig project to use cmd.exe /c instead. vim.fn.executable returns 1 for cmd.exe.

Then getting the path to my JDK 17 right, with forward-slashes, back-slashes, and the space in "Program Files" took awhile to figure out.

Altogether a working Windows cmd looks like:

cmd = {
    "cmd.exe",
    "/c",
    "C:/Program Files/Java/jdk-17.0.4.1/bin/java",
    "-Declipse.application=org.eclipse.jdt.ls.core.id1",
    -- ... include other arguments
}

A more complete example can be found in my GitHub config.

Anyways, it would be nice to document this somehow to hopefully prevent other Windows users the same few hours it took me to get this working.


P.S. If you happen to develop Java 17, and have its bin directory as the 1st JDK directory in your PATH, then cmd with java works fine:

cmd = {
    "java",  -- works fine on Windows if Java 17 is the 1st JDK directory in your PATH.
    "-Declipse.application=org.eclipse.jdt.ls.core.id1",
    -- .. include other arguments
},

For others who don't want Java 17 on their PATH, and want to provide a directory to their JDK 17 installation instead, may have trouble configuring it on Windows without a sample configuration.

Ideas or possible solutions

No response

mfussenegger commented 1 year ago

I removed the executable check with https://github.com/mfussenegger/nvim-jdtls/pull/481

Does this fix the problem, or does it still require the cmd.exe workaround?

gbroques commented 1 year ago

I removed the executable check with #481

Does this fix the problem, or does it still require the cmd.exe workaround?

Thank you for the swift reply and fix Mathias! 🚀

I re-tested with the latest commit, and no longer need the cmd.exe /c prefix for cmd.

It can be tricky on Windows depending on whether you're using a "unix-like" environment such as Cygwin or Git for Windows to know the correct path, slashes, and escaping for configuration.

For me on Windows / Cygwin, here's what worked and didn't work:

cmd = {
    "C:/Program Files/Java/jdk-17.0.4.1/bin/java", -- this works ✔
    "-Declipse.application=org.eclipse.jdt.ls.core.id1",
    -- ... include other arguments
}
cmd = {
    "/cygdrive/c/Program\\ Files/Java/jdk-17.0.4.1/bin/java", -- this doesn't work ❌
    "-Declipse.application=org.eclipse.jdt.ls.core.id1",
    -- ... include other arguments
}

We can probably close this out, but an example cmd somewhere for Windows would probably still help someone in the future.

Maybe a Windows configuration section in the Sample Configurations page of the Wiki?

Is the preference for it to be a ful and working configuration, rather than a configuration snippet?

I don't really want to link to my dot files as I may switch to Mac in the future.

I could link to a specific commit of my dotfiles though.

mfussenegger commented 1 year ago

Maybe a Windows configuration section in the Sample Configurations page of the Wiki?

That's preferable. This plugin targets more experienced users, not beginners, so it in general assumes that people know their way around their environment and how to format paths.