Open
Description
I’ve discovered that the TailwindCSS LSP picks its root_dir
from the first package I open which contains tailwind.config.ts
file —so when I jump into a different package in my monorepo, I lose all completions until I manually restart the server.
To work around this, I’ve hooked into BufEnter
/InsertEnter
and written a tiny utility that:
- Finds the nearest
tailwind.config.ts
for the current buffer - Compares it to the active LSP client’s
root_dir
- Stops & restarts
tailwindcss
LSP if the root_dir has changed
vim.api.nvim_create_autocmd({ "BufEnter", "InsertEnter" }, {
pattern = "*.tsx",
callback = require("utils.tailwind_lsp").restart,
})
-- utils/tailwind_lsp.lua
local M = {}
function M.restart()
local buf = vim.api.nvim_get_current_buf()
local clients = vim.lsp.get_clients({ bufnr = buf, name = "tailwindcss" })
local lspconfig_tailwind = require("lspconfig.configs.tailwindcss")
-- Get current file's path and detect new root
local current_file = vim.api.nvim_buf_get_name(buf)
local new_root = lspconfig_tailwind.default_config.root_dir(current_file)
-- Check if tailwindcss is not attached to the buffer
if #clients == 0 then
vim.cmd("LspStart tailwindcss")
return
end
local client = clients[1]
if client.config.root_dir == new_root then
return
end
client.stop()
vim.defer_fn(function()
vim.cmd("LspStart tailwindcss")
end, 100)
end
return M
It works, but feels hacky. Is there a cleaner way to make the TailwindCSS LSP automatically pick up each package’s config in a monorepo (e.g. by customizing root_dir in lspconfig)? I can’t hoist the config to the root because the tailwind config differs between packages.
Additional info:
Tailwind LSP: v0.14.16
Tailwind: v3
package manager: pnpm
Metadata
Metadata
Assignees
Labels
No labels