Skip to content

Commit 943438b

Browse files
maulceWolf
authored andcommitted
fix: Indexing a nil value when no target launch settings are configured
When running a specific target there might not be a `target_settings` config available. Previously the `target_settings` of that target were always accessed assuming it was there. Now each access is checked and an empty table is used as a fallback.
1 parent 1d96327 commit 943438b

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

lua/cmake-tools/init.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,9 @@ function cmake.run(opt, callback)
525525

526526
local launch_path = cmake.get_launch_path(opt.target)
527527
local env = environment.get_run_environment(config, opt.target)
528-
local _args = opt.args and opt.args or config.target_settings[opt.target].args
528+
local _args = opt.args and opt.args
529+
or utils.get_nested(config, "target_settings", opt.target, "args")
530+
or {}
529531
local cmd = target_path
530532
utils.run(cmd, config.env_script, env, _args, launch_path, config.runner, callback)
531533
end)
@@ -1573,7 +1575,9 @@ function cmake.register_dap_function()
15731575
name = opt.target,
15741576
program = result.data,
15751577
cwd = cmake.get_launch_path(opt.target),
1576-
args = opt.args and opt.args or config.target_settings[opt.target].args,
1578+
args = opt.args and opt.args
1579+
or utils.get_nested(config, "target_settings", opt.target, "args")
1580+
or {},
15771581
env = env,
15781582
initCommands = initCmds,
15791583
}

lua/cmake-tools/utils.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,15 @@ function utils.execute(cmd, env_script, env, args, cwd, executor, callback)
269269
end, notify_update_line(ntfy))
270270
end
271271

272+
function utils.get_nested(tbl, ...)
273+
local value = tbl
274+
for _, key in ipairs({ ... }) do
275+
value = value and value[key]
276+
if value == nil then
277+
return nil
278+
end
279+
end
280+
return value
281+
end
282+
272283
return utils

0 commit comments

Comments
 (0)