Skip to content

Commit

Permalink
fix(quickfix): whitespace in directory path will make quickfix execut…
Browse files Browse the repository at this point in the history
…or fail
  • Loading branch information
Civitasv committed Jun 8, 2024
1 parent 8853f79 commit 168f8b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 59 deletions.
44 changes: 2 additions & 42 deletions lua/cmake-tools/environment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
35 changes: 19 additions & 16 deletions lua/cmake-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
".",
}
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lua/cmake-tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 168f8b1

Please sign in to comment.