Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCode LSP Server Configuration

OpenCode LSP Server Configuration

OpenCode is deeply integrated with the Language Server Protocol (LSP, Language Server Protocol), providing real-time feedback such as code diagnostics and error prompts to the LLM through LSP servers, helping AI understand and operate your codebase more accurately.

Simply put, an LSP server is the component that gives OpenCode the ability to "understand code", similar to plugins in VS Code that provide code completion and error prompts. When syntax errors or type mismatches occur 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 performs the following steps in sequence:


打开文件
  │
  ▼
读取文件扩展名(如 .ts、.py、.go)
  │
  ▼
与所有已启用的 LSP 服务器进行匹配
  │
  ├── 匹配成功且服务器已运行 → 直接使用
  │
  └── 匹配成功但服务器未运行 → 自动启动对应 LSP 服务器

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 LSP server support for mainstream languages;when the corresponding file extension is detected and the relevant requirements are met, the LSP server is automatically enabled, and in most cases no configuration is required.

LSP Server Supported file extensions Enablement requirement
astro .astro Auto-installed for Astro projects
bash .sh, .bash, .zsh, .ksh Automatically install bash-language-server
clangd .c, .cpp, .cc, .cxx, .c++, .h, .hpp, .hh, .hxx, .h++ Auto-installed for C/C++ projects
csharp .cs Requires .NET SDK to be installed
clojure-lsp .clj, .cljs, .cljc, .edn Needed clojure-lsp Command available
dart .dart Needed dart Command available
deno .ts, .tsx, .js, .jsx, .mjs Needed deno Command available, and present in project deno.json or deno.jsonc(auto-detected)
elixir-ls .ex, .exs Needed elixir Command available
eslint .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts, .vue Must be installed in the project eslint Depends on
fsharp .fs, .fsi, .fsx, .fsscript Requires .NET SDK to be installed
gleam .gleam Needed gleam Command available
gopls .go Needed go Command available
hls .hs, .lhs Needed haskell-language-server-wrapper Command available
jdtls .java Requires Java SDK (version 21 or above) to be installed
julials .jl Installation required julia and LanguageServer.jl package
kotlin-ls .kt, .kts Auto-installed for Kotlin projects
lua-ls .lua Auto-installed for Lua projects
nixd .nix Needed nixd Command available
ocaml-lsp .ml, .mli Needed ocamllsp Command available
oxlint .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts, .vue, .astro, .svelte Must be installed in the project oxlint Depends on
php intelephense .php Auto-installed for PHP projects
prisma .prisma Needed prisma Command available
pyright .py, .pyi Requires prior installation pyright Depends on
ruby-lsp .rb, .rake, .gemspec, .ru Needed ruby and gem Command available
rust .rs Needed rust-analyzer Command available
sourcekit-lsp .swift, .objc, .objcpp Requires Swift to be installed (on macOS via Xcode)
svelte .svelte Auto-installed for Svelte projects
terraform .tf, .tfvars Auto-installed from GitHub Releases
tinymist .typ, .typc Auto-installed from GitHub Releases
typescript .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts Must be installed in the project typescript Depends on
vue .vue Auto-installed for Vue projects
yaml-ls .yaml, .yml Automatically install Red Hat yaml-language-server
zls .zig, .zon Needed zig Command 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 automatic downloading; see the explanation at the end of this document.


Configuring LSP servers

LSP-related configurations are written under the opencode.json 's lsp field. In most cases no configuration is needed and LSP is enabled automatically; but when customization is required, each LSP server supports the following configuration items:

property Type Description
disabled boolean to true Can disable this LSP server
command string[] Command to start the LSP server (as an array; the first element is the executable name, the rest are arguments)
extensions string[] List of file extensions handled by this LSP server
env object Environment variables injected when starting the server; the key is the variable name and the value is the variable value
initialization object Initialization options sent to the server during the LSP initialize handshake phase; the content varies by server

1. Setting Environment Variables (env)

Use env The property injects environment variables when starting the LSP server. Commonly used to enable debug logging 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 help troubleshoot LSP issues

      }

    }

  }

}

2. Setting Initialization Options (initialization)

initialization Used to pass server-specific configuration 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 the TypeScript LSP to prefer relative path imports (such as ./utils)

          // instead of absolute paths (such as /src/utils) or path aliases configured in tsconfig.json

        }

      }

    }

  }

}

Initialization options vary by LSP server. For example, typescript the options of one server are completely different from gopls those of another; consult the official documentation of the corresponding LSP server before use to avoid passing invalid configuration.


Disabling LSP Servers

1. Disable All LSP Servers

If you don't need LSP functionality at all, you can set the lsp entirely to false, shutting down 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 limited network/resources

}

2. Disable a Specific LSP Server

If you only want to disable one LSP server while keeping the others, set it individually for that server: disabled: true:

Example

{

  "$schema": "https://opencode.ai/config.json",

  "lsp": {

    "typescript": {

      "disabled": true // disable only the TypeScript LSP; LSP servers for other languages are unaffected

    }

  }

}


Adding a Custom LSP Server

If the language you use is not in the built-in support list, you can manually add any LSP server by specifying command and extensions , as long as the server follows the standard LSP protocol and supports --stdio mode (communicating via standard input/output):

Example

{

  "$schema": "https://opencode.ai/config.json",

  "lsp": {

    "custom-lsp": { // custom key name, can be named freely, used to identify the server in the configuration

      "command": ["custom-lsp-server", "--stdio"], // startup command: the first element is the executable name,

                                                     // the remaining elements are startup arguments passed to it

                                                     // --stdio means communicating with OpenCode via standard input/output

      "extensions": [".custom"] // specify which file extensions this LSP server handles

    }

  }

}

You can also configure environment variables and initialization options for a custom LSP server, written exactly the same way as for built-in servers:

Example

{

  "$schema": "https://opencode.ai/config.json",

  "lsp": {

    "my-lang-server": {

      "command": ["my-lang-server", "--stdio", "--log-level", "warn"],

      "extensions": [".mylang", ".ml2"], // supports binding multiple file extensions at once

      "env": {

        "MY_LSP_HOME": "/usr/local/my-lang" // inject the required environment variable into the server process

      },

      "initialization": {

        "formatOnSave": true // pass an initialization option defined in the server's documentation

      }

    }

  }

}


Additional Notes

PHP Intelephense License

PHP Intelephense offers a free version and a paid premium version. The premium version is unlocked with a license key and includes enhanced features such as import sorting, code folding, and call hierarchy viewing. If you have purchased a license, simply write the key into a text file at the following path to activate it automatically, with no extra configuration:

Operating system License File Path
macOS / Linux $HOME/intelephense/license.txt
Windows %USERPROFILE%/intelephense/license.txt

The license file should contain only the license key itself, with no other content (including extra spaces or newlines), otherwise activation may fail.

Disabling Automatic Downloads

By default, OpenCode automatically downloads the required LSP server from the network when it detects the corresponding file extension. If you are in a network-restricted environment, or want to manage LSP tools entirely manually, you can disable automatic downloads by setting the following environment variable:


# Linux / macOS: Temporarily disable (valid only for the current terminal session)
export OPENCODE_DISABLE_LSP_DOWNLOAD=true

# Linux / macOS: Permanently disable (write to shell configuration file)
echo 'export OPENCODE_DISABLE_LSP_DOWNLOAD=true' >> ~/.bashrc
source ~/.bashrc

# Windows (PowerShell: current session only)
$env:OPENCODE_DISABLE_LSP_DOWNLOAD = "true"

Once disabled, OpenCode will no longer automatically download any LSP servers, but LSP tools already pre-installed on the system can still be detected and used normally.