Skip to content

Commit

Permalink
redirect Console.Out to stderr
Browse files Browse the repository at this point in the history
Redirecting Console.Out to use the standard error stream to try reduce
the chances of stdout being corrupted by non-rpc content.
  • Loading branch information
HarleyRossetto committed Jan 8, 2025
1 parent 88c9dfe commit e481713
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@

static async Task RunAsync(ServerConfiguration serverConfiguration, CancellationToken cancellationToken)
{
// Before we initialize the LSP server we can't send LSP log messages.
// Redirect Console.Out when using stdio to try prevent corruption of the LSP output
if (serverConfiguration.UseStdIo)
{
if (serverConfiguration.ServerPipeName is not null)
{
throw new Exception("Server cannot be started with both --stdio and --pipe options.");
}

// Redirect Console.Out to try prevent the standard output stream from being corrupted.
Console.SetOut(new StreamWriter(Console.OpenStandardError()));
}

// Before we initalize the LSP server we can't send LSP log messages.
// Create a console logger as a fallback to use before the LSP server starts.
using var loggerFactory = LoggerFactory.Create(builder =>
{
Expand Down Expand Up @@ -246,7 +258,8 @@ static CliRootCommand CreateCommandLineParser()
{
Description = "Use stdio for communication with the client.",
Required = false,
DefaultValueFactory = _ => false
DefaultValueFactory = _ => false,

};

var rootCommand = new CliRootCommand()
Expand Down

0 comments on commit e481713

Please sign in to comment.