[BUG] LSP: Roslyn language server crashes with BadRpcHeaderException on Windows (\r\n header issue)

Responsive Ad Header

Question

Grade: Education Subject: anthropics claude-code
[BUG] LSP: Roslyn language server crashes with BadRpcHeaderException on Windows (\r\n header issue)
Asked by:
99 Viewed 99 Answers
Responsive Ad After Question

Answer (99)

Best Answer
(3610)

Bug Description

The LSP tool fails with [Tool result missing due to internal error] when using roslyn-language-server (Microsoft's Roslyn LSP) on Windows. The root cause is a BadRpcHeaderException — the LSP client appears to send headers with \n line endings instead of the \r\n required by the LSP/JSON-RPC specification.

Steps to Reproduce

  1. Install the Roslyn language server as a .NET global tool:

    dotnet tool install --global Microsoft.CodeAnalysis.LanguageServer
    
  2. Configure .lsp.json in the project root:

    {
      "csharp": {
        "command": "roslyn-language-server.cmd",
        "args": ["--stdio", "--autoLoadProjects", "--logLevel", "Information"],
        "transport": "stdio",
        "extensionToLanguage": {
          ".cs": "csharp",
          ".csx": "csharp"
        },
        "initializationOptions": {},
        "settings": {},
        "startupTimeout": 120000
      }
    }
    
  3. Use any LSP operation on a .cs file:

    LSP documentSymbol / hover / goToDefinition → [Tool result missing due to internal error]
    

Root Cause

Manual testing confirms the server starts but crashes immediately on the first message:

Content-Length: 116

{"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"[Program] Language server initialized"}}
Unhandled exception. StreamJsonRpc.BadRpcHeaderException: Header does not end with expected \r\n character sequence: ...
   at StreamJsonRpc.HeaderDelimitedMessageHandler.ReadHeadersAsync(CancellationToken cancellationToken)
   at StreamJsonRpc.HeaderDelimitedMessageHandler.ReadCoreAsync(CancellationToken cancellationToken)

The LSP specification requires \r\n as header delimiters. The StreamJsonRpc library used by Roslyn enforces this strictly.

Environment

  • OS: Windows 11 Enterprise (10.0.22631)
  • Claude Code: Latest (via Anthropic API)
  • Language Server: roslyn-language-server v5.5.0 (Microsoft.CodeAnalysis.LanguageServer)
  • Shell: bash on Windows

Expected Behavior

LSP operations (hover, goToDefinition, documentSymbol, etc.) should work with the Roslyn language server.

Actual Behavior

All LSP operations return [Tool result missing due to internal error].

Related Issues

  • #16360 — Missing protocol handlers with csharp-ls (different server, different root cause)
  • #15914 — .cmd extension not found on Windows (different root cause — server discovery vs. protocol)

Suggested Fix

Ensure the LSP client sends \r\n line endings in JSON-RPC headers per the LSP specification §Header Part. This is likely a single-line fix in the header serialization code.

(2722)

Answer:

The issue you're encountering is related to the Roslyn language server crashing on Windows due to a BadRpcHeaderException. This exception is caused by the LSP client sending headers with \n line endings instead of the required \r\n by the LSP/JSON-RPC specification.

Steps to Reproduce:

  1. Install the Roslyn language server as a .NET global tool:
    dotnet tool install --global Microsoft.CodeAnalysis.LanguageServer
    
  2. Configure .lsp.json in the project root:
    {
      "csharp": {
        "command": "roslyn-language-server.cmd",
        "args": [ "--stdio", "--autoLoadProjects", "--logLevel", "Information" ],
        "transport": "stdio",
        "extensionToLanguage": {
          ".cs": "csharp",
          ".csx": "csharp"
        },
        "initializationOptions": {},
        "settings": {},
        "startupTimeout": 120000
      }
    }
    
  3. Use any LSP operation on a .cs file:
    LSP documentSymbol / hover / goToDefinition → [Tool result missing due to internal error]
    

Root Cause:

The root cause is the client sending \n line endings in JSON-RPC headers instead of the required \r\n. The LSP specification mandates \r\n as header delimiters.

Expected Behavior:

LSP operations (hover, goToDefinition, documentSymbol, etc.) should work with the Roslyn language server.

Suggested Fix:

To resolve this issue, the LSP client needs to ensure it sends \r\n line endings in JSON-RPC headers. This is likely a single-line fix in the header serialization code.

Related Issues:

  • #16360 - Missing protocol handlers with csharp-ls (different server, different root cause).
  • #15914 - .cmd extension not found on Windows (different root cause — server discovery vs. protocol).

Environment:

  • OS: Windows 11 Enterprise (10.0.22631)
  • Claude Code: Latest (via Anthropic API)
  • Language Server: roslyn-language-server v5.5.0 (Microsoft.CodeAnalysis.LanguageServer)
  • Shell: bash on Windows