C# LSP 在使用 OmniSharp 初始化期间出现“消息头必须使用 ':' 分隔键和值”错误

问题描述 投票:0回答:1

我正在尝试使用 OmniSharp 在 C# 中构建一个玩具语言服务器协议 (LSP) 服务器,但我不断遇到以下错误:

Message header must separate key and value using ':'

当 JSON-RPC 消息格式不正确时,通常会发生此错误。然而,我假设所有 OmniSharp 对象的格式都是正确的,所以我对出了什么问题感到困惑。该错误在初始化后立即发生,因此后续处理程序(如 DidOpen、DidChange 等)都不会被触发。

namespace ModdingToolChain
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.AddConsole(); // Configure console logging
            });
            var logger = loggerFactory.CreateLogger<Program>();

            FileLogger.Initialize("C:\\Users\\Admin\\source\\repos\\ModdingLSP\\ModdingToolChain\\Logs\\debug.txt", "[Program]"); // For debugging
            FileLogger.Log("Initialized");

            logger.LogInformation("Starting LSP server...");

            try
            {
                var server = await LanguageServer.From(options =>
                    options
                        .WithInput(Console.OpenStandardInput())
                        .WithOutput(Console.OpenStandardOutput())
                        .WithHandler<TextDocumentHandler>()
                        .WithHandler<CompletionHandler>()
                        .OnInitialize((server, request, token) =>
                        {
                            logger.LogInformation("LSP Server Initialized with request: " + request);
                            if (request is InitializeParams initParams)
                            {
                                FileLogger.Log($"ProcessId: {initParams.ProcessId}");
                                FileLogger.Log($"ClientInfo: {initParams.ClientInfo}");
                                // Log other properties as needed
                            }

                            var response = new InitializeResult
                            {
                                Capabilities = new ServerCapabilities
                                {
                                    TextDocumentSync = TextDocumentSyncKind.Full,
                                    HoverProvider = true,
                                    CodeActionProvider = true,
                                },
                                ServerInfo = new ServerInfo
                                {
                                    Name = "ModdingLSP",
                                    Version = "1.0.0",
                                }
                            };

                            FileLogger.Log("Response: " + response);
                            return Task.FromResult(response);
                        })
                        .OnStarted((server, token) =>
                        {
                            FileLogger.Log("LSP Server Initialized");
                            logger.LogInformation("LSP Server Initialized");
                            return Task.CompletedTask;
                        })
                ).ConfigureAwait(false);

                await server.WaitForExit.ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                FileLogger.Log($"An error occurred while running the LSP server. {ex}");
                logger.LogError(ex, "An error occurred while running the LSP server.");
            }

            logger.LogInformation("LSP server stopping...");
        }
    }
}

我使用 Console.OpenStandardInput() 和 Console.OpenStandardOutput() 作为输入和输出流。我已经记录到位,这就是初始化期间调试日志中显示的内容:

2024-10-22 21:32:40 [Program] Initialized
2024-10-22 21:32:40 [Program] ProcessId: 36396
2024-10-22 21:32:40 [Program] ClientInfo: Visual Studio Code (1.94.1)
2024-10-22 21:32:40 [Program] Response: InitializeResult { Capabilities = OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities.ServerCapabilities, ServerInfo = ModdingLSP (1.0.0) }
2024-10-22 21:32:42 [Program] Initialized
2024-10-22 21:32:43 [Program] ProcessId: 36396
2024-10-22 21:32:43 [Program] ClientInfo: Visual Studio Code (1.94.1)
2024-10-22 21:32:43 [Program] Response: InitializeResult { Capabilities = OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities.ServerCapabilities, ServerInfo = ModdingLSP (1.0.0) }

它似乎已初始化,但随后失败并出现典型的 JSON-RPC 错误。主要问题是:我配置输入/输出流或处理 JSON-RPC 协议的方式是否有问题?

任何帮助或指示将不胜感激!

c# roslyn language-server-protocol omnisharp
1个回答
0
投票

跳出的一件事:看起来您正在尝试将控制台输入/输出用于 LSP RPC,并且您还尝试使用它来使用此位进行日志记录:

var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddConsole(); // Configure console logging
});

因此,每当您写入该记录器时,您都将写入与 RPC 流相同的流,这会损坏它。

(通常,使用 stdin/stdout 作为 LSP RPC 通道时要非常小心——有人很容易意外写入并损坏它。)

© www.soinside.com 2019 - 2024. All rights reserved.