Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support creating binlogs for the language server's design time builds #69572

Merged
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
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)
jasonmalinowski marked this conversation as resolved.
Show resolved Hide resolved
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: "projects", 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public void VerifyLspClientOptionNames()
"background_analysis.dotnet_analyzer_diagnostics_scope",
"background_analysis.dotnet_compiler_diagnostics_scope",
"code_lens.dotnet_enable_references_code_lens",
"code_lens.dotnet_enable_tests_code_lens"
"code_lens.dotnet_enable_tests_code_lens",
"projects.dotnet_binary_log_path"
}.OrderBy(name => name);

Assert.Equal(expectedNames, actualNames);
Expand Down Expand Up @@ -267,6 +268,10 @@ private static string GenerateDefaultValue(IOption2 option)
var defaultValue = (int)option.DefaultValue!;
return defaultValue + 1;
}
else if (type == typeof(string))
{
return Guid.NewGuid().ToString();
}
else if (type.IsEnum)
{
return GetDifferentEnumValue(type, option.DefaultValue!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public void OptionHasStorageIfNecessary(string configName)
"dotnet_remove_unnecessary_suppression_exclusions", // Doesn't have VS UI. TODO: https://github.com/dotnet/roslyn/issues/66062
"dotnet_style_operator_placement_when_wrapping", // Doesn't have VS UI. TODO: https://github.com/dotnet/roslyn/issues/66062
"dotnet_style_prefer_foreach_explicit_cast_in_source", // For a small customer segment, doesn't warrant VS UI.
"dotnet_binary_log_path", // VSCode only option for the VS Code project system; does not apply to VS
"dotnet_lsp_using_devkit", // VSCode internal only option. Does not need any UI.
"dotnet_enable_references_code_lens", // VSCode only option. Does not apply to VS.
"dotnet_enable_tests_code_lens", // VSCode only option. Does not apply to VS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ public void StartBatchBuild(IDictionary<string, string>? globalProperties = null
{
Loggers = _msbuildLogger is null
? (new MSB.Framework.ILogger[] { _batchBuildLogger })
: (new MSB.Framework.ILogger[] { _batchBuildLogger, _msbuildLogger })
: (new MSB.Framework.ILogger[] { _batchBuildLogger, _msbuildLogger }),

// If we have an additional logger and it's diagnostic, then we need to opt into task inputs globally, or otherwise
// it won't get any log events. This logic matches https://github.com/dotnet/msbuild/blob/fa6710d2720dcf1230a732a8858ffe71bcdbe110/src/Build/Instance/ProjectInstance.cs#L2365-L2371
LogTaskInputs = _msbuildLogger is not null && _msbuildLogger.Verbosity == LoggerVerbosity.Diagnostic
JoeRobich marked this conversation as resolved.
Show resolved Hide resolved
};

MSB.Execution.BuildManager.DefaultBuildManager.BeginBuild(buildParameters);
Expand Down
Loading