Skip to content

Commit

Permalink
Don't use VS 2017 MSBuild if it is RTM and display a helpful message …
Browse files Browse the repository at this point in the history
…for users

Fixes OmniSharp#1014
  • Loading branch information
DustinCampbell committed Nov 9, 2017
1 parent c01e43b commit 5cacd71
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
52 changes: 41 additions & 11 deletions src/OmniSharp.Host/CompositionHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,13 @@ public CompositionHost Build()
var fileSystemWatcher = new ManualFileSystemWatcher();
var metadataHelper = new MetadataHelper(assemblyLoader);

var logger = loggerFactory.CreateLogger<CompositionHostBuilder>();

// We must register an MSBuild instance before composing MEF to ensure that
// our AssemblyResolve event is hooked up first.
var msbuildLocator = _serviceProvider.GetRequiredService<IMSBuildLocator>();
var instances = msbuildLocator.GetInstances();
var instance = instances.FirstOrDefault();
if (instance != null)
{
msbuildLocator.RegisterInstance(instance);
}
else
{
var logger = loggerFactory.CreateLogger<CompositionHostBuilder>();
logger.LogError("Could not locate MSBuild instance to register with OmniSharp");
}

RegisterMSBuildInstance(msbuildLocator, logger);

config = config
.WithProvider(MefValueProvider.From(_serviceProvider))
Expand Down Expand Up @@ -94,6 +87,43 @@ public CompositionHost Build()
return config.CreateContainer();
}

private static void RegisterMSBuildInstance(IMSBuildLocator msbuildLocator, ILogger logger)
{
MSBuildInstance instanceToRegister = null;
var invalidVSFound = false;

foreach (var instance in msbuildLocator.GetInstances())
{
if (instance.IsInvalidVisualStudio())
{
invalidVSFound = true;
}
else
{
instanceToRegister = instance;
break;
}
}


if (instanceToRegister != null)
{
// Did we end up choosing the standalone MSBuild because there was an invalid Visual Studio?
// If so, provide a helpful message to the user.
if (invalidVSFound && instanceToRegister.DiscoveryType == DiscoveryType.StandAlone)
{
logger.LogWarning(@"It looks like you have Visual Studio 2017 RTM installed.
Try updating Visual Studio 2017 to the most recent release to enable better MSBuild support.");
}

msbuildLocator.RegisterInstance(instanceToRegister);
}
else
{
logger.LogError("Could not locate MSBuild instance to register with OmniSharp");
}
}

private static IEnumerable<Type> SafeGetTypes(Assembly a)
{
try
Expand Down
13 changes: 13 additions & 0 deletions src/OmniSharp.Host/MSBuild/Discovery/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace OmniSharp.MSBuild.Discovery
{
internal static class Extensions
{
public static bool IsInvalidVisualStudio(this MSBuildInstance instance)
// MSBuild from Visual Studio 2017 RTM cannot be used.
=> instance.Version.Major == 15
&& instance.Version.Minor == 0
&& (instance.DiscoveryType == DiscoveryType.DeveloperConsole
|| instance.DiscoveryType == DiscoveryType.VisualStudioSetup);

}
}

0 comments on commit 5cacd71

Please sign in to comment.