diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index 50a757cd7c..04b70a1c61 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -1778,7 +1778,9 @@ vim.system({cmd}, {opts}, {on_exit}) *vim.system()* the new process. Inherits the current environment with `NVIM` set to |v:servername|. • clear_env: (boolean) `env` defines the job environment - exactly, instead of merging current environment. + exactly, instead of merging current environment. Note: if + `env` is `nil`, the current environment is used but + without `NVIM` set. • stdin: (string|string[]|boolean) If `true`, then a pipe to stdin is opened and can be written to via the `write()` method to SystemObj. If string or string[] then diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 735e746a70..a8c26eedea 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -106,7 +106,7 @@ local utfs = { --- - env: table Set environment variables for the new process. Inherits the --- current environment with `NVIM` set to |v:servername|. --- - clear_env: (boolean) `env` defines the job environment exactly, instead of merging current ---- environment. +--- environment. Note: if `env` is `nil`, the current environment is used but without `NVIM` set. --- - stdin: (string|string[]|boolean) If `true`, then a pipe to stdin is opened and can be written --- to via the `write()` method to SystemObj. If string or string[] then will be written to stdin --- and closed. Defaults to `false`. diff --git a/runtime/lua/vim/_system.lua b/runtime/lua/vim/_system.lua index 0621f1c0a3..6ccec6df70 100644 --- a/runtime/lua/vim/_system.lua +++ b/runtime/lua/vim/_system.lua @@ -210,13 +210,18 @@ end --- @param clear_env? boolean --- @return string[]? local function setup_env(env, clear_env) + if not env and clear_env then + return + end + + env = env or {} if not clear_env then --- @type table - env = vim.tbl_extend('force', base_env(), env or {}) + env = vim.tbl_extend('force', base_env(), env) end local renv = {} --- @type string[] - for k, v in pairs(env or {}) do + for k, v in pairs(env) 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 97972698a6..7016ffbc59 100644 --- a/test/functional/lua/system_spec.lua +++ b/test/functional/lua/system_spec.lua @@ -93,6 +93,27 @@ describe('vim.system', function() ) end) + it('can set environment with clear_env = true and env = nil', function() + exec_lua(function() + vim.env.TEST = 'TESTVAL' + end) + + -- Not passing env with clear_env should not clear the environment + eq( + 'TESTVAL', + system({ n.testprg('printenv-test'), 'TEST' }, { clear_env = true, text = true }).stdout + ) + + -- Passing env = {} with clear_env should clear the environment + eq( + '', + system( + { n.testprg('printenv-test'), 'TEST' }, + { env = {}, clear_env = true, text = true } + ).stdout + ) + end) + it('supports timeout', function() eq({ code = 124,