Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#44): support env var specification #47

Merged
merged 3 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lua/guard/filetype.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ local function box()
return self
end

function tbl:env(env)
vim.validate({
env = { env, 'table' }
})
if vim.tbl_count(env) == 0 then
return self
end
local tool = self[current][#self[current]]
if type(tool) == 'string' then
tool = current == 'format' and require('guard.tools.formatter')[tool]
or require('guard.tools.linter.' .. tool)
end
tool.env = {}
env = vim.tbl_extend('force', vim.uv.os_environ(), env or {})
for k, v in pairs(env) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm why we need these codes. just tool.env = env ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I say that above:

Also note that we cannot set tool.env = env because uv.os_environ() returns the environment like:

{ key = val }
and uv.spawn() needs the env like:

{ 'key=val' }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's make sense but do we need extend os_environ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I believe applying an env override removes all environment variables from the uv.spawn() process or something.

As evidence, it just doesn't work with spawning certain processes (like prettierd, for example) and breaks. Keeping uv.os_environ() fixes it. null-ls also does it.

tool.env[#tool.env + 1 ] = ('%s=%s'):format(k, tostring(v))
end
return self
end

function tbl:key_alias(key)
local _t = {
['lint'] = function()
Expand Down
2 changes: 1 addition & 1 deletion lua/guard/spawn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ local function spawn(opt)
stdio = { stdin, stdout, stderr },
args = opt.args,
cwd = opt.cwd,
env = opt.env_flat or nil,
env = opt.env,
}, function(exit_code, signal)
if timeout then
timeout:stop()
Expand Down