use direnv export json for multiline env vars

This commit is contained in:
Tim Macfarlane 2026-01-20 10:43:39 +01:00
commit f2533e2d55

View file

@ -224,13 +224,24 @@ M._init = function(path)
end end
vim.schedule(function() vim.schedule(function()
local env_commands = vim.split(obj.stdout, "\n") local ok, env = pcall(vim.json.decode, obj.stdout)
if #env_commands > 0 then
for _, cmd in ipairs(env_commands) do if not ok or type(env) ~= "table" then
if cmd ~= "" then notify(
pcall(function() "Failed to parse direnv JSON output",
vim.cmd(cmd) vim.log.levels.ERROR
end) )
return
end
for key, value in pairs(env) do
if value == vim.NIL or value == nil then
vim.env[key] = nil
else
if type(value) ~= "string" then
value = tostring(value)
end
vim.env[key] = value
end end
end end
@ -244,12 +255,11 @@ M._init = function(path)
"User", "User",
{ pattern = "DirenvLoaded", modeline = false } { pattern = "DirenvLoaded", modeline = false }
) )
end
end) end)
end end
vim.system( vim.system(
{ M.config.bin, "export", "vim" }, { M.config.bin, "export", "json" },
{ text = true, cwd = cwd }, { text = true, cwd = cwd },
on_exit on_exit
) )