Skip to content

Commit 4bc5807

Browse files
authored
Merge pull request #170 from leancodepl/extra-logs
Add some extra logging and cleanup MSBuild integration
2 parents 3eabec6 + cb5cb02 commit 4bc5807

File tree

7 files changed

+71
-102
lines changed

7 files changed

+71
-102
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"type": "coreclr",
1010
"request": "launch",
1111
"preLaunchTask": "build",
12-
"program": "${workspaceFolder}/src/LeanCode.ContractsGenerator/bin/Debug/net8.0/LeanCode.ContractsGenerator.dll",
12+
"program": "${workspaceFolder}/src/LeanCode.ContractsGenerator/bin/Debug/net9.0/LeanCode.ContractsGenerator.dll",
1313
"args": [],
1414
"cwd": "${workspaceFolder}",
1515
"console": "internalConsole",

Directory.Build.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<PackageReference Update="Microsoft.Build.Locator" Version="1.9.1" />
2020
<PackageReference Update="Microsoft.Build" Version="$(MsbVer)" />
2121
<PackageReference Update="Microsoft.Build.Framework" Version="$(MsbVer)" />
22+
<PackageReference Update="Microsoft.Build.Tasks.Core" Version="$(MsbVer)" />
23+
<PackageReference Update="Microsoft.Build.Utilities.Core" Version="$(MsbVer)" />
2224
<PackageReference Update="Microsoft.CodeAnalysis.CSharp" Version="$(CscVer)" />
2325
<PackageReference Update="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(CscVer)" />
2426
<PackageReference Update="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="$(CscVer)" />

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "8.0.100",
3+
"version": "9.0.100",
44
"allowPrerelease": true,
55
"rollForward": "latestMajor"
66
}

src/LeanCode.ContractsGenerator/Compilation/MSBuild/LooseAssemblyVersionLoader.cs

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/LeanCode.ContractsGenerator/Compilation/MSBuild/MSBuildHelper.cs

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
#nullable disable
55

66
using System.Collections.Immutable;
7+
using System.Runtime.InteropServices;
78
using Microsoft.Build.Evaluation;
89
using Microsoft.Build.Execution;
10+
using Microsoft.Build.Framework;
911
using Microsoft.Build.Locator;
12+
using Microsoft.Build.Logging;
1013
using Microsoft.CodeAnalysis.MSBuild;
1114
using static Microsoft.Build.Execution.BuildRequestDataFlags;
1215

@@ -15,6 +18,8 @@ namespace LeanCode.ContractsGenerator.Compilation.MSBuild;
1518
public 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

src/LeanCode.ContractsGenerator/LeanCode.ContractsGenerator.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
<PackAsTool>true</PackAsTool>
1111
<ToolCommandName>dotnet-contracts-generate</ToolCommandName>
1212
</PropertyGroup>
13+
<ItemGroup>
14+
<!-- We don't actually ship affected versions, instead the versions from .NET SDK are used. -->
15+
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-h4j7-5rxr-p4wc" />
16+
</ItemGroup>
1317
<ItemGroup>
1418
<Protobuf Include="../../contracts.proto" GrpcServices="None" />
1519
</ItemGroup>
@@ -25,6 +29,8 @@
2529
</PackageReference>
2630
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" PrivateAssets="all" />
2731
<PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="runtime" PrivateAssets="all" />
32+
<PackageReference Include="Microsoft.Build.Tasks.Core" ExcludeAssets="runtime" PrivateAssets="all" />
33+
<PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="runtime" PrivateAssets="all" />
2834
<PackageReference Include="Microsoft.Build.Locator" />
2935
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
3036
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />

src/LeanCode.ContractsGenerator/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Runtime.InteropServices;
12
using CommandLine;
23
using Google.Protobuf;
34
using LeanCode.ContractsGenerator.Compilation;
@@ -98,6 +99,10 @@ private static async Task<int> Main(string[] args)
9899
{
99100
return await Parser
100101
.Default.ParseArguments<ProjectOptions, FileOptions, PathOptions>(args)
102+
.WithNotParsed(errs =>
103+
{
104+
Console.Error.WriteLine($"Running on {RuntimeInformation.FrameworkDescription}");
105+
})
101106
.MapResult(
102107
(ProjectOptions p) => HandleProjectAsync(p),
103108
(FileOptions f) => HandleFileAsync(f),

0 commit comments

Comments
 (0)