Skip to content

Commit

Permalink
fix: don't auto install servers when headless (#175)
Browse files Browse the repository at this point in the history
You should be able to run headless commands without mason-lspconfig starting installations in the background with
limited ability to wait until they're done.
  • Loading branch information
williamboman committed Mar 18, 2023
1 parent d1a76a5 commit 2b81103
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lua/mason-lspconfig/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local _ = require "mason-core.functional"
local log = require "mason-core.log"
local platform = require "mason-core.platform"

local M = {}

Expand All @@ -19,7 +20,7 @@ function M.setup(config)
log.error("Failed to set up lspconfig integration.", err)
end

if #settings.current.ensure_installed > 0 then
if not platform.is_headless and #settings.current.ensure_installed > 0 then
require "mason-lspconfig.ensure_installed"()
end

Expand Down
3 changes: 3 additions & 0 deletions lua/mason-lspconfig/lspconfig_hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ local memoized_set = _.memoize(_.set_of)

---@param server_name string
local function should_auto_install(server_name)
if platform.is_headless then
return false
end
local settings = require "mason-lspconfig.settings"

if settings.current.automatic_installation == true then
Expand Down
35 changes: 35 additions & 0 deletions tests/mason-lspconfig/setup_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local stub = require "luassert.stub"
local Pkg = require "mason-core.package"
local filetype_mappings = require "mason-lspconfig.mappings.filetype"
local mason_lspconfig = require "mason-lspconfig"
local platform = require "mason-core.platform"
local registry = require "mason-registry"
local server_mappings = require "mason-lspconfig.mappings.server"

Expand Down Expand Up @@ -51,6 +52,7 @@ describe("mason-lspconfig setup", function()
local fail_dummy = registry.get_package "fail_dummy"
spy.on(Pkg, "install")

platform.is_headless = false
mason_lspconfig.setup { ensure_installed = { "dummylsp@1.0.0", "fail_dummylsp" } }

assert.spy(Pkg.install).was_called(2)
Expand All @@ -63,11 +65,24 @@ describe("mason-lspconfig setup", function()
end)
)

it(
"should not install servers listed in ensure_installed when headless",
async_test(function()
spy.on(Pkg, "install")

platform.is_headless = true
mason_lspconfig.setup { ensure_installed = { "dummylsp@1.0.0", "fail_dummylsp" } }

assert.spy(Pkg.install).was_called(0)
end)
)

it(
"should notify when installing servers listed in ensure_installed",
async_test(function()
spy.on(vim, "notify")

platform.is_headless = false
mason_lspconfig.setup { ensure_installed = { "dummylsp", "fail_dummylsp" } }

assert
Expand Down Expand Up @@ -100,6 +115,7 @@ describe("mason-lspconfig setup", function()
spy.on(Pkg, "install")
spy.on(vim, "notify")

platform.is_headless = false
mason_lspconfig.setup { automatic_installation = true }
local lspconfig = require "lspconfig"
lspconfig.dummylsp.setup {}
Expand Down Expand Up @@ -139,6 +155,7 @@ describe("mason-lspconfig setup", function()
local dummy2 = registry.get_package "dummy2"
spy.on(Pkg, "install")

platform.is_headless = false
mason_lspconfig.setup { automatic_installation = true }
local lspconfig = require "lspconfig"
spy.on(lspconfig.dummylsp, "setup")
Expand All @@ -159,6 +176,23 @@ describe("mason-lspconfig setup", function()
end)
)

it(
"should not automatically install servers when headless",
async_test(function()
spy.on(Pkg, "install")

platform.is_headless = true
mason_lspconfig.setup { automatic_installation = true }
local lspconfig = require "lspconfig"
spy.on(lspconfig.dummylsp, "setup")
spy.on(lspconfig.dummy2lsp, "setup")
lspconfig.dummylsp.setup {}
lspconfig.dummy2lsp.setup {}

assert.spy(Pkg.install).was_called(0)
end)
)

it("should apply mason-lspconfig server configs", function()
stub(registry, "is_installed")
registry.is_installed.on_call_with("dummy").returns(true)
Expand Down Expand Up @@ -247,6 +281,7 @@ describe("mason-lspconfig setup_handlers", function()

it("should print warning when providing invalid server entries in ensure_installed", function()
spy.on(vim, "notify")
platform.is_headless = false
mason_lspconfig.setup {
ensure_installed = { "yamllint", "hadolint" },
}
Expand Down

0 comments on commit 2b81103

Please sign in to comment.