From 7f7c444a2d25eb4236580771ff85ce27d5fb5542 Mon Sep 17 00:00:00 2001 From: Denzel <> Date: Thu, 2 Apr 2026 12:10:34 +0200 Subject: [PATCH] fix(executer): shell quote arguments --- lua/cmake-tools/init.lua | 8 +++----- lua/cmake-tools/terminal.lua | 4 ++-- lua/cmake-tools/test/ctest.lua | 2 +- lua/cmake-tools/toggleterm.lua | 6 +++--- lua/cmake-tools/utils.lua | 19 ++++++++----------- lua/cmake-tools/vimux.lua | 6 +++--- 6 files changed, 20 insertions(+), 25 deletions(-) diff --git a/lua/cmake-tools/init.lua b/lua/cmake-tools/init.lua index 08a1cb91..6a1007fc 100644 --- a/lua/cmake-tools/init.lua +++ b/lua/cmake-tools/init.lua @@ -257,7 +257,7 @@ function cmake.generate(opt, callback) local args = { "-B", - utils.transform_path(config:build_directory_path(), config.executor.name == "quickfix"), + config:build_directory_path(), "-S", ".", } @@ -298,9 +298,7 @@ function cmake.clean(callback) return end - local path = Path:new( - utils.transform_path(config:build_directory_path(), config.executor.name == "quickfix") - ) + local path = Path:new(config:build_directory_path()) if not (path / "CMakeCache.txt"):exists() then -- no need to clean up as we do not have a cache return @@ -407,7 +405,7 @@ function cmake.build(opt, callback) else args = { "--build", - utils.transform_path(config:build_directory_path(), config.executor.name == "quickfix"), + config:build_directory_path(), } end diff --git a/lua/cmake-tools/terminal.lua b/lua/cmake-tools/terminal.lua index c377463d..b5182002 100644 --- a/lua/cmake-tools/terminal.lua +++ b/lua/cmake-tools/terminal.lua @@ -602,14 +602,14 @@ function _terminal.run(cmd, env_script, env, args, cwd, opts, on_exit, on_output if osys.iswin32 and not is_windows_shell() then cwd = cwd:gsub("\\", "/") end - cwd = utils.transform_path(cwd) + cwd = utils.shell_quote(cwd) local envTbl = {} local fmtStr = osys.iswin32 and "set %s=%s" or "%s=%s" for k, v in pairs(env) do table.insert(envTbl, string.format(fmtStr, k, v)) end env = table.concat(envTbl, " ") - args = table.concat(args, " ") + args = table.concat(vim.tbl_map(utils.shell_quote, args), " ") return cmd, env, args, cwd end diff --git a/lua/cmake-tools/test/ctest.lua b/lua/cmake-tools/test/ctest.lua index 75913fb7..2999d1d3 100644 --- a/lua/cmake-tools/test/ctest.lua +++ b/lua/cmake-tools/test/ctest.lua @@ -77,7 +77,7 @@ function ctest.run(ctest_command, env, config, opt) if opt.preset then vim.list_extend(args, { "--preset", opt.preset }) else - vim.list_extend(args, { "--test-dir", utils.transform_path(opt.build_dir) }) + vim.list_extend(args, { "--test-dir", opt.build_dir }) end if opt.label then diff --git a/lua/cmake-tools/toggleterm.lua b/lua/cmake-tools/toggleterm.lua index 4f2068d5..f4a7e47a 100644 --- a/lua/cmake-tools/toggleterm.lua +++ b/lua/cmake-tools/toggleterm.lua @@ -28,7 +28,7 @@ function _toggleterm.run(cmd, env_script, env, args, cwd, opts, on_exit, on_outp local full_cmd = "" -- Launch form executable's build directory by default - full_cmd = "cd " .. utils.transform_path(cwd) .. " &&" + full_cmd = "cd " .. utils.shell_quote(cwd) .. " &&" if osys.iswin32 then for k, v in pairs(env) do @@ -40,7 +40,7 @@ function _toggleterm.run(cmd, env_script, env, args, cwd, opts, on_exit, on_outp end end - full_cmd = full_cmd .. " " .. utils.transform_path(cmd) + full_cmd = full_cmd .. " " .. utils.shell_quote(cmd) if osys.islinux or osys.iswsl or osys.ismac then full_cmd = " " .. full_cmd -- adding a space in front of the command prevents bash from recording the command in the history (if configured) @@ -48,7 +48,7 @@ function _toggleterm.run(cmd, env_script, env, args, cwd, opts, on_exit, on_outp -- Add args to the cmd for _, arg in ipairs(args) do - full_cmd = full_cmd .. " " .. arg + full_cmd = full_cmd .. " " .. utils.shell_quote(arg) end _toggleterm.cmd = full_cmd diff --git a/lua/cmake-tools/utils.lua b/lua/cmake-tools/utils.lua index be75c154..8afc38ff 100644 --- a/lua/cmake-tools/utils.lua +++ b/lua/cmake-tools/utils.lua @@ -88,9 +88,9 @@ function utils.copyfile(src, target) -- if we don't always use terminal local cmd = "exec " .. "'!cmake -E copy " - .. utils.transform_path(src) + .. utils.shell_quote(src) .. " " - .. utils.transform_path(target) + .. utils.shell_quote(target) .. "'" vim.cmd(cmd) end @@ -101,22 +101,19 @@ function utils.softlink(src, target) -- if we don't always use terminal local cmd = "exec " .. "'!cmake -E create_symlink " - .. utils.transform_path(src) + .. utils.shell_quote(src) .. " " - .. utils.transform_path(target) + .. utils.shell_quote(target) .. "'" vim.cmd(cmd) end end -function utils.transform_path(path, keep) - if keep then - return path - end - if path[1] ~= '"' and string.find(path, " ") then - return '"' .. path .. '"' +function utils.shell_quote(str) + if str[1] ~= '"' and string.find(str, " ") then + return '"' .. str .. '"' else - return path + return str end end diff --git a/lua/cmake-tools/vimux.lua b/lua/cmake-tools/vimux.lua index 067cd774..529992b1 100644 --- a/lua/cmake-tools/vimux.lua +++ b/lua/cmake-tools/vimux.lua @@ -43,7 +43,7 @@ function _vimux.prepare_cmd_for_run(cmd, env, args, cwd) local full_cmd = "" -- Launch form executable's build directory by default - full_cmd = "cd " .. utils.transform_path(cwd) .. " &&" + full_cmd = "cd " .. utils.shell_quote(cwd) .. " &&" if osys.iswin32 then for k, v in pairs(env) do @@ -55,7 +55,7 @@ function _vimux.prepare_cmd_for_run(cmd, env, args, cwd) end end - full_cmd = full_cmd .. " " .. utils.transform_path(cmd) + full_cmd = full_cmd .. " " .. utils.shell_quote(cmd) if osys.islinux or osys.iswsl or osys.ismac then full_cmd = " " .. full_cmd -- adding a space in front of the command prevents bash from recording the command in the history (if configured) @@ -63,7 +63,7 @@ function _vimux.prepare_cmd_for_run(cmd, env, args, cwd) -- Add args to the cmd for _, arg in ipairs(args) do - full_cmd = full_cmd .. " " .. arg + full_cmd = full_cmd .. " " .. utils.shell_quote(arg) end if osys.iswin32 then -- wrap in sub process to prevent env vars from being persited