Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for choosing stdio as the LSP communication channel #76437

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public static Task<ExportProvider> CreateExportProviderAsync(
RazorSourceGenerator: null,
RazorDesignTimePath: null,
ExtensionLogDirectory: string.Empty,
ServerPipeName: null);
ServerPipeName: null,
UseStdIo: false);
var extensionManager = ExtensionAssemblyManager.Create(serverConfiguration, loggerFactory);
assemblyLoader = new CustomExportAssemblyLoader(extensionManager, loggerFactory);
return ExportProviderBuilder.CreateExportProviderAsync(extensionManager, assemblyLoader, devKitDependencyPath, cacheDirectory, loggerFactory);
Expand Down
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
HarleyRossetto marked this conversation as resolved.
Show resolved Hide resolved
if (serverConfiguration.UseStdIo)
{
if (serverConfiguration.ServerPipeName is not null)
{
throw new Exception("Server cannot be started with both --stdio and --pipe options.");
HarleyRossetto marked this conversation as resolved.
Show resolved Hide resolved
}

// Redirect Console.Out to try prevent the standard output stream from being corrupted.
HarleyRossetto marked this conversation as resolved.
Show resolved Hide resolved
Console.SetOut(new StreamWriter(Console.OpenStandardError()));
}

// Before we initalize the LSP server we can't send LSP log messages.
HarleyRossetto marked this conversation as resolved.
Show resolved Hide resolved
// Create a console logger as a fallback to use before the LSP server starts.
using var loggerFactory = LoggerFactory.Create(builder =>
{
Expand Down Expand Up @@ -125,23 +137,32 @@ static async Task RunAsync(ServerConfiguration serverConfiguration, Cancellation

var languageServerLogger = loggerFactory.CreateLogger(nameof(LanguageServerHost));

var (clientPipeName, serverPipeName) = serverConfiguration.ServerPipeName is null
? CreateNewPipeNames()
: (serverConfiguration.ServerPipeName, serverConfiguration.ServerPipeName);
LanguageServerHost? server = null;
if (serverConfiguration.UseStdIo)
{
server = new LanguageServerHost(Console.OpenStandardInput(), Console.OpenStandardOutput(), exportProvider, languageServerLogger, typeRefResolver);
}
else
{
var (clientPipeName, serverPipeName) = serverConfiguration.ServerPipeName is null
? CreateNewPipeNames()
: (serverConfiguration.ServerPipeName, serverConfiguration.ServerPipeName);

var pipeServer = new NamedPipeServerStream(serverPipeName,
PipeDirection.InOut,
maxNumberOfServerInstances: 1,
PipeTransmissionMode.Byte,
PipeOptions.CurrentUserOnly | PipeOptions.Asynchronous);
var pipeServer = new NamedPipeServerStream(serverPipeName,
PipeDirection.InOut,
maxNumberOfServerInstances: 1,
PipeTransmissionMode.Byte,
PipeOptions.CurrentUserOnly | PipeOptions.Asynchronous);

// Send the named pipe connection info to the client
Console.WriteLine(JsonSerializer.Serialize(new NamedPipeInformation(clientPipeName)));
// Send the named pipe connection info to the client
Console.WriteLine(JsonSerializer.Serialize(new NamedPipeInformation(clientPipeName)));

// Wait for connection from client
await pipeServer.WaitForConnectionAsync(cancellationToken);
// Wait for connection from client
await pipeServer.WaitForConnectionAsync(cancellationToken);

server = new LanguageServerHost(pipeServer, pipeServer, exportProvider, languageServerLogger, typeRefResolver);
}

var server = new LanguageServerHost(pipeServer, pipeServer, exportProvider, languageServerLogger, typeRefResolver);
server.Start();

logger.LogInformation("Language server initialized");
Expand Down Expand Up @@ -230,7 +251,15 @@ static CliRootCommand CreateCommandLineParser()
var serverPipeNameOption = new CliOption<string?>("--pipe")
{
Description = "The name of the pipe the server will connect to.",
Required = false
};

var useStdIoOption = new CliOption<bool>("--stdio")
{
Description = "Use stdio for communication with the client.",
Required = false,
DefaultValueFactory = _ => false,

};

var rootCommand = new CliRootCommand()
Expand All @@ -246,7 +275,8 @@ static CliRootCommand CreateCommandLineParser()
razorSourceGeneratorOption,
razorDesignTimePathOption,
extensionLogDirectoryOption,
serverPipeNameOption
serverPipeNameOption,
useStdIoOption
};
rootCommand.SetAction((parseResult, cancellationToken) =>
{
Expand All @@ -261,6 +291,7 @@ static CliRootCommand CreateCommandLineParser()
var razorDesignTimePath = parseResult.GetValue(razorDesignTimePathOption);
var extensionLogDirectory = parseResult.GetValue(extensionLogDirectoryOption)!;
var serverPipeName = parseResult.GetValue(serverPipeNameOption);
var useStdIo = parseResult.GetValue(useStdIoOption);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add validation that --stdio and --pipe are not both passed together. Logging an error and returning a non-zero exit code would be appropriate.


var serverConfiguration = new ServerConfiguration(
LaunchDebugger: launchDebugger,
Expand All @@ -273,6 +304,7 @@ static CliRootCommand CreateCommandLineParser()
RazorSourceGenerator: razorSourceGenerator,
RazorDesignTimePath: razorDesignTimePath,
ServerPipeName: serverPipeName,
UseStdIo: useStdIo,
ExtensionLogDirectory: extensionLogDirectory);

return RunAsync(serverConfiguration, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal record class ServerConfiguration(
string? RazorSourceGenerator,
string? RazorDesignTimePath,
string? ServerPipeName,
bool UseStdIo,
string ExtensionLogDirectory);

internal class LogConfiguration
Expand Down
Loading