Skip to content

Commit

Permalink
Remove compile-time dependency from Workspace.MSBuild on Workspace.MS…
Browse files Browse the repository at this point in the history
…Build.BuildHost (#73393)

* Remove compile-time dependency from Workspace.MSBuild on Workspace.MSBuild.BuildHost

* Remove dependency on Microsoft.Extensions.Logging.Console from MSBuild.BuildHost

* Remove other dependencies on BuildHost

* Fix source build
  • Loading branch information
tmat authored May 8, 2024
1 parent 9c9420d commit e14e32a
Show file tree
Hide file tree
Showing 48 changed files with 131 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.LanguageServer.HostWorkspace.ProjectTelemetry;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.MSBuild.Logging;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.ProjectSystem;
using Microsoft.CodeAnalysis.Shared.TestHooks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@

<ItemGroup Label="Project References">
<ProjectReference Include="..\..\..\Workspaces\Core\MSBuild\Microsoft.CodeAnalysis.Workspaces.MSBuild.csproj" />
<ProjectReference Include="..\..\..\Workspaces\Core\MSBuild.BuildHost\Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.csproj" />
<ProjectReference Include="..\..\..\Workspaces\Remote\Core\Microsoft.CodeAnalysis.Remote.Workspaces.csproj" />
<ProjectReference Include="..\Protocol\Microsoft.CodeAnalysis.LanguageServer.Protocol.csproj" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,9 @@
<ItemGroup>
<ProjectReference Include="..\..\LanguageServer\Protocol\Microsoft.CodeAnalysis.LanguageServer.Protocol.csproj" />
<ProjectReference Include="..\..\..\Workspaces\Core\MSBuild\Microsoft.CodeAnalysis.Workspaces.MSBuild.csproj" />
<ProjectReference Include="..\..\..\Workspaces\Core\MSBuild.BuildHost\Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.csproj" />
<ProjectReference Include="..\..\..\Workspaces\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.Workspaces.csproj" />
<ProjectReference Include="..\..\..\Workspaces\VisualBasic\Portable\Microsoft.CodeAnalysis.VisualBasic.Workspaces.vbproj" />
<ProjectReference Include="..\..\Core\Portable\Microsoft.CodeAnalysis.Features.csproj" />

<!-- Below is the transitive closure of the project references above to placate BuildBoss. If changes are made above this line,
please update the stuff here accordingly. -->
<ProjectReference Include="..\..\..\Compilers\Core\Portable\Microsoft.CodeAnalysis.csproj" />
<ProjectReference Include="..\..\..\Compilers\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.csproj" />
<ProjectReference Include="..\..\..\Compilers\VisualBasic\Portable\Microsoft.CodeAnalysis.VisualBasic.vbproj" />
<ProjectReference Include="..\..\..\Workspaces\Core\Portable\Microsoft.CodeAnalysis.Workspaces.csproj" />
<ProjectReference Include="..\..\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.Features.csproj" />
<ProjectReference Include="..\..\VisualBasic\Portable\Microsoft.CodeAnalysis.VisualBasic.Features.vbproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
<ProjectReference Include="..\..\Workspaces\Core\Portable\Microsoft.CodeAnalysis.Workspaces.csproj" PrivateAssets="all" />
<ProjectReference Include="..\..\Workspaces\Core\Desktop\Microsoft.CodeAnalysis.Workspaces.Desktop.csproj" PrivateAssets="all" />
<ProjectReference Include="..\..\Workspaces\Core\MSBuild\Microsoft.CodeAnalysis.Workspaces.MSBuild.csproj" PrivateAssets="all" />
<ProjectReference Include="..\..\Workspaces\Core\MSBuild.BuildHost\Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.csproj" PrivateAssets="all" />
<ProjectReference Include="..\..\Workspaces\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.Workspaces.csproj" PrivateAssets="all" />
<ProjectReference Include="..\..\Workspaces\VisualBasic\Portable\Microsoft.CodeAnalysis.VisualBasic.Workspaces.vbproj" PrivateAssets="all" />
<ProjectReference Include="..\..\Workspaces\Remote\Core\Microsoft.CodeAnalysis.Remote.Workspaces.csproj" PrivateAssets="all" />
Expand Down
1 change: 0 additions & 1 deletion src/Tools/AnalyzerRunner/AnalyzerRunner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<ProjectReference Include="..\..\Scripting\Core\Microsoft.CodeAnalysis.Scripting.csproj" />
<ProjectReference Include="..\..\Workspaces\Core\Portable\Microsoft.CodeAnalysis.Workspaces.csproj" />
<ProjectReference Include="..\..\Workspaces\Core\MSBuild\Microsoft.CodeAnalysis.Workspaces.MSBuild.csproj" />
<ProjectReference Include="..\..\Workspaces\Core\MSBuild.BuildHost\Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.csproj" />
<ProjectReference Include="..\..\Workspaces\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.Workspaces.csproj" />
<ProjectReference Include="..\..\Workspaces\VisualBasic\Portable\Microsoft.CodeAnalysis.VisualBasic.Workspaces.vbproj" />
<ProjectReference Include="..\..\Features\Core\Portable\Microsoft.CodeAnalysis.Features.csproj" />
Expand Down
11 changes: 0 additions & 11 deletions src/VisualStudio/Setup/Roslyn.VisualStudio.Setup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,6 @@
<AdditionalProperties>TargetFramework=net472</AdditionalProperties>
<PkgDefEntry>BindingRedirect</PkgDefEntry>
</ProjectReference>
<ProjectReference Include="..\..\Workspaces\Core\MSBuild.BuildHost\Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.csproj">
<Name>Workspaces.MSBuild</Name>
<IncludeOutputGroupsInVSIX>BuiltProjectOutputGroup;SatelliteDllsProjectOutputGroup</IncludeOutputGroupsInVSIX>
<ForceIncludeInVSIX>true</ForceIncludeInVSIX>
<AdditionalProperties>TargetFramework=net472</AdditionalProperties>
<PkgDefEntry>BindingRedirect</PkgDefEntry>
<!-- Don't ngen the BuildHost executable. We don't expect it to be used in Visual Studio scenarios, except for third-party extensions that might be using it.
At the moment, it's consumed not as a running executable but rather as a library, and it also has some dependencies for the executable portion, like
command line parsing and MSBuildLocator that we don't expect to be shipping either. -->
<Ngen>false</Ngen>
</ProjectReference>
<ProjectReference Include="..\..\Workspaces\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.Workspaces.csproj">
<Name>CSharpWorkspace</Name>
<IncludeOutputGroupsInVSIX>BuiltProjectOutputGroup;SatelliteDllsProjectOutputGroup</IncludeOutputGroupsInVSIX>
Expand Down
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
21 changes: 8 additions & 13 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 @@ -109,7 +104,7 @@ private bool TryEnsureMSBuildLoaded(string projectOrSolutionFilePath)
#if NET472 || NET6_0 // If we're compiling against net472 or net6.0, we get our MemberNotNull from the workspaces assembly. It has it in the net6.0 case since we're consuming the netstandard2.0 version of Workspaces.
[workspaces::System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_buildManager))]
#else // If we're compiling against net7.0 or higher, then we're getting it staright from the framework.
[MemberNotNull(nameof(_buildManager))]
[System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_buildManager))]
#endif
[MethodImpl(MethodImplOptions.NoInlining)] // Do not inline this, since this creates MSBuild types which are being loaded by the caller
private void CreateBuildManager()
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 All @@ -40,8 +39,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\CompilerFeatureRequiredAttribute.cs" Link="Utilities\CompilerFeatureRequiredAttribute.cs" />
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\IsExternalInit.cs" Link="Utilities\IsExternalInit.cs" Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'" />
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\RequiredMemberAttribute.cs" Link="Utilities\RequiredMemberAttribute.cs" />
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\IsExternalInit.cs" Link="Utilities\IsExternalInit.cs" Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'" />
</ItemGroup>
<ItemGroup>
<PublicAPI Include="PublicAPI.Shipped.txt" />
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
21 changes: 21 additions & 0 deletions src/Workspaces/Core/MSBuild.BuildHost/Rpc/Contracts/IBuildHost.cs
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();
}
Loading

0 comments on commit e14e32a

Please sign in to comment.