Skip to content

Commit

Permalink
Set MSBuild environment variables a bit more robustly, using the loca…
Browse files Browse the repository at this point in the history
…tion of OmniSharp.sln if necessary
  • Loading branch information
DustinCampbell committed Dec 13, 2016
1 parent 15b8d85 commit 023e2c1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
51 changes: 46 additions & 5 deletions src/OmniSharp.MSBuild/MSBuildProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -67,8 +68,49 @@ public MSBuildProjectSystem(
_logger = loggerFactory.CreateLogger("OmniSharp#MSBuild");
}

public static void SetUpMSBuildEnvironment(string msbuildFolder, ILogger logger)
private static string FindMSBuildFolder()
{
// Try to locate the appropriate build-time msbuild folder by searching for
// the OmniSharp solution relative to the current folder.

var current = Directory.GetCurrentDirectory();
while (!File.Exists(Path.Combine(current, "OmniSharp.sln")))
{
current = Path.GetDirectoryName(current);
if (Path.GetPathRoot(current) == current)
{
break;
}
}

#if NET46
var folderName = ".msbuild-net46";
#else
var folderName = ".msbuild-netcoreapp1.0";
#endif

var result = Path.Combine(current, folderName);

return Directory.Exists(result)
? result
: null;
}

public static void SetUpMSBuildEnvironment(ILogger logger)
{
var msbuildFolder = Path.Combine(AppContext.BaseDirectory, "msbuild");

if (!Directory.Exists(msbuildFolder))
{
msbuildFolder = FindMSBuildFolder();
}

if (msbuildFolder == null || !Directory.Exists(msbuildFolder))
{
logger.LogError("Could not locate MSBuild path. MSBuildProjectSystem will not function properly.");
return;
}

// Set the MSBuildExtensionsPath environment variable to the msbuild folder.
Environment.SetEnvironmentVariable("MSBuildExtensionsPath", msbuildFolder);
logger.LogInformation($"MSBuildExtensionsPath environment variable set to {msbuildFolder}");
Expand Down Expand Up @@ -114,13 +156,12 @@ public void Initalize(IConfiguration configuration)
_options = new MSBuildOptions();
ConfigurationBinder.Bind(configuration, _options);

var msbuildFolder = Path.Combine(AppContext.BaseDirectory, "msbuild");
SetUpMSBuildEnvironment(msbuildFolder, _logger);
SetUpMSBuildEnvironment(_logger);

if (_options.WaitForDebugger)
{
Console.WriteLine($"Attach to process {System.Diagnostics.Process.GetCurrentProcess().Id}");
while (!System.Diagnostics.Debugger.IsAttached)
Console.WriteLine($"Attach to process {Process.GetCurrentProcess().Id}");
while (!Debugger.IsAttached)
{
System.Threading.Thread.Sleep(100);
}
Expand Down
9 changes: 1 addition & 8 deletions tests/OmniSharp.MSBuild.Tests/ProjectFileInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ public ProjectFileInfoTests()
var loggerFactory = new FakeLoggerFactory();
this._logger = loggerFactory.CreateLogger("test");

#if NET46
var folderName = ".msbuild-net46";
#else
var folderName = ".msbuild-netcoreapp1.0";
#endif

var msbuildFolder = Path.Combine(this._testAssets.SolutionFolder, folderName);
MSBuildProjectSystem.SetUpMSBuildEnvironment(msbuildFolder, this._logger);
MSBuildProjectSystem.SetUpMSBuildEnvironment(this._logger);
}

[Fact]
Expand Down

0 comments on commit 023e2c1

Please sign in to comment.