mirror of
https://github.com/NotAShelf/direnv.nvim.git
synced 2026-05-19 13:30:31 +00:00
fix: defer to direnv for env tracking
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I6e8b1291ee589ab2f398f24dd7bad1966a6a6964
This commit is contained in:
parent
be0b3fd58f
commit
be64c2efb7
1 changed files with 36 additions and 18 deletions
|
|
@ -23,9 +23,6 @@ local cache = {
|
||||||
pending_request = false,
|
pending_request = false,
|
||||||
}
|
}
|
||||||
|
|
||||||
local direnv_env = {}
|
|
||||||
local unload_pending = false
|
|
||||||
|
|
||||||
local notification_queue = {}
|
local notification_queue = {}
|
||||||
local pending_callbacks = {}
|
local pending_callbacks = {}
|
||||||
|
|
||||||
|
|
@ -218,23 +215,48 @@ M.refresh_status = function()
|
||||||
M._get_rc_status(function() end)
|
M._get_rc_status(function() end)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Unload direnv environment by clearing tracked variables
|
--- Unload direnv environment by running direnv export in the current directory.
|
||||||
|
--- direnv handles proper env restoration (including $PATH), unlike manual tracking.
|
||||||
M._unload = function()
|
M._unload = function()
|
||||||
if next(direnv_env) == nil then
|
local cwd = get_cwd()
|
||||||
|
if not cwd then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
unload_pending = true
|
vim.system(
|
||||||
vim.schedule(function()
|
{ M.config.bin, "export", "json" },
|
||||||
if unload_pending then
|
{ text = true, cwd = cwd },
|
||||||
for key, _ in pairs(direnv_env) do
|
function(obj)
|
||||||
vim.env[key] = nil
|
if obj.code ~= 0 then
|
||||||
|
return
|
||||||
end
|
end
|
||||||
direnv_env = {}
|
|
||||||
unload_pending = false
|
vim.schedule(function()
|
||||||
notify("direnv environment unloaded", vim.log.levels.DEBUG)
|
local stdout = obj.stdout or ""
|
||||||
|
if stdout == "" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local ok, env = pcall(vim.json.decode, stdout)
|
||||||
|
if not ok or type(env) ~= "table" then
|
||||||
|
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
|
||||||
|
|
||||||
|
notify("direnv environment unloaded", vim.log.levels.DEBUG)
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Initialize direnv for current directory
|
--- Initialize direnv for current directory
|
||||||
|
|
@ -280,9 +302,6 @@ M._init = function(path)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
unload_pending = false
|
|
||||||
direnv_env = {}
|
|
||||||
|
|
||||||
for key, value in pairs(env) do
|
for key, value in pairs(env) do
|
||||||
if value == vim.NIL or value == nil then
|
if value == vim.NIL or value == nil then
|
||||||
vim.env[key] = nil
|
vim.env[key] = nil
|
||||||
|
|
@ -291,7 +310,6 @@ M._init = function(path)
|
||||||
value = tostring(value)
|
value = tostring(value)
|
||||||
end
|
end
|
||||||
vim.env[key] = value
|
vim.env[key] = value
|
||||||
direnv_env[key] = true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue