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

Remove compile-time dependency from Workspace.MSBuild on Workspace.MSBuild.BuildHost #73393

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -11,11 +11,10 @@
using System.Threading.Tasks;
using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.CodeAnalysis.MSBuild.Logging;
using Roslyn.Utilities;
using MSB = Microsoft.Build;

namespace Microsoft.CodeAnalysis.MSBuild.Build
namespace Microsoft.CodeAnalysis.MSBuild
{
internal class ProjectBuildManager
{
Expand Down
19 changes: 7 additions & 12 deletions src/Workspaces/Core/MSBuild.BuildHost/BuildHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

extern alias workspaces;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
Expand All @@ -13,26 +12,22 @@
using Microsoft.Build.Construction;
using Microsoft.Build.Locator;
using Microsoft.Build.Logging;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.MSBuild.Build;
using Microsoft.CodeAnalysis.MSBuild.Rpc;
using Microsoft.Extensions.Logging;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost;
namespace Microsoft.CodeAnalysis.MSBuild;

internal sealed class BuildHost
internal sealed class BuildHost : IBuildHost
{
private readonly ILogger _logger;
private readonly BuildHostLogger _logger;
private readonly ImmutableDictionary<string, string> _globalMSBuildProperties;
private readonly string? _binaryLogPath;
private readonly RpcServer _server;
private readonly object _gate = new object();
private ProjectBuildManager? _buildManager;

public BuildHost(ILoggerFactory loggerFactory, ImmutableDictionary<string, string> globalMSBuildProperties, string? binaryLogPath, RpcServer server)
public BuildHost(BuildHostLogger logger, ImmutableDictionary<string, string> globalMSBuildProperties, string? binaryLogPath, RpcServer server)
{
_logger = loggerFactory.CreateLogger<BuildHost>();
_logger = logger;
_globalMSBuildProperties = globalMSBuildProperties;
_binaryLogPath = binaryLogPath;
_server = server;
Expand Down Expand Up @@ -177,8 +172,8 @@ public async Task<int> LoadProjectFileAsync(string projectFilePath, string langu

ProjectFileLoader projectLoader = languageName switch
{
LanguageNames.CSharp => new CSharp.CSharpProjectFileLoader(),
LanguageNames.VisualBasic => new VisualBasic.VisualBasicProjectFileLoader(),
LanguageNames.CSharp => new CSharpProjectFileLoader(),
LanguageNames.VisualBasic => new VisualBasicProjectFileLoader(),
_ => throw ExceptionUtilities.UnexpectedValue(languageName)
};

Expand Down
16 changes: 16 additions & 0 deletions src/Workspaces/Core/MSBuild.BuildHost/BuildHostLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 System.IO;

namespace Microsoft.CodeAnalysis.MSBuild;

internal sealed class BuildHostLogger(TextWriter output)
{
public void LogInformation(string message)
=> output.WriteLine(message);

public void LogCritical(string message)
=> output.WriteLine(message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.MSBuild;
using MSB = Microsoft.Build;

namespace Microsoft.CodeAnalysis.CSharp
namespace Microsoft.CodeAnalysis.MSBuild
{
internal class CSharpCommandLineArgumentReader : CommandLineArgumentReader
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.MSBuild.Build;
using Microsoft.CodeAnalysis.MSBuild.Logging;
using MSB = Microsoft.Build;

namespace Microsoft.CodeAnalysis.CSharp
namespace Microsoft.CodeAnalysis.MSBuild
{
internal class CSharpProjectFile : ProjectFile
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
// 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.MSBuild;
using Microsoft.CodeAnalysis.MSBuild.Build;
using Microsoft.CodeAnalysis.MSBuild.Logging;
using MSB = Microsoft.Build;

namespace Microsoft.CodeAnalysis.CSharp
namespace Microsoft.CodeAnalysis.MSBuild
{
internal partial class CSharpProjectFileLoader : ProjectFileLoader
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.CodeAnalysis.MSBuild.Logging
namespace Microsoft.CodeAnalysis.MSBuild
{
internal class DiagnosticLog : IEnumerable<DiagnosticLogItem>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using System;

namespace Microsoft.CodeAnalysis.MSBuild.Logging
namespace Microsoft.CodeAnalysis.MSBuild
{
internal class MSBuildDiagnosticLogItem : DiagnosticLogItem
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Roslyn.Utilities;
using MSB = Microsoft.Build;

namespace Microsoft.CodeAnalysis.MSBuild.Logging
namespace Microsoft.CodeAnalysis.MSBuild
{
internal class MSBuildDiagnosticLogger : MSB.Framework.ILogger
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.MSBuild.Build;
using Microsoft.CodeAnalysis.MSBuild.Logging;
using Roslyn.Utilities;
using MSB = Microsoft.Build;

namespace Microsoft.CodeAnalysis.MSBuild
{
internal abstract class ProjectFile
internal abstract class ProjectFile : IProjectFile
{
private readonly ProjectFileLoader _loader;
private readonly MSB.Evaluation.Project? _loadedProject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.MSBuild.Build;
using Microsoft.CodeAnalysis.MSBuild.Logging;
using MSB = Microsoft.Build;

namespace Microsoft.CodeAnalysis.MSBuild
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.MSBuild;
using Roslyn.Utilities;
using MSB = Microsoft.Build;

namespace Microsoft.CodeAnalysis.VisualBasic
namespace Microsoft.CodeAnalysis.MSBuild
{
internal class VisualBasicCommandLineArgumentReader : CommandLineArgumentReader
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.MSBuild.Build;
using Microsoft.CodeAnalysis.MSBuild.Logging;
using MSB = Microsoft.Build;

namespace Microsoft.CodeAnalysis.VisualBasic
namespace Microsoft.CodeAnalysis.MSBuild
{
internal class VisualBasicProjectFile : ProjectFile
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
// 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.MSBuild;
using Microsoft.CodeAnalysis.MSBuild.Build;
using Microsoft.CodeAnalysis.MSBuild.Logging;
using MSB = Microsoft.Build;

namespace Microsoft.CodeAnalysis.VisualBasic
namespace Microsoft.CodeAnalysis.MSBuild
{
internal partial class VisualBasicProjectFileLoader : ProjectFileLoader
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<PackageReference Include="Microsoft.Build.Tasks.Core" VersionOverride="17.3.2" ExcludeAssets="Runtime" PrivateAssets="All" />
<PackageReference Include="Microsoft.Build.Locator" PrivateAssets="All" />
<PackageReference Include="System.CommandLine" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Newtonsoft.Json" />
</ItemGroup>
<ItemGroup>
Expand Down
21 changes: 3 additions & 18 deletions src/Workspaces/Core/MSBuild.BuildHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
using System.Collections.Immutable;
using System.CommandLine;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.MSBuild.Rpc;
using Microsoft.Extensions.Logging;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost;
namespace Microsoft.CodeAnalysis.MSBuild;

internal static class Program
{
Expand All @@ -31,26 +29,13 @@ internal static async Task Main(string[] args)
propertiesBuilder.Add(propertyParts[0], propertyParts[1]);
}

// Create a console logger that logs everything to standard error instead of standard out; by setting the threshold to Trace
// everything will go to standard error.
var loggerFactory = LoggerFactory.Create(builder =>
builder.AddConsole(configure =>
{
// DisableColors is deprecated in favor of us moving to simple console, but that loses the LogToStandardErrorThreshold
// which we also need
#pragma warning disable CS0618
configure.DisableColors = true;
#pragma warning restore CS0618
configure.LogToStandardErrorThreshold = LogLevel.Trace;
}));

var logger = loggerFactory.CreateLogger(typeof(Program));
var logger = new BuildHostLogger(Console.Error);

logger.LogInformation($"BuildHost Runtime Version: {System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription}");

var server = new RpcServer(sendingStream: Console.OpenStandardOutput(), receivingStream: Console.OpenStandardInput());

var targetObject = server.AddTarget(new BuildHost(loggerFactory, propertiesBuilder.ToImmutable(), binaryLogPath, server));
var targetObject = server.AddTarget(new BuildHost(logger, propertiesBuilder.ToImmutable(), binaryLogPath, server));
Contract.ThrowIfFalse(targetObject == 0, "The first object registered should have target 0, which is assumed by the client.");

await server.RunAsync().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System;
using System.Runtime.Serialization;

namespace Microsoft.CodeAnalysis.MSBuild.Logging
namespace Microsoft.CodeAnalysis.MSBuild
{
[DataContract]
internal class DiagnosticLogItem
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.CodeAnalysis.MSBuild;

/// <summary>
/// RPC methods.
/// </summary>
internal interface IBuildHost
{
bool HasUsableMSBuild(string projectOrSolutionFilePath);
ImmutableArray<(string ProjectPath, string ProjectGuid)> GetProjectsInSolution(string solutionFilePath);
Task<int> LoadProjectFileAsync(string projectFilePath, string languageName, CancellationToken cancellationToken);
Task<string?> TryGetProjectOutputPathAsync(string projectFilePath, CancellationToken cancellationToken);
Task ShutdownAsync();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.CodeAnalysis.MSBuild;

/// <summary>
/// RPC methods.
/// </summary>
internal interface IProjectFile
{
ImmutableArray<DiagnosticLogItem> GetDiagnosticLogItems();
string GetDocumentExtension(SourceCodeKind kind);
Task<ImmutableArray<ProjectFileInfo>> GetProjectFileInfosAsync(CancellationToken cancellationToken);
void AddDocument(string filePath, string? logicalPath);
void RemoveDocument(string filePath);
void AddMetadataReference(string metadataReferenceIdentity, MetadataReferenceProperties properties, string? hintPath);
void RemoveMetadataReference(string shortAssemblyName, string fullAssemblyName, string filePath);
void AddProjectReference(string projectName, ProjectFileReference reference);
void RemoveProjectReference(string projectName, string projectFilePath);
void AddAnalyzerReference(string fullPath);
void RemoveAnalyzerReference(string fullPath);
void Save();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Microsoft.CodeAnalysis.MSBuild.Rpc;
namespace Microsoft.CodeAnalysis.MSBuild;

internal static class JsonSettings
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Runtime.InteropServices;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost;
namespace Microsoft.CodeAnalysis.MSBuild;

internal static class MonoMSBuildDiscovery
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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 System.Collections.Immutable;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace Microsoft.CodeAnalysis.MSBuild;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Diagnostics;
using System.Runtime.Serialization;
using Microsoft.CodeAnalysis.MSBuild.Logging;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.MSBuild
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal sealed class ProjectFileReference
public ImmutableArray<string> Aliases { get; }

/// <summary>
/// The value of <see cref="MetadataNames.ReferenceOutputAssembly"/>.
/// The value of "ReferenceOutputAssembly" metadata.
/// </summary>
[DataMember(Order = 2)]
public bool ReferenceOutputAssembly { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Collections.Immutable;
using Newtonsoft.Json.Linq;

namespace Microsoft.CodeAnalysis.MSBuild.Rpc;
namespace Microsoft.CodeAnalysis.MSBuild;

internal sealed class Request
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using Newtonsoft.Json.Linq;

namespace Microsoft.CodeAnalysis.MSBuild.Rpc;
namespace Microsoft.CodeAnalysis.MSBuild;

internal sealed class Response
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.CodeAnalysis.MSBuild.Rpc;
namespace Microsoft.CodeAnalysis.MSBuild;

internal static class TextReaderExtensions
{
Expand Down
Loading
Loading