aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/process.lua
blob: bb40b25bbdb6e0131abe96ca44ef5cdf7d3f7070 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
local log = require "nvim-lsp-installer.log"
local Data = require "nvim-lsp-installer.data"
local platform = require "nvim-lsp-installer.platform"
local uv = vim.loop

local list_any = Data.list_any

local M = {}

local function connect_sink(pipe, sink)
    return function(err, data)
        if err then
            log.error("Unexpected error when reading pipe.", err)
        end
        if data ~= nil then
            sink(data)
        else
            pipe:read_stop()
            pipe:close()
        end
    end
end

-- We gather the root env immediately, primarily because of E5560.
-- Also, there's no particular reason we need to refresh the environment (yet).
local initial_environ = vim.fn.environ()

function M.extend_path(new_paths)
    local new_path_str = table.concat(new_paths, platform.path_sep)
    if initial_environ["PATH"] then
        return new_path_str .. platform.path_sep .. initial_environ["PATH"]
    end
    return new_path_str
end

function M.graft_env(env)
    local root_env = {}
    for key, val in pairs(initial_environ) do
        root_env[#root_env + 1] = key .. "=" .. val
    end
    for key, val in pairs(env) do
        root_env[#root_env + 1] = key .. "=" .. val
    end
    return root_env
end

local function sanitize_env_list(env_list)
    local sanitized_list = {}
    for _, env in ipairs(env_list) do
        local safe_envs = {
            "GO111MODULE",
            "GOBIN",
            "GOPATH",
            "PATH",
            "GEM_HOME",
            "GEM_PATH",
        }
        local is_safe_env = list_any(safe_envs, function(safe_env)
            return env:find(safe_env .. "=") == 1
        end)
        if is_safe_env then
            sanitized_list[#sanitized_list + 1] = env
        else
            local idx = env:find "="
            sanitized_list[#sanitized_list + 1] = env:sub(1, idx) .. "=<redacted>"
        end
    end
    return sanitized_list
end

function M.spawn(cmd, opts, callback)
    local stdin = uv.new_pipe(false)
    local stdout = uv.new_pipe(false)
    local stderr = uv.new_pipe(false)

    local stdio = { stdin, stdout, stderr }

    local spawn_opts = {
        env = opts.env,
        stdio = stdio,
        args = opts.args,
        cwd = opts.cwd,
        detached = false,
        hide = true,
    }

    log.lazy_debug(function()
        local sanitized_env = sanitize_env_list(opts.env or {})
        return "Spawning cmd=%s, spawn_opts=%s",
            cmd,
            {
                args = opts.args,
                cwd = opts.cwd,
                env = sanitized_env,
            }
    end)

    local handle, pid
    handle, pid = uv.spawn(cmd, spawn_opts, function(exit_code, signal)
        local successful = exit_code == 0 and signal == 0
        handle:close()
        if not stdin:is_closing() then
            stdin:close()
        end

        -- ensure all pipes are closed, for I am a qualified plumber
        local check = uv.new_check()
        check:start(function()
            for i = 1, #stdio do
                local pipe = stdio[i]
                if not pipe:is_closing() then
                    return
                end
            end
            check:stop()
            callback(successful)
        end)
    end)

    if handle == nil then
        log.error("Failed to spawn process", cmd, pid)
        opts.stdio_sink.stderr(("Failed to spawn process cmd=%s pid=%s\n"):format(cmd, pid))
        callback(false)
        return nil, nil
    end

    log.debug("Spawned with pid", pid)

    stdout:read_start(connect_sink(stdout, opts.stdio_sink.stdout))
    stderr:read_start(connect_sink(stderr, opts.stdio_sink.stderr))

    return handle, stdio
end

function M.chain(opts)
    local stack = {}
    return {
        run = function(cmd, args)
            stack[#stack + 1] = { cmd = cmd, args = args }
        end,
        spawn = function(callback)
            local function execute(idx)
                local item = stack[idx]
                M.spawn(
                    item.cmd,
                    vim.tbl_deep_extend("force", opts, {
                        args = item.args,
                    }),
                    function(successful)
                        if successful and stack[idx + 1] then
                            -- iterate
                            execute(idx + 1)
                        else
                            -- we done
                            callback(successful)
                        end
                    end
                )
            end

            execute(1)
        end,
    }
end

function M.empty_sink()
    local function noop() end
    return {
        stdout = noop,
        stderr = noop,
    }
end

function M.simple_sink()
    return {
        stdout = vim.schedule_wrap(print),
        stderr = vim.schedule_wrap(vim.api.nvim_err_writeln),
    }
end

function M.in_memory_sink()
    local buffers = { stdout = {}, stderr = {} }
    return {
        buffers = buffers,
        sink = {
            stdout = function(chunk)
                buffers.stdout[#buffers.stdout + 1] = chunk
            end,
            stderr = function(chunk)
                buffers.stderr[#buffers.stderr + 1] = chunk
            end,
        },
    }
end

-- this prob belongs elsewhere ¯\_(ツ)_/¯
function M.debounced(debounced_fn)
    local queued = false
    local last_arg = nil
    return function(a)
        last_arg = a
        if queued then
            return
        end
        queued = true
        vim.schedule(function()
            debounced_fn(last_arg)
            queued = false
            last_arg = nil
        end)
    end
end

return M