Make java and neotest config portable

jdtls hardcoded /Library/Java/JavaVirtualMachines paths, which resolve to
nothing off macOS; discover JDKs across the macOS, Linux and sdkman roots
instead and only declare runtimes that exist. neotest-mocha passed `docker exec
-it`, which hangs without a TTY, and hardcoded the container name.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-31 12:27:20 -04:00
co-authored by Claude Opus 5
parent d805ae3ee4
commit cf36c2f164
2 changed files with 43 additions and 11 deletions
+40 -8
View File
@@ -1,15 +1,50 @@
local function first_jdk(candidates)
for _, path in ipairs(candidates) do
if vim.fn.executable(path .. "/bin/java") == 1 then
return path
end
end
end
local function find_jdk(patterns)
local roots = {
"/Library/Java/JavaVirtualMachines/%s/Contents/Home",
"/usr/lib/jvm/%s",
vim.fn.expand("~") .. "/.sdkman/candidates/java/%s",
}
local candidates = {}
for _, pattern in ipairs(patterns) do
for _, root in ipairs(roots) do
for _, path in ipairs(vim.fn.glob(root:format(pattern), true, true)) do
candidates[#candidates + 1] = path
end
end
end
return first_jdk(candidates)
end
return {
{
"mfussenegger/nvim-jdtls",
opts = function(_, opts)
local corretto8 = "/Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home"
local temurin21 = "/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home"
local jdk8 = find_jdk({ "*corretto-8*", "*corretto*1.8*", "*-8-*", "*java-8*", "*1.8*" })
local jdk21 = find_jdk({ "*temurin-21*", "*-21-*", "*java-21*", "*21*" })
if jdk21 then
opts.cmd = opts.cmd or {}
table.insert(opts.cmd, "--java-executable")
table.insert(opts.cmd, temurin21 .. "/bin/java")
table.insert(opts.cmd, jdk21 .. "/bin/java")
end
local project_jdk = vim.env.JAVA_HOME or temurin21
local runtimes = {}
if jdk8 then
table.insert(runtimes, { name = "JavaSE-1.8", path = jdk8 })
end
if jdk21 then
table.insert(runtimes, { name = "JavaSE-21", path = jdk21 })
end
local project_jdk = vim.env.JAVA_HOME or jdk21
opts.settings = vim.tbl_deep_extend("force", opts.settings or {}, {
java = {
@@ -19,10 +54,7 @@ return {
},
},
configuration = {
runtimes = {
{ name = "JavaSE-1.8", path = corretto8 },
{ name = "JavaSE-21", path = temurin21 },
},
runtimes = runtimes,
},
},
})
+1 -1
View File
@@ -7,7 +7,7 @@ return {
return {
adapters = {
["neotest-mocha"] = {
command = "docker exec -it api-sandbox npm run test:integration",
command = "docker exec " .. (vim.env.NEOTEST_MOCHA_CONTAINER or "api-sandbox") .. " npm run test:integration",
command_args = function(context)
-- The context contains:
-- results_path: The file that json results are written to