Skip to content

Commit

Permalink
Support creating binlogs for the language server's design time builds
Browse files Browse the repository at this point in the history
This allows you to configure a directory where we will write binlogs
for any design time builds. This is only needed when debugging project
system issues, but when you need this, you really need this.
  • Loading branch information
jasonmalinowski committed Aug 16, 2023
1 parent 47b1d1c commit d012eef
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Microsoft.Build.Locator;
using Microsoft.Build.Logging;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.MSBuild.Build;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.ProjectSystem;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Workspaces.ProjectSystem;
Expand All @@ -32,13 +34,23 @@ internal sealed class LanguageServerProjectSystem
/// This is just we don't have code simultaneously trying to load and unload solutions at once.
/// </summary>
private readonly SemaphoreSlim _gate = new SemaphoreSlim(initialCount: 1);

private bool _msbuildLoaded = false;

/// <summary>
/// The suffix to use for the binary log name; incremented each time we have a new build. Should be incremented with <see cref="Interlocked.Increment(ref int)"/>.
/// </summary>
private int _binaryLogNumericSuffix;

/// <summary>
/// A GUID put into all binary log file names, so that way one session doesn't accidentally overwrite the logs from a prior session.
/// </summary>
private readonly Guid _binaryLogGuidSuffix = Guid.NewGuid();

private readonly AsyncBatchingWorkQueue<string> _projectsToLoadAndReload;

private readonly LanguageServerWorkspaceFactory _workspaceFactory;
private readonly IFileChangeWatcher _fileChangeWatcher;
private readonly IGlobalOptionService _globalOptionService;
private readonly ILogger _logger;

/// <summary>
Expand All @@ -54,11 +66,13 @@ internal sealed class LanguageServerProjectSystem
public LanguageServerProjectSystem(
LanguageServerWorkspaceFactory workspaceFactory,
IFileChangeWatcher fileChangeWatcher,
IGlobalOptionService globalOptionService,
ILoggerFactory loggerFactory,
IAsynchronousOperationListenerProvider listenerProvider)
{
_workspaceFactory = workspaceFactory;
_fileChangeWatcher = fileChangeWatcher;
_globalOptionService = globalOptionService;
_logger = loggerFactory.CreateLogger(nameof(LanguageServerProjectSystem));

// TODO: remove the DiagnosticReporter that's coupled to the Workspace here
Expand Down Expand Up @@ -164,7 +178,7 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<string>
var stopwatch = Stopwatch.StartNew();

// TODO: support configuration switching
var projectBuildManager = new ProjectBuildManager(additionalGlobalProperties: ImmutableDictionary<string, string>.Empty);
var projectBuildManager = new ProjectBuildManager(additionalGlobalProperties: ImmutableDictionary<string, string>.Empty, msbuildLogger: CreateMSBuildLogger());

projectBuildManager.StartBatchBuild();

Expand Down Expand Up @@ -202,6 +216,19 @@ private async ValueTask LoadOrReloadProjectsAsync(ImmutableSegmentedList<string>
}
}

private Build.Framework.ILogger? CreateMSBuildLogger()
{
if (_globalOptionService.GetOption(LanguageServerProjectSystemOptionsStorage.BinaryLogPath) is not string binaryLogDirectory)
return null;

var numericSuffix = Interlocked.Increment(ref _binaryLogNumericSuffix);
var binaryLogPath = Path.Combine(binaryLogDirectory, $"LanguageServerDesignTimeBuild-{_binaryLogGuidSuffix}-{numericSuffix}.binlog");

_logger.LogInformation($"Logging design-time builds to {binaryLogPath}");

return new BinaryLogger { Parameters = binaryLogPath, Verbosity = Build.Framework.LoggerVerbosity.Diagnostic };
}

private async Task<LSP.MessageType?> LoadOrReloadProjectAsync(string projectPath, ProjectBuildManager projectBuildManager, CancellationToken cancellationToken)
{
try
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.LanguageServer.HostWorkspace
{
internal static class LanguageServerProjectSystemOptionsStorage
{
private static readonly OptionGroup s_optionGroup = new(name: "project_loading", description: "");

/// <summary>
/// A folder to log binlogs to when running design-time builds.
/// </summary>
public static readonly Option2<string?> BinaryLogPath = new Option2<string?>("dotnet_binary_log_path", defaultValue: null, s_optionGroup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.ImplementType;
using Microsoft.CodeAnalysis.InlineHints;
using Microsoft.CodeAnalysis.LanguageServer.HostWorkspace;
using Microsoft.CodeAnalysis.MetadataAsSource;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.QuickInfo;
Expand Down Expand Up @@ -58,6 +59,8 @@ internal partial class DidChangeConfigurationNotificationHandler
SolutionCrawlerOptionsStorage.CompilerDiagnosticsScopeOption,
// Code lens options
LspOptionsStorage.LspEnableReferencesCodeLens,
LspOptionsStorage.LspEnableTestsCodeLens);
LspOptionsStorage.LspEnableTestsCodeLens,
// Project system
LanguageServerProjectSystemOptionsStorage.BinaryLogPath);
}
}

0 comments on commit d012eef

Please sign in to comment.