Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/bsp-server/Handlers/BuildInitializeHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BaseProtocol;
using System.Text.Json;
using BaseProtocol;
using bsp4csharp.Protocol;
using Microsoft.Build.Locator;

Expand All @@ -16,9 +17,34 @@ public Task<InitializeBuildResult> HandleRequestAsync(InitializeBuildParams requ
{
_initializeManager.SetInitializeParams(request);

if (MSBuildLocator.IsRegistered)
{
throw new ServerNotInitializedException("MSBuild instance already registered.");
}

// this has to be loaded before we try to use any Microsoft.Build.* references
var msBuildInstance = MSBuildLocator.RegisterDefaults();
context.Logger.LogInformation("MSBuild instance used: {}", msBuildInstance.MSBuildPath);
var instanceQueryOptions = new VisualStudioInstanceQueryOptions
{
DiscoveryTypes = DiscoveryType.DotNetSdk,
WorkingDirectory = request.RootUri.AbsolutePath,
};

var msBuildInstances = MSBuildLocator.QueryVisualStudioInstances(instanceQueryOptions)
.OrderByDescending(x => x.Version);
var msBuildInstance = msBuildInstances
.FirstOrDefault();
foreach (var instance in msBuildInstances)
{
context.Logger.LogInformation("MSBuild instance found: {}", JsonSerializer.Serialize(instance));
}

if (msBuildInstance == null)
{
throw new ServerNotInitializedException("MSBuild instance could not be found on your system.");
}

MSBuildLocator.RegisterInstance(msBuildInstance);
context.Logger.LogInformation("MSBuild instance used: {}", JsonSerializer.Serialize(msBuildInstance));

var serverCapabilities = _initializeManager.GetInitializeResult();

Expand Down
17 changes: 13 additions & 4 deletions src/bsp-server/Logging/MSBuildLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
private readonly ICollection<string> _diagnosticKeysCollection = [];
private int Warnings = 0;
private int Errors = 0;
private readonly DateTime _buildStartTimestamp;
private readonly string[] _targetsOfInterest = ["Clean", "Build", "Restore"];
private TimeProvider _timeProvider = TimeProvider.System;
private DateTimeOffset _buildStartTimestamp;
private readonly string[] _targetsOfInterest = [
"Clean",
"Build",
"Restore",
];

public LoggerVerbosity Verbosity { get; set; } = LoggerVerbosity.Normal;

public string Parameters { get; set; } = string.Empty;

Check warning on line 27 in src/bsp-server/Logging/MSBuildLogger.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Nullability of reference types in type of parameter 'value' of 'void MSBuildLogger.Parameters.set' doesn't match implicitly implemented member 'void ILogger.Parameters.set' (possibly because of nullability attributes).

Check warning on line 27 in src/bsp-server/Logging/MSBuildLogger.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Nullability of reference types in type of parameter 'value' of 'void MSBuildLogger.Parameters.set' doesn't match implicitly implemented member 'void ILogger.Parameters.set' (possibly because of nullability attributes).

Check warning on line 27 in src/bsp-server/Logging/MSBuildLogger.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Nullability of reference types in type of parameter 'value' of 'void MSBuildLogger.Parameters.set' doesn't match implicitly implemented member 'void ILogger.Parameters.set' (possibly because of nullability attributes).

Check warning on line 27 in src/bsp-server/Logging/MSBuildLogger.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Nullability of reference types in type of parameter 'value' of 'void MSBuildLogger.Parameters.set' doesn't match implicitly implemented member 'void ILogger.Parameters.set' (possibly because of nullability attributes).

public void Initialize(IEventSource eventSource)
{
Expand All @@ -36,8 +41,12 @@

private void TargetStarted(object sender, TargetStartedEventArgs e)
{
if (_targetsOfInterest.Contains(e.TargetName, StringComparer.InvariantCultureIgnoreCase) &&
e.BuildReason == TargetBuiltReason.None)
if (_buildStartTimestamp == null)

Check warning on line 44 in src/bsp-server/Logging/MSBuildLogger.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The result of the expression is always 'false' since a value of type 'DateTimeOffset' is never equal to 'null' of type 'DateTimeOffset?'

Check warning on line 44 in src/bsp-server/Logging/MSBuildLogger.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The result of the expression is always 'false' since a value of type 'DateTimeOffset' is never equal to 'null' of type 'DateTimeOffset?'

Check warning on line 44 in src/bsp-server/Logging/MSBuildLogger.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

The result of the expression is always 'false' since a value of type 'DateTimeOffset' is never equal to 'null' of type 'DateTimeOffset?'

Check warning on line 44 in src/bsp-server/Logging/MSBuildLogger.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

The result of the expression is always 'false' since a value of type 'DateTimeOffset' is never equal to 'null' of type 'DateTimeOffset?'
{
_buildStartTimestamp = _timeProvider.GetUtcNow();
}

if (_targetsOfInterest.Contains(e.TargetName, StringComparer.InvariantCultureIgnoreCase))
{
var taskId = new TaskId { Id = e.ProjectFile + "#" + e.TargetName + "#" + e.ThreadId };
var taskStartParams = new TaskStartParams
Expand Down