diff --git a/runtime/lua/vim/_system.lua b/runtime/lua/vim/_system.lua index 23a32fbd49..38e348bc03 100644 --- a/runtime/lua/vim/_system.lua +++ b/runtime/lua/vim/_system.lua @@ -208,15 +208,13 @@ end --- @param clear_env? boolean --- @return string[]? local function setup_env(env, clear_env) - if clear_env then - return env + if not clear_env then + --- @type table + env = vim.tbl_extend('force', base_env(), env or {}) end - --- @type table - env = vim.tbl_extend('force', base_env(), env or {}) - local renv = {} --- @type string[] - for k, v in pairs(env) do + for k, v in pairs(env or {}) do renv[#renv + 1] = string.format('%s=%s', k, tostring(v)) end diff --git a/test/functional/lua/system_spec.lua b/test/functional/lua/system_spec.lua index 52206b7906..97972698a6 100644 --- a/test/functional/lua/system_spec.lua +++ b/test/functional/lua/system_spec.lua @@ -73,6 +73,26 @@ describe('vim.system', function() eq('hellocat', system({ 'cat' }, { stdin = 'hellocat', text = true }).stdout) end) + it('can set environment', function() + eq( + 'TESTVAL', + system( + { n.testprg('printenv-test'), 'TEST' }, + { env = { TEST = 'TESTVAL' }, text = true } + ).stdout + ) + end) + + it('can set environment with clear_env = true', function() + eq( + 'TESTVAL', + system( + { n.testprg('printenv-test'), 'TEST' }, + { clear_env = true, env = { TEST = 'TESTVAL' }, text = true } + ).stdout + ) + end) + it('supports timeout', function() eq({ code = 124,