Skip to content

Commit

Permalink
fix: Show an actionable error message on SDK mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Feb 10, 2023
1 parent 9851312 commit ce24b5a
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Uno.Extensions;
using Uno.UI.RemoteControl.Server.Processors.Helpers;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

namespace Uno.UI.RemoteControl.Host.HotReload.MetadataUpdates
{
Expand Down Expand Up @@ -71,6 +72,17 @@ public static void InitializeRoslyn()

MSBuildBasePath = BuildMSBuildPath();

var version = GetDotnetVersion();
if (version.Major != typeof(object).Assembly.GetName().Version?.Major)
{
if (typeof(CompilationWorkspaceProvider).Log().IsEnabled(LogLevel.Error))
{
typeof(CompilationWorkspaceProvider).Log().LogError($"Unable to start the Remote Control server because the application's TargetFramework version does not match the default runtime. Change the TargetFramework version to match net{version.Major}.0 in your project file.");
}

throw new InvalidOperationException($"Project TargetFramework version mismatch");
}

Environment.SetEnvironmentVariable("MSBuildSDKsPath", Path.Combine(MSBuildBasePath, "Sdks"));

var MSBuildExists = File.Exists(Path.Combine(MSBuildBasePath, "Microsoft.Build.dll"));
Expand All @@ -81,6 +93,23 @@ public static void InitializeRoslyn()
}
}

private static Version GetDotnetVersion()
{
var result = ProcessHelper.RunProcess("dotnet.exe", "--version");

if (result.exitCode == 0)
{
var reader = new StringReader(result.output);

if (Version.TryParse(reader.ReadLine()?.Split('-').FirstOrDefault(), out var version))
{
return version;
}
}

throw new InvalidOperationException("Failed to read dotnet version");
}

private static string BuildMSBuildPath()
{
var result = ProcessHelper.RunProcess("dotnet.exe", "--info");
Expand Down

0 comments on commit ce24b5a

Please sign in to comment.