Skip to content

Commit 39f5cc0

Browse files
authored
feat(lsp/runnables): support runnable.args.environment (#771)
1 parent e916612 commit 39f5cc0

File tree

9 files changed

+27
-12
lines changed

9 files changed

+27
-12
lines changed

doc/rustaceanvim.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ rustaceanvim.ExecutorOpts *rustaceanvim.ExecutorOpts*
229229
Fields: ~
230230
{bufnr?} (integer)
231231
The buffer from which the executor was invoked.
232+
{env?} (table<string, string>)
233+
The environment variables to set for the command.
232234

233235

234236
rustaceanvim.FloatWinConfig *rustaceanvim.FloatWinConfig*

lua/rustaceanvim/config/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ vim.g.rustaceanvim = vim.g.rustaceanvim
114114
---
115115
---The buffer from which the executor was invoked.
116116
---@field bufnr? integer
117+
---The environment variables to set for the command.
118+
---@field env? table<string, string>
117119

118120
---@class rustaceanvim.FloatWinConfig
119121
---@field auto_focus? boolean

lua/rustaceanvim/executors/background.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ M.execute_command = function(command, args, cwd, opts)
2828
local notify_prefix = (is_single_test and 'test ' or 'tests ')
2929
local cmd = vim.list_extend({ command }, args)
3030
local fname = vim.api.nvim_buf_get_name(opts.bufnr)
31-
vim.system(cmd, { cwd = cwd }, function(sc)
31+
vim.system(cmd, { cwd = cwd, env = opts.env }, function(sc)
3232
---@cast sc vim.SystemCompleted
3333
if sc.code == 0 then
3434
local summary = get_test_summary(sc.stdout or '')

lua/rustaceanvim/executors/quickfix.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ end
2020

2121
---@type rustaceanvim.Executor
2222
local M = {
23-
execute_command = function(command, args, cwd, _)
23+
execute_command = function(command, args, cwd, opts)
2424
-- open quickfix
2525
copen()
2626
-- go back to the previous window
@@ -32,7 +32,7 @@ local M = {
3232
local cmd = vim.list_extend({ command }, args)
3333
vim.system(
3434
cmd,
35-
cwd and { cwd = cwd } or {},
35+
{ cwd = cwd, env = opts.env },
3636
vim.schedule_wrap(function(sc)
3737
---@cast sc vim.SystemCompleted
3838
local data = ([[

lua/rustaceanvim/executors/termopen.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local latest_buf_id = nil
33

44
---@type rustaceanvim.Executor
55
local M = {
6-
execute_command = function(command, args, cwd, _)
6+
execute_command = function(command, args, cwd, opts)
77
local shell = require('rustaceanvim.shell')
88
local ui = require('rustaceanvim.ui')
99
local commands = {}
@@ -29,7 +29,7 @@ local M = {
2929
-- close the buffer when escape is pressed :)
3030
vim.keymap.set('n', '<Esc>', '<CMD>q<CR>', { buffer = latest_buf_id, noremap = true })
3131

32-
vim.fn.jobstart(full_command, { term = true })
32+
vim.fn.jobstart(full_command, { term = true, env = opts.env })
3333

3434
-- when the buffer is closed, set the latest buf id to nil else there are
3535
-- some edge cases with the id being sit but a buffer not being open

lua/rustaceanvim/executors/toggleterm.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---@type rustaceanvim.Executor
22
local M = {
3-
execute_command = function(command, args, cwd, _)
3+
execute_command = function(command, args, cwd, opts)
44
local ok, term = pcall(require, 'toggleterm.terminal')
55
if not ok then
66
vim.schedule(function()
@@ -13,6 +13,7 @@ local M = {
1313
term.Terminal
1414
:new({
1515
dir = cwd,
16+
env = opts.env,
1617
cmd = shell.make_command_from_args(command, args),
1718
close_on_exit = false,
1819
on_open = function(t)

lua/rustaceanvim/executors/vimux.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ local shell = require('rustaceanvim.shell')
22

33
---@type rustaceanvim.Executor
44
local M = {
5-
execute_command = function(command, args, cwd, _)
5+
execute_command = function(command, args, cwd, opts)
6+
local envs = ''
7+
for k, v in pairs(opts.env) do
8+
envs = envs .. k .. "='" .. v .. "' "
9+
end
610
local commands = {}
711
if cwd then
812
table.insert(commands, shell.make_cd_command(cwd))
913
end
1014
table.insert(commands, shell.make_command_from_args(command, args))
1115
local full_command = shell.chain_commands(commands)
12-
vim.fn.VimuxRunCommand(full_command)
16+
vim.fn.VimuxRunCommand(envs .. full_command)
1317
end,
1418
}
1519

lua/rustaceanvim/neotest/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ function NeotestAdapter.build_spec(run_args)
269269
type = pos.type,
270270
tree = tree,
271271
}
272-
local exe, args, cwd = require('rustaceanvim.runnables').get_command(runnable)
272+
local exe, args, cwd, env = require('rustaceanvim.runnables').get_command(runnable)
273273
if run_args.strategy == 'dap' then
274274
local dap = require('rustaceanvim.dap')
275275
overrides.sanitize_command_for_debugging(runnable.args.cargoArgs)
@@ -313,6 +313,7 @@ function NeotestAdapter.build_spec(run_args)
313313
command = vim.list_extend({ exe }, args),
314314
cwd = cwd,
315315
context = context,
316+
env = env,
316317
}
317318
return run_spec
318319
end

lua/rustaceanvim/runnables.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ end
2626
---@field cargoArgs string[]
2727
---@field cargoExtraArgs? string[]
2828
---@field executableArgs string[]
29+
---@field environment? table<string, string>
2930

3031
---@param option string
3132
---@return string
@@ -69,10 +70,12 @@ end
6970
---@return string executable
7071
---@return string[] args
7172
---@return string | nil dir
73+
---@return table<string, string> | nil env
7274
function M.get_command(runnable)
7375
local args = runnable.args
7476

7577
local dir = args.workspaceRoot
78+
local env = args.environment
7679

7780
local ret = vim.list_extend({}, args.cargoArgs or {})
7881
ret = vim.list_extend(ret, args.cargoExtraArgs or {})
@@ -86,14 +89,15 @@ function M.get_command(runnable)
8689
ret = overrides.try_nextest_transform(ret)
8790
end
8891

89-
return config.tools.cargo_override or 'cargo', ret, dir
92+
return config.tools.cargo_override or 'cargo', ret, dir, env
9093
end
9194

9295
---@param choice integer
9396
---@param runnables rustaceanvim.RARunnable[]
9497
---@return rustaceanvim.CargoCmd command build command
9598
---@return string[] args
9699
---@return string|nil dir
100+
---@return table<string, string> | nil env
97101
local function getCommand(choice, runnables)
98102
return M.get_command(runnables[choice])
99103
end
@@ -108,7 +112,7 @@ function M.run_command(choice, runnables)
108112

109113
local opts = config.tools
110114

111-
local command, args, cwd = getCommand(choice, runnables)
115+
local command, args, cwd, env = getCommand(choice, runnables)
112116
if not cwd then
113117
return
114118
end
@@ -117,10 +121,11 @@ function M.run_command(choice, runnables)
117121
local test_executor = vim.tbl_contains(args, '--all-targets') and opts.crate_test_executor or opts.test_executor
118122
test_executor.execute_command(command, args, cwd, {
119123
bufnr = vim.api.nvim_get_current_buf(),
124+
env = env,
120125
runnable = runnables[choice],
121126
})
122127
else
123-
opts.executor.execute_command(command, args, cwd)
128+
opts.executor.execute_command(command, args, cwd, { env = env })
124129
end
125130
end
126131

0 commit comments

Comments
 (0)