Opencode Lsp
OpenCode deeply integrates with the Language Server Protocol (LSP). Through LSP servers, it provides LLM with real-time feedback such as code diagnostics and error hints, helping AI more accurately understand and operate your codebase.
Simply put, an LSP server is the component that gives OpenCode the ability to "understand code," similar to the plugins in VS Code that provide code completion and error hints. When there are syntax errors or type mismatches in a file, the LSP server feeds this diagnostic information back to the LLM, enabling more accurate modifications.
* * *
## How It Works
OpenCode's LSP integration is fully automatic and requires no manual intervention. When OpenCode opens a file, it executes the following process in sequence:
Open File β βΌRead File Extension (e.g., .ts, .py, .go) β βΌMatch with All Enabled LSP Servers β βββ Match Succeeded and Server Running β Use Directly β βββ Match Succeeded but Server Not Running β Auto-start Corresponding LSP Server
The entire process completes silently in the background. Once started, the LSP server continuously provides diagnostic information for the current session.
* * *
## Built-in LSP Servers
OpenCode has built-in support for LSP servers for mainstream languages. **When the corresponding file extension is detected and requirements are met, the LSP server will automatically enable**, and in most cases, no configuration is needed.
| LSP Server | Supported File Extensions | Enable Requirements |
| --- | --- | --- |
| `astro` | `.astro` | Automatically installed for Astro projects |
| `bash` | `.sh`, `.bash`, `.zsh`, `.ksh` | Automatically installs bash-language-server |
| `clangd` | `.c`, `.cpp`, `.cc`, `.cxx`, `.c++`, `.h`, `.hpp`, `.hh`, `.hxx`, `.h++` | Automatically installed for C/C++ projects |
| `csharp` | `.cs` | Requires .NET SDK to be installed |
| `clojure-lsp` | `.clj`, `.cljs`, `.cljc`, `.edn` | Requires `clojure-lsp` command to be available |
| `dart` | `.dart` | Requires `dart` command to be available |
| `deno` | `.ts`, `.tsx`, `.js`, `.jsx`, `.mjs` | Requires `deno` command to be available, and `deno.json` or `deno.jsonc` exists in the project (auto-detected) |
| `elixir-ls` | `.ex`, `.exs` | Requires `elixir` command to be available |
| `eslint` | `.ts`, `.tsx`, `.js`, `.jsx`, `.mjs`, `.cjs`, `.mts`, `.cts`, `.vue` | Requires `eslint` dependency to be installed in the project |
| `fsharp` | `.fs`, `.fsi`, `.fsx`, `.fsscript` | Requires .NET SDK to be installed |
| `gleam` | `.gleam` | Requires `gleam` command to be available |
| `gopls` | `.go` | Requires `go` command to be available |
| `hls` | `.hs`, `.lhs` | Requires `haskell-language-server-wrapper` command to be available |
| `jdtls` | `.java` | Requires Java SDK to be installed (version 21 and above) |
| `julials` | `.jl` | Requires `julia` and `LanguageServer.jl` package to be installed |
| `kotlin-ls` | `.kt`, `.kts` | Automatically installed for Kotlin projects |
| `lua-ls` | `.lua` | Automatically installed for Lua projects |
| `nixd` | `.nix` | Requires `nixd` command to be available |
| `ocaml-lsp` | `.ml`, `.mli` | Requires `ocamllsp` command to be available |
| `oxlint` | `.ts`, `.tsx`, `.js`, `.jsx`, `.mjs`, `.cjs`, `.mts`, `.cts`, `.vue`, `.astro`, `.svelte` | Requires `oxlint` dependency to be installed in the project |
| `php intelephense` | `.php` | Automatically installed for PHP projects |
| `prisma` | `.prisma` | Requires `prisma` command to be available |
| `pyright` | `.py`, `.pyi` | Requires `pyright` dependency to be installed |
| `ruby-lsp` | `.rb`, `.rake`, `.gemspec`, `.ru` | Requires `ruby` and `gem` commands to be available |
| `rust` | `.rs` | Requires `rust-analyzer` command to be available |
| `sourcekit-lsp` | `.swift`, `.objc`, `.objcpp` | Requires Swift to be installed (via Xcode on macOS) |
| `svelte` | `.svelte` | Automatically installed for Svelte projects |
| `terraform` | `.tf`, `.tfvars` | Automatically installed from GitHub Releases |
| `tinymist` | `.typ`, `.typc` | Automatically installed from GitHub Releases |
| `typescript` | `.ts`, `.tsx`, `.js`, `.jsx`, `.mjs`, `.cjs`, `.mts`, `.cts` | Requires `typescript` dependency to be installed in the project |
| `vue` | `.vue` | Automatically installed for Vue projects |
| `yaml-ls` | `.yaml`, `.yml` | Automatically installs Red Hat yaml-language-server |
| `zls` | `.zig`, `.zon` | Requires `zig` command to be available |
> If you don't want OpenCode to automatically download LSP servers, you can set the environment variable `OPENCODE_DISABLE_LSP_DOWNLOAD` to `true` to disable the automatic download behavior. See the note at the end of this document for details.
* * *
## Configure LSP Servers
LSP-related configuration is written in the `lsp` field of `opencode.json`. In most cases, no configuration is needed as LSP will automatically enable; but when customization is needed, each LSP server supports the following configuration options:
| Property | Type | Description |
| --- | --- | --- |
| `disabled` | `boolean` | Set to `true` to disable that LSP server |
| `command` | `string[]` | Command to start the LSP server (in array form, first element is the executable filename, followed by arguments) |
| `extensions` | `string[]` | List of file extensions that this LSP server is responsible for handling |
| `env` | `object` | Environment variables to inject when starting the server; keys are variable names, values are variable values |
| `initialization` | `object` | Initialization options sent to the server during the LSP `initialize` handshake phase; content varies by server |
### 1. Set Environment Variables (env)
Use the `env` property to inject environment variables when starting the LSP server. Commonly used to enable debug logs or specify paths required by the runtime:
## Example
{
"$schema": "https://opencode.ai/config.json",
"lsp": {
"rust": {
"env": {
"RUST_LOG": "debug" // Enable debug level logging for rust-analyzer to facilitate troubleshooting LSP issues
}
}
}
}
### 2. Set Initialization Options (initialization)
`initialization` is used to pass server-specific configurations to the LSP server. These options are sent once during the LSP handshake (`initialize` request) phase and affect all subsequent behavior of that server:
## Example
{
"$schema": "https://opencode.ai/config.json",
"lsp": {
"typescript": {
"initialization": {
"preferences": {
"importModuleSpecifierPreference": "relative"
// Tell TypeScript LSP to prefer relative path imports (e.g., ./utils)
// over absolute paths (e.g., /src/utils) or path aliases configured in tsconfig.json
}
}
}
}
}
> Initialization options vary by LSP server. For example, the options for `typescript` are completely different from `gopls`. Please consult the official documentation of the corresponding LSP server before use to avoid passing invalid configurations.
* * *
## Disable LSP Servers
### 1. Disable All LSP Servers
If you don't need LSP functionality at all, you can set `lsp` to `false` to turn off all LSP servers at once:
## Example
{
"$schema": "https://opencode.ai/config.json",
"lsp": false // Globally disable all LSP servers, suitable for environments that don't need code diagnostics or have network/resource constraints
}
### 2. Disable Specific LSP Servers
If you only want to disable one particular LSP server while keeping others, set `disabled: true` for that server individually:
## Example
{
"$schema": "https://opencode.ai/config.json",
"lsp": {
YouTip