44#nullable disable
55
66using System . Collections . Immutable ;
7+ using System . Runtime . InteropServices ;
78using Microsoft . Build . Evaluation ;
89using Microsoft . Build . Execution ;
10+ using Microsoft . Build . Framework ;
911using Microsoft . Build . Locator ;
12+ using Microsoft . Build . Logging ;
1013using Microsoft . CodeAnalysis . MSBuild ;
1114using static Microsoft . Build . Execution . BuildRequestDataFlags ;
1215
@@ -15,6 +18,8 @@ namespace LeanCode.ContractsGenerator.Compilation.MSBuild;
1518public static class MSBuildHelper
1619{
1720 private static readonly string [ ] RestoreTarget = [ "Restore" ] ;
21+ private static readonly string LoggerVerbosity = Environment . GetEnvironmentVariable ( "LNCD_CG_MSB_LOG" ) ;
22+ private static readonly bool LogEnabled = LoggerVerbosity is { Length : > 0 } ;
1823
1924 private static readonly ImmutableDictionary < string , string > GlobalProperties = ImmutableDictionary . CreateRange (
2025 new Dictionary < string , string >
@@ -30,19 +35,57 @@ public static class MSBuildHelper
3035
3136 static MSBuildHelper ( )
3237 {
33- // QueryVisualStudioInstances returns Visual Studio installations on .NET Framework, and .NET Core SDK
34- // installations on .NET Core. We use the one with the most recent version.
35- var msBuildInstance = MSBuildLocator . QueryVisualStudioInstances ( ) . OrderByDescending ( x => x . Version ) . First ( ) ;
38+ try
39+ {
40+ // QueryVisualStudioInstances returns Visual Studio installations on .NET Framework,
41+ // and .NET Core SDK installations on .NET Core. We use the one with the most recent version.
42+ var msBuildInstances = MSBuildLocator
43+ . QueryVisualStudioInstances ( )
44+ . OrderByDescending ( x => x . Version )
45+ . ToList ( ) ;
46+
47+ if ( LogEnabled )
48+ {
49+ Console . Error . WriteLine ( $ "Running on { RuntimeInformation . FrameworkDescription } ") ;
50+ Console . Error . WriteLine ( "MSBuild instances:" ) ;
51+
52+ if ( msBuildInstances . Count > 0 )
53+ {
54+ foreach ( var instance in msBuildInstances )
55+ {
56+ Console . Error . WriteLine ( $ " { instance . Version } @ { instance . MSBuildPath } ") ;
57+ }
58+ }
59+ else
60+ {
61+ Console . Error . WriteLine ( $ " none found") ;
62+ }
63+ }
3664
37- // Since we do not inherit msbuild.deps.json when referencing the SDK copy
38- // of MSBuild and because the SDK no longer ships with version matched assemblies, we
39- // register an assembly loader that will load assemblies from the msbuild path with
40- // equal or higher version numbers than requested.
41- LooseVersionAssemblyLoader . Register ( msBuildInstance . MSBuildPath ) ;
65+ var msBuildInstance = msBuildInstances [ 0 ] ;
4266
43- if ( MSBuildLocator . CanRegister )
67+ if ( MSBuildLocator . CanRegister )
68+ {
69+ MSBuildLocator . RegisterInstance ( msBuildInstance ) ;
70+
71+ if ( LogEnabled )
72+ {
73+ Console . Error . WriteLine (
74+ $ "MSBuild instance { msBuildInstance . Version } @ { msBuildInstance . MSBuildPath } registered."
75+ ) ;
76+ }
77+ }
78+ else if ( LogEnabled )
79+ {
80+ Console . Error . WriteLine (
81+ $ "Could not register found MSBuild instance { msBuildInstance . Version } @ { msBuildInstance . MSBuildPath } ."
82+ ) ;
83+ }
84+ }
85+ catch ( Exception e )
4486 {
45- MSBuildLocator . RegisterInstance ( msBuildInstance ) ;
87+ Console . Error . WriteLine ( e ) ;
88+ throw ;
4689 }
4790 }
4891
@@ -78,6 +121,9 @@ public static int RestoreProjects(IReadOnlyCollection<string> projectPaths)
78121 DisableInProcNode = true ,
79122 // don't ask the user for anything
80123 Interactive = false ,
124+ Loggers = Enum . TryParse < LoggerVerbosity > ( LoggerVerbosity , true , out var verbosity )
125+ ? [ new ConsoleLogger ( verbosity ) ]
126+ : [ ] ,
81127 }
82128 ) ;
83129
0 commit comments