forked from microsoft/qsharp-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
213 lines (192 loc) · 8.38 KB
/
Program.cs
File metadata and controls
213 lines (192 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using System.IO.Pipes;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.Loader;
using CommandLine;
using CommandLine.Text;
using Microsoft.Build.Locator;
namespace Microsoft.Quantum.QsLanguageServer
{
public class Server
{
public class Options
{
// Note: items in one set are mutually exclusive with items from other sets
protected const string ConnectionViaSocket = "connectionViaSocket";
protected const string ConnectionViaPipe = "connectionViaPipe";
[Option(
'l',
"log",
Required = false,
Default = null,
HelpText = "Path to log messages to.")]
public string? LogFile { get; set; }
[Option(
'p',
"port",
Required = true,
SetName = ConnectionViaSocket,
HelpText = "Port to use for TCP/IP connections.")]
public int Port { get; set; }
[Option(
'w',
"writer",
Required = true,
SetName = ConnectionViaPipe,
HelpText = "Named pipe to write to.")]
public string? WriterPipeName { get; set; }
[Option(
'r',
"reader",
Required = true,
SetName = ConnectionViaPipe,
HelpText = "Named pipe to read from.")]
public string? ReaderPipeName { get; set; }
}
public enum ReturnCode
{
SUCCESS = 0,
MISSING_ARGUMENTS = 1,
INVALID_ARGUMENTS = 2,
MSBUILD_UNINITIALIZED = 3,
CONNECTION_ERROR = 4,
UNEXPECTED_ERROR = 100
}
private static int LogAndExit(ReturnCode code, string? logFile = null, string? message = null)
{
var text = message ?? (
code == ReturnCode.SUCCESS ? "Exiting normally." :
code == ReturnCode.MISSING_ARGUMENTS ? "Missing command line options." :
code == ReturnCode.INVALID_ARGUMENTS ? "Invalid command line arguments. Use --help to see the list of options." :
code == ReturnCode.MSBUILD_UNINITIALIZED ? "Failed to initialize MsBuild." :
code == ReturnCode.CONNECTION_ERROR ? "Failed to connect." :
code == ReturnCode.UNEXPECTED_ERROR ? "Exiting abnormally." : "");
Log(text, logFile);
return (int)code;
}
public static string? Version =
typeof(Server).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? typeof(Server).Assembly.GetName().Version?.ToString();
public static int Main(string[] args)
{
// We need to set the current directory to the same directory of
// the LanguageServer executable so that it will pick the global.json file
// and force the MSBuildLocator to use .NET Core SDK 3.1
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
var parser = new Parser(parser => parser.HelpWriter = null); // we want our own custom format for the version info
var options = parser.ParseArguments<Options>(args);
return options.MapResult(
(Options opts) => Run(opts),
errs => errs.IsVersion()
? LogAndExit(ReturnCode.SUCCESS, message: Version)
: LogAndExit(ReturnCode.INVALID_ARGUMENTS, message: HelpText.AutoBuild(options)));
}
private static int Run(Options options)
{
if (options == null)
{
return LogAndExit(ReturnCode.MISSING_ARGUMENTS);
}
// In the case where we actually instantiate a server, we need to "configure" the design time build.
// This needs to be done before any MsBuild packages are loaded.
try
{
VisualStudioInstance vsi = MSBuildLocator.RegisterDefaults();
// We're using the installed version of the binaries to avoid a dependency between
// the .NET Core SDK version and NuGet. This is a workaround due to the issue below:
// https://github.com/microsoft/MSBuildLocator/issues/86
AssemblyLoadContext.Default.Resolving += (assemblyLoadContext, assemblyName) =>
{
string path = Path.Combine(vsi.MSBuildPath, assemblyName.Name + ".dll");
if (File.Exists(path))
{
return assemblyLoadContext.LoadFromAssemblyPath(path);
}
return null;
};
}
catch (Exception ex)
{
// Don't exit here, since exiting without establishing a connection will result in a cryptic failure of the extension.
// Instead, we proceed to create a server instance and establish the connection.
// Any errors can then be properly processed via the standard server-client communication as needed.
Log("[ERROR] MsBuildLocator could not register defaults.", options.LogFile);
Log(ex, options.LogFile);
}
QsLanguageServer server;
try
{
server = options.ReaderPipeName != null && options.WriterPipeName != null
? ConnectViaNamedPipe(options.WriterPipeName, options.ReaderPipeName, options.LogFile)
: ConnectViaSocket(port: options.Port, logFile: options.LogFile);
}
catch (Exception ex)
{
Log("[ERROR] Failed to launch server.", options.LogFile);
return LogAndExit(ReturnCode.CONNECTION_ERROR, options.LogFile, ex.ToString());
}
Log("Listening...", options.LogFile);
try
{
_ = server.CheckDotNetSdkVersionAsync();
server.WaitForShutdown();
}
catch (Exception ex)
{
Log("[ERROR] Unexpected error.", options.LogFile);
return LogAndExit(ReturnCode.UNEXPECTED_ERROR, options.LogFile, ex.ToString());
}
return server.ReadyForExit
? LogAndExit(ReturnCode.SUCCESS, options.LogFile)
: LogAndExit(ReturnCode.UNEXPECTED_ERROR, options.LogFile);
}
private static void Log(object msg, string? logFile = null)
{
if (logFile != null)
{
using var writer = new StreamWriter(logFile, append: true);
writer.WriteLine(msg);
}
else
{
Console.WriteLine(msg);
}
}
internal static QsLanguageServer ConnectViaNamedPipe(string writerName, string readerName, string? logFile = null)
{
Log($"Connecting via named pipe. {Environment.NewLine}ReaderPipe: \"{readerName}\" {Environment.NewLine}WriterPipe:\"{writerName}\"", logFile);
var writerPipe = new NamedPipeClientStream(writerName);
var readerPipe = new NamedPipeClientStream(readerName);
readerPipe.Connect(30000);
if (!readerPipe.IsConnected)
{
Log($"[ERROR] Connection attempted timed out.", logFile);
}
writerPipe.Connect(30000);
if (!writerPipe.IsConnected)
{
Log($"[ERROR] Connection attempted timed out.", logFile);
}
return new QsLanguageServer(writerPipe, readerPipe);
}
internal static QsLanguageServer ConnectViaSocket(string hostname = "localhost", int port = 8008, string? logFile = null)
{
Log($"Connecting via socket. {Environment.NewLine}Port number: {port}", logFile);
Stream? stream = null;
try
{
stream = new TcpClient(hostname, port).GetStream();
}
catch (Exception ex)
{
Log("[ERROR] Failed to get network stream.", logFile);
Log(ex.ToString(), logFile);
}
return new QsLanguageServer(stream, stream);
}
}
}