nvim-lspconfig/lsp/tailwindcss.lua
Omar Elhawary 8caef47d1d
fix(tailwindcss): revert broken config detection #4376
Problem:
`find_tailwind_global_css` attempted to address #4204, where `experimental.configFile` was set using the return value of `vim.fs.find()`.
The language server rejected this with `Invalid experimental.configFile
configuration, not initializing` because `configFile` expects either a string or
a key-value record (object), not an array/list. This was a syntax issue, not
a detection issue.

Using the correct syntax for `configFile` in Lua should be
sufficient to address the original issue. Right now, `find_tailwind_global_css`
always runs for users who haven't explicitly set `configFile` — overriding the
LSP's native detection and **forcing anyone who wants to opt out to manually set
all entry-points by hand.**

Solution:
- Remove `find_tailwind_global_css` entirely and restores `configFile` to its
  default `nil` so the `tailwindcss` LSP handles project detection natively.
- Simplify `before_init` based on [this suggestion from the initial
  PR](https://github.com/neovim/nvim-lspconfig/pull/4222#discussion_r3018499628).

The following syntax worked for me while testing to explicitly set the
`configFile` based on the [official
docs](https://github.com/tailwindlabs/tailwindcss-intellisense#tailwindcssexperimentalconfigfile)
for single entry-point:

> [!NOTE]
> Single entry-point is resolved relative to the workspace root (`root_dir` — verify with `:checkhealth vim.lsp`)

```lua
vim.lsp.config('tailwindcss', {
  settings = {
    tailwindCSS = {
      experimental = {
        -- v3: config file
        configFile = 'tailwind.config.js',
        -- v4: CSS entry-point
        -- configFile = 'src/styles/app.css',
      },
    },
  },
})
```

For projects with multiple entry-points, or different projects, the following
syntax can be used for multiple entry-points:

> [!NOTE]
> Keys are relative to `root_dir` as above, but from my testing on macOS, absolute paths worked better

```lua
vim.lsp.config('tailwindcss', {
  settings = {
    tailwindCSS = {
      experimental = {
        configFile = {
          ['tailwind.config.js'] = '/Users/username/path/to/project-a/**',
          ['src/main.css'] = '/Users/username/path/to/project-b/**',
        },
      },
    },
  },
})
```

#### Project or Local Configuration

For project-specific settings without modifying your global Neovim config:

1. Enable in your Neovim config:
   ```lua
   vim.o.exrc = true
   ```
2. Create `.nvim.lua` in the project root:
   ```lua
   vim.lsp.config('tailwindcss', {
     settings = {
       tailwindCSS = {
         experimental = {
           configFile = 'tailwind.config.ts',
         },
       },
     },
   })
   ```
3. Open `.nvim.lua` and run `:trust` to allow the file, then restart Neovim.
4. Verify with `:checkhealth vim.lsp`.
2026-04-05 10:33:03 -04:00

142 lines
3.3 KiB
Lua

---@brief
--- https://github.com/tailwindlabs/tailwindcss-intellisense
---
--- Tailwind CSS Language Server can be installed via npm:
---
--- npm install -g @tailwindcss/language-server
---
--- To manually set the config file or CSS entry-point, see:
--- https://github.com/tailwindlabs/tailwindcss-intellisense#tailwindcssexperimentalconfigfile
local util = require('lspconfig.util')
---@type vim.lsp.Config
return {
cmd = { 'tailwindcss-language-server', '--stdio' },
-- filetypes copied and adjusted from tailwindcss-intellisense
filetypes = {
-- html
'aspnetcorerazor',
'astro',
'astro-markdown',
'blade',
'clojure',
'django-html',
'htmldjango',
'edge',
'eelixir', -- vim ft
'elixir',
'ejs',
'erb',
'eruby', -- vim ft
'gohtml',
'gohtmltmpl',
'haml',
'handlebars',
'hbs',
'html',
'htmlangular',
'html-eex',
'heex',
'jade',
'leaf',
'liquid',
'markdown',
'mdx',
'mustache',
'njk',
'nunjucks',
'php',
'razor',
'slim',
'twig',
-- css
'css',
'less',
'postcss',
'sass',
'scss',
'stylus',
'sugarss',
-- js
'javascript',
'javascriptreact',
'reason',
'rescript',
'typescript',
'typescriptreact',
-- mixed
'vue',
'svelte',
'templ',
},
capabilities = {
workspace = {
didChangeWatchedFiles = {
dynamicRegistration = true,
},
},
},
---@type lspconfig.settings.tailwindcss
settings = {
tailwindCSS = {
validate = true,
lint = {
cssConflict = 'warning',
invalidApply = 'error',
invalidScreen = 'error',
invalidVariant = 'error',
invalidConfigPath = 'error',
invalidTailwindDirective = 'error',
recommendedVariantOrder = 'warning',
},
classAttributes = {
'class',
'className',
'class:list',
'classList',
'ngClass',
},
includeLanguages = {
eelixir = 'html-eex',
elixir = 'phoenix-heex',
eruby = 'erb',
heex = 'phoenix-heex',
htmlangular = 'html',
templ = 'html',
},
},
},
before_init = function(_, config)
config.settings = vim.tbl_deep_extend('keep', config.settings, {
editor = { tabSize = vim.lsp.util.get_effective_tabstop() },
})
end,
workspace_required = true,
root_dir = function(bufnr, on_dir)
local root_files = {
-- Generic
'tailwind.config.js',
'tailwind.config.cjs',
'tailwind.config.mjs',
'tailwind.config.ts',
'postcss.config.js',
'postcss.config.cjs',
'postcss.config.mjs',
'postcss.config.ts',
-- Django
'theme/static_src/tailwind.config.js',
'theme/static_src/tailwind.config.cjs',
'theme/static_src/tailwind.config.mjs',
'theme/static_src/tailwind.config.ts',
'theme/static_src/postcss.config.js',
-- Fallback for tailwind v4, where tailwind.config.* is not required anymore
'.git',
}
local fname = vim.api.nvim_buf_get_name(bufnr)
root_files = util.insert_package_json(root_files, 'tailwindcss', fname)
root_files = util.root_markers_with_field(root_files, { 'mix.lock', 'Gemfile.lock' }, 'tailwind', fname)
on_dir(vim.fs.dirname(vim.fs.find(root_files, { path = fname, upward = true })[1]))
end,
}