Skip to content

Commit 15bc822

Browse files
authored
feat: add validations for exec order, duplicate setup calls (nvim-java#219)
NOTE: Previous installation instruction we had are causing jdtls to setup multiple times and on some configuration jdtls setup is done before nvim-java setup (nvim-java should always should be setup before jdtls). - Now there are validations for execution order check. For example, if the jdtls setup is called before the nvim-java setup, user will see an error on startup (This is not perfect but it's something). You can either remove the check from the nvim-java configuration if everything works fine for you and you don't want to touch anything. But if you have time, check the latest installation instruction or try to figure out what's causing the issue in your config. - Additionally, we check is nvim-java setup is being called more than once. If you are passing some configuration to nvim-java if they are not working, this is probably why. First time it's setup with the config you are passing but using the default in consequent calls. Once again, you can check the instructions to fix the issue.
1 parent 96472bf commit 15bc822

7 files changed

Lines changed: 213 additions & 44 deletions

File tree

README.md

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -264,38 +264,58 @@ want, following options are available
264264

265265
```lua
266266
{
267-
-- list of file that exists in root of the project
268-
root_markers = {
269-
'settings.gradle',
270-
'settings.gradle.kts',
271-
'pom.xml',
272-
'build.gradle',
273-
'mvnw',
274-
'gradlew',
275-
'build.gradle',
276-
'build.gradle.kts',
277-
'.git',
278-
},
279-
280-
-- load java test plugins
281-
java_test = {
282-
enable = true,
283-
},
284-
285-
-- load java debugger plugins
286-
java_debug_adapter = {
287-
enable = true,
288-
},
289-
290-
jdk = {
291-
-- install jdk using mason.nvim
292-
auto_install = true,
293-
},
294-
295-
notifications = {
296-
-- enable 'Configuring DAP' & 'DAP configured' messages on start up
297-
dap = true,
298-
},
267+
-- list of file that exists in root of the project
268+
root_markers = {
269+
'settings.gradle',
270+
'settings.gradle.kts',
271+
'pom.xml',
272+
'build.gradle',
273+
'mvnw',
274+
'gradlew',
275+
'build.gradle',
276+
'build.gradle.kts',
277+
'.git',
278+
},
279+
280+
-- load java test plugins
281+
java_test = {
282+
enable = true,
283+
},
284+
285+
-- load java debugger plugins
286+
java_debug_adapter = {
287+
enable = true,
288+
},
289+
290+
jdk = {
291+
-- install jdk using mason.nvim
292+
auto_install = true,
293+
},
294+
295+
notifications = {
296+
-- enable 'Configuring DAP' & 'DAP configured' messages on start up
297+
dap = true,
298+
},
299+
300+
-- We do multiple verifications to make sure things are in place to run this
301+
-- plugin
302+
verification = {
303+
-- nvim-java checks for the order of execution of following
304+
-- * require('java').setup()
305+
-- * require('lspconfig').jdtls.setup()
306+
-- IF they are not executed in the correct order, you will see a error
307+
-- notification.
308+
-- Set following to false to disable the notification if you know what you
309+
-- are doing
310+
invalid_order = true,
311+
312+
-- nvim-java checks if the require('java').setup() is called multiple
313+
-- times.
314+
-- IF there are multiple setup calls are executed, an error will be shown
315+
-- Set following property value to false to disable the notification if
316+
-- you know what you are doing
317+
duplicate_setup_calls = true,
318+
},
299319
}
300320
```
301321

lua/java.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local decomple_watch = require('java.startup.decompile-watcher')
22
local mason_dep = require('java.startup.mason-dep')
3-
local nvim_dep = require('java.startup.nvim-dep')
43
local setup_wrap = require('java.startup.lspconfig-setup-wrap')
4+
local startup_check = require('java.startup.startup-check')
55

66
local test = require('java.api.test')
77
local dap = require('java.api.dap')
@@ -16,9 +16,12 @@ local M = {}
1616
function M.setup(custom_config)
1717
local config =
1818
vim.tbl_deep_extend('force', global_config, custom_config or {})
19+
1920
vim.g.nvim_java_config = config
2021

21-
nvim_dep.check()
22+
if not startup_check() then
23+
return
24+
end
2225

2326
local is_installing = mason_dep.install(config)
2427

lua/java/config.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
---@field java_debug_adapter { enable: boolean }
55
---@field jdk { auto_install: boolean }
66
---@field notifications { dap: boolean }
7+
---@field verification { invalid_order: boolean, duplicate_setup_calls: boolean }
78
local config = {
89
-- list of file that exists in root of the project
910
root_markers = {
@@ -37,6 +38,26 @@ local config = {
3738
-- enable 'Configuring DAP' & 'DAP configured' messages on start up
3839
dap = true,
3940
},
41+
42+
-- We do multiple verifications to make sure things are in place to run this
43+
-- plugin
44+
verification = {
45+
-- nvim-java checks for the order of execution of following
46+
-- * require('java').setup()
47+
-- * require('lspconfig').jdtls.setup()
48+
-- IF they are not executed in the correct order, you will see a error
49+
-- notification.
50+
-- Set following to false to disable the notification if you know what you
51+
-- are doing
52+
invalid_order = true,
53+
54+
-- nvim-java checks if the require('java').setup() is called multiple
55+
-- times.
56+
-- IF there are multiple setup calls are executed, an error will be shown
57+
-- Set following property value to false to disable the notification if
58+
-- you know what you are doing
59+
duplicate_setup_calls = true,
60+
},
4061
}
4162

4263
return config
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local M = {}
2+
3+
local message = 'require("java").setup() is called more than once'
4+
.. '\nnvim-java will continue to setup but nvim-java configurations might not work as expected'
5+
.. '\nThis might be due to old installation instructions.'
6+
.. '\nPlease check the latest guide at https://github.com/nvim-java/nvim-java#hammer-how-to-install'
7+
.. '\nIf you know what you are doing, you can disable the check from the config'
8+
.. '\nhttps://github.com/nvim-java/nvim-java#wrench-configuration'
9+
10+
function M.is_valid()
11+
if vim.g.nvim_java_setup_is_called then
12+
return {
13+
success = false,
14+
continue = true,
15+
message = message,
16+
}
17+
end
18+
19+
vim.g.nvim_java_setup_is_called = true
20+
21+
return {
22+
success = true,
23+
continue = true,
24+
}
25+
end
26+
27+
return M
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
local lspconfig = require('lspconfig')
2+
3+
local M = {}
4+
5+
lspconfig.util.on_setup = lspconfig.util.add_hook_before(
6+
lspconfig.util.on_setup,
7+
function(config)
8+
if config.name == 'jdtls' then
9+
vim.g.nvim_java_jdtls_setup_is_called = true
10+
end
11+
end
12+
)
13+
14+
local message = 'Looks like require("lspconfig").jdtls.setup() is called before require("java").setup().'
15+
.. '\nnvim-java will continue to setup but most features may not work as expected'
16+
.. '\nThis might be due to old installation instructions.'
17+
.. '\nPlease check the latest guide at https://github.com/nvim-java/nvim-java#hammer-how-to-install'
18+
.. '\nIf you know what you are doing, you can disable the check from the config'
19+
.. '\nhttps://github.com/nvim-java/nvim-java#wrench-configuration'
20+
21+
function M.is_valid()
22+
if vim.g.nvim_java_jdtls_setup_is_called then
23+
return {
24+
success = false,
25+
continue = true,
26+
message = message,
27+
}
28+
end
29+
30+
local clients = vim.lsp.get_clients({ name = 'jdtls' })
31+
32+
if #clients > 0 then
33+
return {
34+
success = false,
35+
continue = true,
36+
message = message,
37+
}
38+
end
39+
40+
return {
41+
success = true,
42+
continue = true,
43+
}
44+
end
45+
46+
return M

lua/java/startup/nvim-dep.lua

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local notify = require('java-core.utils.notify')
21
local log = require('java.utils.log')
32

43
local pkgs = {
@@ -36,26 +35,33 @@ Please follow the install guide in https://github.com/nvim-java/nvim-java to ins
3635

3736
local M = {}
3837

39-
function M.check()
38+
function M.is_valid()
4039
log.info('check neovim plugin dependencies')
41-
M.neovim_plugin_check()
42-
end
4340

44-
---@private
45-
function M.neovim_plugin_check()
4641
for _, pkg in ipairs(pkgs) do
4742
local ok, _ = pcall(require, pkg.name)
4843

4944
if not ok then
5045
if pkg.warn then
51-
log.warn(pkg.warn)
52-
notify.warn(pkg.warn)
46+
return {
47+
success = false,
48+
continue = true,
49+
message = pkg.warn,
50+
}
5351
else
54-
log.error(pkg.err)
55-
error(pkg.err)
52+
return {
53+
success = false,
54+
continue = false,
55+
message = pkg.err,
56+
}
5657
end
5758
end
5859
end
60+
61+
return {
62+
success = true,
63+
continue = true,
64+
}
5965
end
6066

6167
return M

lua/java/startup/startup-check.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
local log = require('java.utils.log')
2+
local notify = require('java-core.utils.notify')
3+
4+
local function get_checkers()
5+
local config = vim.g.nvim_java_config
6+
local checks = {}
7+
8+
if config.verification.invalid_order then
9+
table.insert(checks, select(1, require('java.startup.exec-order-check')))
10+
end
11+
12+
if config.verification.duplicate_setup_calls then
13+
table.insert(
14+
checks,
15+
select(1, require('java.startup.duplicate-setup-check'))
16+
)
17+
end
18+
19+
table.insert(checks, select(1, require('java.startup.nvim-dep')))
20+
21+
return checks
22+
end
23+
24+
return function()
25+
local checkers = get_checkers()
26+
27+
for _, check in ipairs(checkers) do
28+
local check_res = check.is_valid()
29+
30+
if check_res.message then
31+
if not check_res.success then
32+
log.error(check_res.message)
33+
notify.error(check_res.message)
34+
else
35+
log.warn(check_res.message)
36+
notify.warn(check_res.message)
37+
end
38+
end
39+
40+
if not check_res.continue then
41+
return false
42+
end
43+
end
44+
45+
return true
46+
end

0 commit comments

Comments
 (0)