From 168f8b19e361915855aba1017d04089684e32ad7 Mon Sep 17 00:00:00 2001 From: Civitasv Date: Sat, 8 Jun 2024 10:22:24 +0800 Subject: [PATCH] fix(quickfix): whitespace in directory path will make quickfix executor fail --- lua/cmake-tools/environment.lua | 44 ++------------------------------- lua/cmake-tools/init.lua | 35 ++++++++++++++------------ lua/cmake-tools/utils.lua | 5 +++- 3 files changed, 25 insertions(+), 59 deletions(-) diff --git a/lua/cmake-tools/environment.lua b/lua/cmake-tools/environment.lua index e4e02a2c..57554883 100644 --- a/lua/cmake-tools/environment.lua +++ b/lua/cmake-tools/environment.lua @@ -2,46 +2,6 @@ local osys = require("cmake-tools.osys") local environment = {} --- expected format: --- --- { --- inherit_base_environment = true/false -- for launch targets only --- env = { --- VERBOSE = 1, --- SOME_PATH = "/tmp/test.txt", --- } --- } - --- terminal needs strings to be esacaped --- for quickfix (plenary.job) dont escape strings because of how it's being passed to cmake. --- We dont want additional " " in the env vars -local function unroll(env, escape) - local res = {} - - if osys.iswin32 then - escape = false -- windows wants env vars unescaped -> set VAL=TEST1 TEST2 ... - end - - for k, v in pairs(env) do - local var = k - if type(v) == "string" then - if escape then - var = var .. '="' .. v .. '"' - else - var = var .. "=" .. v - end - table.insert(res, var) - elseif type(v) == "number" then - var = var .. "=" .. v - table.insert(res, var) - else - -- unsupported type - end - end - - return res -end - -- parse and merge configured environment variables function environment.get_build_environment_table(config) local env = {} @@ -58,7 +18,7 @@ function environment.get_build_environment_table(config) return env end -function environment.get_build_environment(config, escape) +function environment.get_build_environment(config) local env = environment.get_build_environment_table(config) return env end @@ -88,7 +48,7 @@ function environment.get_run_environment_table(config, target) return env end -function environment.get_run_environment(config, target, escape) +function environment.get_run_environment(config, target) return environment.get_run_environment_table(config, target) end diff --git a/lua/cmake-tools/init.lua b/lua/cmake-tools/init.lua index 449c0bd5..e515080d 100644 --- a/lua/cmake-tools/init.lua +++ b/lua/cmake-tools/init.lua @@ -111,7 +111,7 @@ function cmake.generate(opt, callback) vim.list_extend(args, config:generate_options()) vim.list_extend(args, fargs) - local env = environment.get_build_environment(config, config.executor.name == "terminal") + local env = environment.get_build_environment(config) local cmd = const.cmake_command return utils.execute(cmd, config.env_script, env, args, config.cwd, config.executor, function() if type(callback) == "function" then @@ -163,7 +163,7 @@ function cmake.generate(opt, callback) local args = { "-B", - utils.transform_path(config:build_directory_path()), + utils.transform_path(config:build_directory_path(), config.executor.name == "quickfix"), "-S", ".", } @@ -172,7 +172,7 @@ function cmake.generate(opt, callback) vim.list_extend(args, config:generate_options()) vim.list_extend(args, fargs) - local env = environment.get_build_environment(config, config.executor.name == "terminal") + local env = environment.get_build_environment(config) local cmd = const.cmake_command env = vim.tbl_extend("keep", env, kit_option.env) return utils.execute(cmd, config.env_script, env, args, config.cwd, config.executor, function() @@ -195,10 +195,14 @@ function cmake.clean(callback) return log.error(result.message) end - local args = - { "--build", utils.transform_path(config:build_directory_path()), "--target", "clean" } + local args = { + "--build", + utils.transform_path(config:build_directory_path(), config.executor.name == "quickfix"), + "--target", + "clean", + } - local env = environment.get_build_environment(config, config.executor.name == "terminal") + local env = environment.get_build_environment(config) local cmd = const.cmake_command return utils.execute(cmd, config.env_script, env, args, config.cwd, config.executor, function() if type(callback) == "function" then @@ -247,7 +251,10 @@ function cmake.build(opt, callback) if presets_file and config.build_preset then args = { "--build", "--preset", config.build_preset } -- preset don't need define build dir. else - args = { "--build", utils.transform_path(config:build_directory_path()) } + args = { + "--build", + utils.transform_path(config:build_directory_path(), config.executor.name == "quickfix"), + } end vim.list_extend(args, config:build_options()) @@ -263,7 +270,7 @@ function cmake.build(opt, callback) vim.list_extend(args, fargs) end - local env = environment.get_build_environment(config, config.executor.name == "terminal") + local env = environment.get_build_environment(config) local cmd = const.cmake_command return utils.execute(cmd, config.env_script, env, args, config.cwd, config.executor, function() if type(callback) == "function" then @@ -411,8 +418,7 @@ function cmake.run(opt) local target_path = result.data local launch_path = cmake.get_launch_path(opt.target) - local env = - environment.get_run_environment(config, opt.target, config.runner.name == "terminal") + local env = environment.get_run_environment(config, opt.target) local _args = opt.args and opt.args or config.target_settings[opt.target].args local cmd = target_path utils.run( @@ -453,11 +459,7 @@ function cmake.run(opt) local launch_path = cmake.get_launch_path(cmake.get_launch_target()) - local env = environment.get_run_environment( - config, - config.launch_target, - config.runner.name == "terminal" - ) + local env = environment.get_run_environment(config, config.launch_target) local cmd = target_path utils.run( cmd, @@ -925,7 +927,7 @@ function cmake.run_test(opt) if utils.has_active_job(config.runner, config.executor) then return end - local env = environment.get_build_environment(config, config.executor.name == "terminal") + local env = environment.get_build_environment(config) local all_tests = ctest.list_all_tests(config:build_directory_path()) if #all_tests == 0 then return @@ -1307,6 +1309,7 @@ function cmake.register_autocmd() local targets = {} local file = ev.file local all_targets = config:build_targets_with_sources() + vim.print(all_targets) if all_targets and all_targets.data and all_targets.data["sources"] then for _, target in ipairs(all_targets.data["sources"]) do if target.path == file then diff --git a/lua/cmake-tools/utils.lua b/lua/cmake-tools/utils.lua index 0fe387b3..279265b0 100644 --- a/lua/cmake-tools/utils.lua +++ b/lua/cmake-tools/utils.lua @@ -96,7 +96,10 @@ function utils.softlink(src, target) end end -function utils.transform_path(path) +function utils.transform_path(path, keep) + if keep then + return path + end if path[1] ~= '"' and string.find(path, " ") then return '"' .. path .. '"' else