File tree Expand file tree Collapse file tree 3 files changed +53
-11
lines changed Expand file tree Collapse file tree 3 files changed +53
-11
lines changed Original file line number Diff line number Diff line change 7
7
using CSharpCodeAnalyst . Exploration ;
8
8
using CSharpCodeAnalyst . GraphArea ;
9
9
using CSharpCodeAnalyst . TreeArea ;
10
+ using Microsoft . Build . Tasks ;
10
11
using Microsoft . Extensions . Configuration ;
11
12
12
13
namespace CSharpCodeAnalyst ;
@@ -20,7 +21,14 @@ protected override void OnStartup(StartupEventArgs e)
20
21
{
21
22
base . OnStartup ( e ) ;
22
23
23
- Initializer . InitializeMsBuildLocator ( ) ;
24
+ try
25
+ {
26
+ Initializer . InitializeMsBuildLocator ( ) ;
27
+ }
28
+ catch ( Exception ex )
29
+ {
30
+ MessageBox . Show ( ex . ToString ( ) ) ;
31
+ }
24
32
25
33
// Load application settings
26
34
var builder = new ConfigurationBuilder ( )
Original file line number Diff line number Diff line change 3
3
<PropertyGroup >
4
4
<TargetFramework >net8.0</TargetFramework >
5
5
<ImplicitUsings >enable</ImplicitUsings >
6
- <Nullable >enable</Nullable >
6
+ <Nullable >enable</Nullable >
7
7
</PropertyGroup >
8
8
9
9
<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 " >
12
12
<PrivateAssets >all</PrivateAssets >
13
13
<IncludeAssets >runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets >
14
14
</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" />
20
20
</ItemGroup >
21
21
22
22
<ItemGroup >
23
- <ProjectReference Include =" ..\Contracts\Contracts.csproj" />
23
+ <ProjectReference Include =" ..\Contracts\Contracts.csproj" />
24
24
</ItemGroup >
25
25
26
26
</Project >
Original file line number Diff line number Diff line change @@ -9,6 +9,40 @@ public static void InitializeMsBuildLocator()
9
9
// Without the MSBuildLocator the Project.Documents list is empty!
10
10
// Referencing MSBuild packages directly and copy to output is not reliable and causes
11
11
// 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 ;
13
47
}
14
48
}
You can’t perform that action at this time.
0 commit comments