1- using BaseProtocol ;
1+ using System . Text . Json ;
2+ using BaseProtocol ;
23using bsp4csharp . Protocol ;
34using Microsoft . Build . Locator ;
45
@@ -16,9 +17,34 @@ public Task<InitializeBuildResult> HandleRequestAsync(InitializeBuildParams requ
1617 {
1718 _initializeManager . SetInitializeParams ( request ) ;
1819
20+ if ( MSBuildLocator . IsRegistered )
21+ {
22+ throw new ServerNotInitializedException ( "MSBuild instance already registered." ) ;
23+ }
24+
1925 // this has to be loaded before we try to use any Microsoft.Build.* references
20- var msBuildInstance = MSBuildLocator . RegisterDefaults ( ) ;
21- context . Logger . LogInformation ( "MSBuild instance used: {}" , msBuildInstance . MSBuildPath ) ;
26+ var instanceQueryOptions = new VisualStudioInstanceQueryOptions
27+ {
28+ DiscoveryTypes = DiscoveryType . DotNetSdk ,
29+ WorkingDirectory = request . RootUri . AbsolutePath ,
30+ } ;
31+
32+ var msBuildInstances = MSBuildLocator . QueryVisualStudioInstances ( instanceQueryOptions )
33+ . OrderByDescending ( x => x . Version ) ;
34+ var msBuildInstance = msBuildInstances
35+ . FirstOrDefault ( ) ;
36+ foreach ( var instance in msBuildInstances )
37+ {
38+ context . Logger . LogInformation ( "MSBuild instance found: {}" , JsonSerializer . Serialize ( instance ) ) ;
39+ }
40+
41+ if ( msBuildInstance == null )
42+ {
43+ throw new ServerNotInitializedException ( "MSBuild instance could not be found on your system." ) ;
44+ }
45+
46+ MSBuildLocator . RegisterInstance ( msBuildInstance ) ;
47+ context . Logger . LogInformation ( "MSBuild instance used: {}" , JsonSerializer . Serialize ( msBuildInstance ) ) ;
2248
2349 var serverCapabilities = _initializeManager . GetInitializeResult ( ) ;
2450
0 commit comments