Skip to content

Commit 4a73a5b

Browse files
committed
Updated to latest MSBuildLocator
1 parent 06ae12c commit 4a73a5b

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

CSharpCodeAnalyst/App.xaml.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using CSharpCodeAnalyst.Exploration;
88
using CSharpCodeAnalyst.GraphArea;
99
using CSharpCodeAnalyst.TreeArea;
10+
using Microsoft.Build.Tasks;
1011
using Microsoft.Extensions.Configuration;
1112

1213
namespace CSharpCodeAnalyst;
@@ -20,7 +21,14 @@ protected override void OnStartup(StartupEventArgs e)
2021
{
2122
base.OnStartup(e);
2223

23-
Initializer.InitializeMsBuildLocator();
24+
try
25+
{
26+
Initializer.InitializeMsBuildLocator();
27+
}
28+
catch (Exception ex)
29+
{
30+
MessageBox.Show(ex.ToString());
31+
}
2432

2533
// Load application settings
2634
var builder = new ConfigurationBuilder()

CodeParser/CodeParser.csproj

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>enable</Nullable>
6+
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.Build.Locator" Version="1.7.8"/>
11-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
10+
<PackageReference Include="Microsoft.Build.Locator" Version="1.9.1" />
11+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="4.14.0">
1212
<PrivateAssets>all</PrivateAssets>
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
15-
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.10.0"/>
16-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0"/>
17-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.10.0"/>
18-
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.10.0"/>
19-
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.10.0"/>
15+
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.14.0" />
16+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />
17+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.14.0" />
18+
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.14.0" />
19+
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.14.0" />
2020
</ItemGroup>
2121

2222
<ItemGroup>
23-
<ProjectReference Include="..\Contracts\Contracts.csproj"/>
23+
<ProjectReference Include="..\Contracts\Contracts.csproj" />
2424
</ItemGroup>
2525

2626
</Project>

CodeParser/Parser/Initializer.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,40 @@ public static void InitializeMsBuildLocator()
99
// Without the MSBuildLocator the Project.Documents list is empty!
1010
// Referencing MSBuild packages directly and copy to output is not reliable and causes
1111
// hard to find problems
12-
MSBuildLocator.RegisterDefaults();
12+
13+
try
14+
{
15+
MSBuildLocator.RegisterDefaults();
16+
}
17+
catch
18+
{
19+
RegisterMsBuildManually();
20+
}
21+
}
22+
23+
private static void RegisterMsBuildManually()
24+
{
25+
var fallback = GetFallbackMsBuildPath();
26+
if (string.IsNullOrEmpty(fallback))
27+
{
28+
throw new InvalidOperationException(
29+
"Failed to register MSBuild path. Please ensure that MSBuild is installed and the path is correct.");
30+
}
31+
32+
MSBuildLocator.RegisterMSBuildPath(fallback);
33+
}
34+
35+
private static string GetFallbackMsBuildPath()
36+
{
37+
var fallbacks = new[] { @"C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin" };
38+
foreach (var fallback in fallbacks)
39+
{
40+
if (Directory.Exists(fallback))
41+
{
42+
return fallback;
43+
}
44+
}
45+
46+
return string.Empty;
1347
}
1448
}

0 commit comments

Comments
 (0)