Skip to content

Commit

Permalink
Add better error message when cadl-server is not found in Visual Studio
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin committed Jun 7, 2022
1 parent 3fd2fbe commit d313663
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 59 deletions.
135 changes: 76 additions & 59 deletions packages/cadl-vs/src/VSExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.VisualStudio.Utilities;
using Task = System.Threading.Tasks.Task;
using System.Linq;
using System.ComponentModel;

namespace Microsoft.Cadl.VisualStudio
{
Expand Down Expand Up @@ -82,20 +83,32 @@ public LanguageClient([Import] IVsFolderWorkspaceService workspaceService)
};

#if DEBUG
// Use local build of cadl-server in development (lauched from F5 in VS)
if (InDevelopmentMode()) {
// --nolazy isn't supported by NODE_OPTIONS so we pass these via CLI instead
info.Environment.Remove("NODE_OPTIONS");
}
// Use local build of cadl-server in development (lauched from F5 in VS)
if (InDevelopmentMode())
{
// --nolazy isn't supported by NODE_OPTIONS so we pass these via CLI instead
info.Environment.Remove("NODE_OPTIONS");
}
#endif
try
{
var process = Process.Start(info);
process.BeginErrorReadLine();
process.ErrorDataReceived += (_, e) => LogStderrMessage(e.Data);

var process = Process.Start(info);
process.BeginErrorReadLine();
process.ErrorDataReceived += (_, e) => LogStderrMessage(e.Data);
return new Connection(
process.StandardOutput.BaseStream,
process.StandardInput.BaseStream);
}
catch (Win32Exception e)
{
if (e.NativeErrorCode == Win32ErrorCodes.ERROR_FILE_NOT_FOUND)
{
throw new CadlServerNotFound(info.FileName);
}
throw e;
}

return new Connection(
process.StandardOutput.BaseStream,
process.StandardInput.BaseStream);
}

public async Task OnLoadedAsync()
Expand All @@ -108,10 +121,12 @@ public async Task OnLoadedAsync()
}

#if VS2019
public Task OnServerInitializeFailedAsync(Exception e) {
Debug.Fail("Failed to initialize cadl-server:\r\n\r\n" + e);
return Task.CompletedTask;
}
public Task OnServerInitializeFailedAsync(Exception e)
{
var message = e is CadlUserErrorException ? e.Message : e.ToString();
Debug.Fail("Failed to initialize cadl-server:\r\n\r\n" + message);
return Task.CompletedTask;
}
#endif

#if VS2022
Expand Down Expand Up @@ -143,56 +158,58 @@ private void LogStderrMessage(string? message)
}

#if DEBUG
private static bool InDevelopmentMode() {
return string.Equals(
Environment.GetEnvironmentVariable("CADL_DEVELOPMENT_MODE"),
"true",
StringComparison.OrdinalIgnoreCase);
}
private static bool InDevelopmentMode()
{
return string.Equals(
Environment.GetEnvironmentVariable("CADL_DEVELOPMENT_MODE"),
"true",
StringComparison.OrdinalIgnoreCase);
}

private static string GetDevelopmentCadlServerPath() {
// Even when debugging, we get deployed to an extension folder outside the
// source tree, so we stash the source directory in a file in debug builds
// so we can use it to run cadl-server against the live developer build in
// the source tree.
var thisDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var srcDir = File.ReadAllText(Path.Combine(thisDir, "DebugSourceDirectory.txt")).Trim();
return Path.GetFullPath(Path.Combine(srcDir, "../compiler/cmd/cadl-server.js"));
}
private static string GetDevelopmentCadlServerPath()
{
// Even when debugging, we get deployed to an extension folder outside the
// source tree, so we stash the source directory in a file in debug builds
// so we can use it to run cadl-server against the live developer build in
// the source tree.
var thisDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var srcDir = File.ReadAllText(Path.Combine(thisDir, "DebugSourceDirectory.txt")).Trim();
return Path.GetFullPath(Path.Combine(srcDir, "../compiler/cmd/cadl-server.js"));
}
#endif

private (string, string[]) resolveCadlServer(IWorkspaceSettings? settings)
{
var args = new string[] { "--stdio" };

#if DEBUG
// Use local build of cadl-server in development (lauched from F5 in VS)
if (InDevelopmentMode()) {
var options = Environment.GetEnvironmentVariable("CADL_SERVER_NODE_OPTIONS");
var module = GetDevelopmentCadlServerPath();
return ("node.exe", new string[] { module, options }.Concat(args).ToArray());
}
#endif

var serverPath = settings?.Property<string>("cadl.cadl-server.path");
if (serverPath == null)
{
return ("cadl-server.cmd", args);
}

if (!serverPath.EndsWith(".js"))
{
if (File.Exists(serverPath))
{
var command = serverPath.EndsWith(".cmd") ? serverPath : $"${serverPath}.cmd";
return (command, args);
}
else
{
serverPath = Path.Combine(serverPath, "cmd/cadl-server.js");
}
}
return ("node.exe", new string[] { serverPath }.Concat(args).ToArray());
return ("foo.cmd", args);
//#if DEBUG
// // Use local build of cadl-server in development (lauched from F5 in VS)
// if (InDevelopmentMode()) {
// var options = Environment.GetEnvironmentVariable("CADL_SERVER_NODE_OPTIONS");
// var module = GetDevelopmentCadlServerPath();
// return ("node.exe", new string[] { module, options }.Concat(args).ToArray());
// }
//#endif

// var serverPath = settings?.Property<string>("cadl.cadl-server.path");
// if (serverPath == null)
// {
// return ("cadl-server.cmd", args);
// }

// if (!serverPath.EndsWith(".js"))
// {
// if (File.Exists(serverPath))
// {
// var command = serverPath.EndsWith(".cmd") ? serverPath : $"${serverPath}.cmd";
// return (command, args);
// }
// else
// {
// serverPath = Path.Combine(serverPath, "cmd/cadl-server.js");
// }
// }
// return ("node.exe", new string[] { serverPath }.Concat(args).ToArray());

}
}
Expand Down
1 change: 1 addition & 0 deletions packages/cadl-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async function restartCadlServer(): Promise<void> {
async function resolveCadlServer(context: ExtensionContext): Promise<Executable> {
const nodeOptions = process.env.CADL_SERVER_NODE_OPTIONS;
const args = ["--stdio"];
return { command: "foo.cmd", args };
// In development mode (F5 launch from source), resolve to locally built server.js.
if (process.env.CADL_DEVELOPMENT_MODE) {
const script = context.asAbsolutePath("../compiler/dist/server/server.js");
Expand Down

0 comments on commit d313663

Please sign in to comment.