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

Make Workspaces.MSBuild build with a netstandard target #76832

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 12 additions & 3 deletions Roslyn.sln
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ExternalAccess", "ExternalA
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.LanguageServer.Protocol.Test.Utilities", "src\LanguageServer\Protocol.TestUtilities\Microsoft.CodeAnalysis.LanguageServer.Protocol.Test.Utilities.csproj", "{7465CE63-A7B7-475F-8C57-48D2F8BC665A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MSBuild", "MSBuild", "{CCEB94EC-E0BB-4A6E-861A-CAD7E9F082F8}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "WorkspacesMSBuildShared", "src\Workspaces\MSBuild\Shared\WorkspacesMSBuildShared.shproj", "{01A9F592-43BD-4B0D-949B-06DFA1A79599}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1449,7 +1453,7 @@ Global
{E3FDC65F-568D-4E2D-A093-5132FD3793B7} = {55A62CFA-1155-46F1-ADF3-BEEE51B58AB5}
{909B656F-6095-4AC2-A5AB-C3F032315C45} = {FD0FAF5F-1DED-485C-99FA-84B97F3A8EEC}
{2E87FA96-50BB-4607-8676-46521599F998} = {55A62CFA-1155-46F1-ADF3-BEEE51B58AB5}
{96EB2D3B-F694-48C6-A284-67382841E086} = {55A62CFA-1155-46F1-ADF3-BEEE51B58AB5}
{96EB2D3B-F694-48C6-A284-67382841E086} = {CCEB94EC-E0BB-4A6E-861A-CAD7E9F082F8}
{21B239D0-D144-430F-A394-C066D58EE267} = {55A62CFA-1155-46F1-ADF3-BEEE51B58AB5}
{57CA988D-F010-4BF2-9A2E-07D6DCD2FF2C} = {55A62CFA-1155-46F1-ADF3-BEEE51B58AB5}
{1A3941F1-1E1F-4EF7-8064-7729C4C2E2AA} = {FD0FAF5F-1DED-485C-99FA-84B97F3A8EEC}
Expand Down Expand Up @@ -1543,7 +1547,7 @@ Global
{60166C60-813C-46C4-911D-2411B4ABBC0F} = {FD0FAF5F-1DED-485C-99FA-84B97F3A8EEC}
{FC2AE90B-2E4B-4045-9FDD-73D4F5ED6C89} = {C2D1346B-9665-4150-B644-075CF1636BAA}
{49E7C367-181B-499C-AC2E-8E17C81418D6} = {C2D1346B-9665-4150-B644-075CF1636BAA}
{037F06F0-3BE8-42D0-801E-2F74FC380AB8} = {55A62CFA-1155-46F1-ADF3-BEEE51B58AB5}
{037F06F0-3BE8-42D0-801E-2F74FC380AB8} = {CCEB94EC-E0BB-4A6E-861A-CAD7E9F082F8}
{2F11618A-9251-4609-B3D5-CE4D2B3D3E49} = {5CA5F70E-0FDB-467B-B22C-3CD5994F0087}
{CFA94A39-4805-456D-A369-FC35CCC170E9} = {C52D8057-43AF-40E6-A01B-6CDBB7301985}
{C52D8057-43AF-40E6-A01B-6CDBB7301985} = {3F40F71B-7DCF-44A1-B15C-38CA34824143}
Expand Down Expand Up @@ -1643,7 +1647,7 @@ Global
{09E88382-0D7B-4A15-B1AF-0B89A5B67227} = {8DBA5174-B0AA-4561-82B1-A46607697753}
{A833B11C-5072-4A1F-A32B-2700433B0D3D} = {806F0C6F-3640-4C92-8D55-6767B1535467}
{8988270E-393A-4B92-AC1A-534F903CFD34} = {8977A560-45C2-4EC2-A849-97335B382C74}
{B1481D94-682E-46EC-ADBE-A16EB46FEEE9} = {55A62CFA-1155-46F1-ADF3-BEEE51B58AB5}
{B1481D94-682E-46EC-ADBE-A16EB46FEEE9} = {CCEB94EC-E0BB-4A6E-861A-CAD7E9F082F8}
{5E8FB6D6-6C5C-42E6-9220-1EAA7ED9BCAD} = {1AE9182D-B03E-4B00-B32E-37AE01715F57}
{09AEDEE4-6358-47C9-8022-3BD37A518070} = {5880FECB-91F1-4AB8-8726-75EAFA8A918E}
{5BABC440-4F1B-46E8-9068-DD7F02ED25D3} = {3E5FE3DB-45F7-4D83-9097-8F05D3B3AEC6}
Expand All @@ -1661,6 +1665,8 @@ Global
{1AE9182D-B03E-4B00-B32E-37AE01715F57} = {EE97CB90-33BB-4F3A-9B3D-69375DEC6AC6}
{806F0C6F-3640-4C92-8D55-6767B1535467} = {D449D505-CC6A-4E0B-AF1B-976E2D0AE67A}
{7465CE63-A7B7-475F-8C57-48D2F8BC665A} = {D449D505-CC6A-4E0B-AF1B-976E2D0AE67A}
{CCEB94EC-E0BB-4A6E-861A-CAD7E9F082F8} = {55A62CFA-1155-46F1-ADF3-BEEE51B58AB5}
{01A9F592-43BD-4B0D-949B-06DFA1A79599} = {CCEB94EC-E0BB-4A6E-861A-CAD7E9F082F8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {604E6B91-7BC0-4126-AE07-D4D2FEFC3D29}
Expand All @@ -1669,6 +1675,7 @@ Global
src\Analyzers\VisualBasic\CodeFixes\VisualBasicCodeFixes.projitems*{0141285d-8f6c-42c7-baf3-3c0ccd61c716}*SharedItemsImports = 5
src\Workspaces\SharedUtilitiesAndExtensions\Workspace\VisualBasic\VisualBasicWorkspaceExtensions.projitems*{0141285d-8f6c-42c7-baf3-3c0ccd61c716}*SharedItemsImports = 5
src\Compilers\CSharp\csc\CscCommandLine.projitems*{0161e25c-918a-4dc8-9648-30fdcc8e31e9}*SharedItemsImports = 5
src\Workspaces\MSBuild\Shared\WorkspacesMSBuildShared.projitems*{01a9f592-43bd-4b0d-949b-06dfa1a79599}*SharedItemsImports = 13
src\Analyzers\Core\CodeFixes\CodeFixes.projitems*{1b6c4a1a-413b-41fb-9f85-5c09118e541b}*SharedItemsImports = 13
src\Compilers\Core\AnalyzerDriver\AnalyzerDriver.projitems*{1ee8cad3-55f9-4d91-96b2-084641da9a6c}*SharedItemsImports = 5
src\Dependencies\CodeAnalysis.Debugging\Microsoft.CodeAnalysis.Debugging.projitems*{1ee8cad3-55f9-4d91-96b2-084641da9a6c}*SharedItemsImports = 5
Expand Down Expand Up @@ -1711,6 +1718,7 @@ Global
src\Compilers\VisualBasic\vbc\VbcCommandLine.projitems*{810b02ad-2ea5-4422-88ac-b71b8ab0df0b}*SharedItemsImports = 13
src\Analyzers\VisualBasic\Analyzers\VisualBasicAnalyzers.projitems*{94faf461-2e74-4dbb-9813-6b2cde6f1880}*SharedItemsImports = 13
src\Compilers\Server\VBCSCompiler\VBCSCompilerCommandLine.projitems*{9508f118-f62e-4c16-a6f4-7c3b56e166ad}*SharedItemsImports = 5
src\Workspaces\MSBuild\Shared\WorkspacesMSBuildShared.projitems*{96eb2d3b-f694-48c6-a284-67382841e086}*SharedItemsImports = 5
src\Compilers\VisualBasic\vbc\VbcCommandLine.projitems*{975cd834-45f4-4ea0-a395-cb60dbd0e214}*SharedItemsImports = 5
src\Workspaces\SharedUtilitiesAndExtensions\Workspace\Core\WorkspaceExtensions.projitems*{99f594b1-3916-471d-a761-a6731fc50e9a}*SharedItemsImports = 13
src\Analyzers\VisualBasic\CodeFixes\VisualBasicCodeFixes.projitems*{9f9ccc78-7487-4127-9d46-db23e501f001}*SharedItemsImports = 13
Expand All @@ -1726,6 +1734,7 @@ Global
src\Compilers\CSharp\csc\CscCommandLine.projitems*{b021ccbc-b2af-4560-af28-ed055f0ed696}*SharedItemsImports = 13
src\Dependencies\Collections\Microsoft.CodeAnalysis.Collections.projitems*{b1481d94-682e-46ec-adbe-a16eb46feee9}*SharedItemsImports = 5
src\Dependencies\PooledObjects\Microsoft.CodeAnalysis.PooledObjects.projitems*{b1481d94-682e-46ec-adbe-a16eb46feee9}*SharedItemsImports = 5
src\Workspaces\MSBuild\Shared\WorkspacesMSBuildShared.projitems*{b1481d94-682e-46ec-adbe-a16eb46feee9}*SharedItemsImports = 5
src\Compilers\CSharp\CSharpAnalyzerDriver\CSharpAnalyzerDriver.projitems*{b501a547-c911-4a05-ac6e-274a50dff30e}*SharedItemsImports = 5
src\ExpressionEvaluator\Core\Source\ResultProvider\ResultProvider.projitems*{bb3ca047-5d00-48d4-b7d3-233c1265c065}*SharedItemsImports = 13
src\ExpressionEvaluator\CSharp\Source\ResultProvider\CSharpResultProvider.projitems*{bf9dac1e-3a5e-4dc3-bb44-9a64e0d4e9d4}*SharedItemsImports = 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,11 @@ public async Task OpenSolutionAsync(string solutionFilePath)
_logger.LogInformation(string.Format(LanguageServerResources.Loading_0, solutionFilePath));
_workspaceFactory.ProjectSystemProjectFactory.SolutionPath = solutionFilePath;

// We'll load solutions out-of-proc, since it's possible we might be running on a runtime that doesn't have a matching SDK installed,
// and we don't want any MSBuild registration to set environment variables in our process that might impact child processes.
// We'll load solutions out-of-proc, since it's possible we might be running on a runtime that doesn't have a matching SDK installed and
// we aren't deploying MSBuild with our server. We wouldn't want to use MSBuildLocator anyways in case that sets environment variables that
// cause problems for child processes.
await using var buildHostProcessManager = new BuildHostProcessManager(globalMSBuildProperties: AdditionalProperties, loggerFactory: _loggerFactory);
var buildHost = await buildHostProcessManager.GetBuildHostAsync(BuildHostProcessKind.NetCore, CancellationToken.None);

// If we don't have a .NET Core SDK on this machine at all, try .NET Framework
if (!await buildHost.HasUsableMSBuildAsync(solutionFilePath, CancellationToken.None))
{
var kind = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? BuildHostProcessKind.NetFramework : BuildHostProcessKind.Mono;
buildHost = await buildHostProcessManager.GetBuildHostAsync(kind, CancellationToken.None);
}
var (buildHost, _) = await buildHostProcessManager.GetBuildHostWithFallbackAsync(GetKindForCurrentProcess(), solutionFilePath, CancellationToken.None);

foreach (var project in await buildHost.GetProjectsInSolutionAsync(solutionFilePath, CancellationToken.None))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.VisualBasic.Features\$(Configuration)\netstandard2.0\Microsoft.CodeAnalysis.VisualBasic.Features.dll" TargetDir="" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.VisualBasic.Workspaces\$(Configuration)\netstandard2.0\Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll" TargetDir="" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.VisualBasic\$(Configuration)\netstandard2.0\Microsoft.CodeAnalysis.VisualBasic.dll" TargetDir="" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.Workspaces.MSBuild\$(Configuration)\net472\Microsoft.CodeAnalysis.Workspaces.MSBuild.dll" TargetDir="" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost\$(Configuration)\net472\Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.exe" TargetDir="" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.Workspaces\$(Configuration)\netstandard2.0\Microsoft.CodeAnalysis.Workspaces.dll" TargetDir="" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.Workspaces.Desktop\$(Configuration)\net472\Microsoft.CodeAnalysis.Workspaces.Desktop.dll" TargetDir="" />
<_File Include="$(ArtifactsBinDir)Microsoft.CodeAnalysis.Workspaces.MSBuild\$(Configuration)\netstandard2.0\Microsoft.CodeAnalysis.Workspaces.MSBuild.dll" TargetDir="" />
<_File Include="$(ArtifactsBinDir)Microsoft.VisualStudio.LanguageServices.CSharp\$(Configuration)\net472\Microsoft.VisualStudio.LanguageServices.CSharp.dll" TargetDir="" />
<_File Include="$(ArtifactsBinDir)Microsoft.VisualStudio.LanguageServices.Implementation\$(Configuration)\net472\Microsoft.VisualStudio.LanguageServices.Implementation.dll" TargetDir="" />
<_File Include="$(ArtifactsBinDir)Microsoft.VisualStudio.LanguageServices.LiveShare\$(Configuration)\net472\Microsoft.VisualStudio.LanguageServices.LiveShare.dll" TargetDir="" />
Expand Down
1 change: 0 additions & 1 deletion src/VisualStudio/Setup/Roslyn.VisualStudio.Setup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@
<Name>Workspaces.MSBuild</Name>
<IncludeOutputGroupsInVSIX>BuiltProjectOutputGroup;SatelliteDllsProjectOutputGroup</IncludeOutputGroupsInVSIX>
<ForceIncludeInVSIX>true</ForceIncludeInVSIX>
<AdditionalProperties>TargetFramework=net472</AdditionalProperties>
<PkgDefEntry>BindingRedirect</PkgDefEntry>
</ProjectReference>
<ProjectReference Include="..\..\Workspaces\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.Workspaces.csproj">
Expand Down
14 changes: 13 additions & 1 deletion src/Workspaces/MSBuild/BuildHost/BuildHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand Down Expand Up @@ -145,11 +146,22 @@ private void EnsureMSBuildLoaded(string projectFilePath)
// WARNING: do not use a lambda in this function, as it internally will be put in a class that contains other lambdas used in
// TryEnsureMSBuildLoaded; on Mono this causes type load errors.

var projectFilter = ImmutableHashSet<string>.Empty;
if (SolutionFilterReader.IsSolutionFilterFilename(solutionFilePath))
(solutionFilePath, projectFilter) = SolutionFilterReader.Read(solutionFilePath);

var builder = ImmutableArray.CreateBuilder<(string ProjectPath, string ProjectGuid)>();

foreach (var project in SolutionFile.Parse(solutionFilePath).ProjectsInOrder)
{
if (project.ProjectType != SolutionProjectType.SolutionFolder)
if (project.ProjectType == SolutionProjectType.SolutionFolder)
{
continue;
}

// Load project if we have an empty project filter and the project path is present.
if (projectFilter.IsEmpty ||
projectFilter.Contains(project.AbsolutePath))
{
builder.Add((project.AbsolutePath, project.ProjectGuid));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<OutputType>Exe</OutputType>
<RootNamespace>Microsoft.CodeAnalysis</RootNamespace>
<TargetFrameworks>$(NetRoslynBuildHostNetCoreVersion);net472</TargetFrameworks>
<!-- We'll always be running our build host with the same host that is used to launch the language server process directly, so we don't need to create another one -->
<UseAppHost>false</UseAppHost>
<!-- Set to false since it's also set in Microsoft.CodeAnalysis.LanguageServer -->
<SelfContained>false</SelfContained>
<!-- We don't ship a regular NuGet package for this (it gets included in the Workspaces.MSBuild package directly), but we still need to publish symbols -->
Expand Down Expand Up @@ -47,6 +45,7 @@
<PackageReference Include="System.Collections.Immutable" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Microsoft.IO.Redist" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
<PackageReference Include="System.Text.Json" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
<PackageReference Include="System.Threading.Tasks.Extensions" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -83,18 +82,15 @@
<PublicAPI Include="PublicAPI.Shipped.txt" />
<PublicAPI Include="PublicAPI.Unshipped.txt" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.LanguageServer" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.Workspaces.MSBuild.UnitTests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" Key="$(MoqPublicKey)" LoadsWithinVisualStudio="false" />
</ItemGroup>

<ItemGroup>
<None Include="Rpc\Readme.md" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="WorkspaceMSBuildBuildHostResources.resx" GenerateSource="true" />
</ItemGroup>
Expand All @@ -113,4 +109,5 @@
<Import Project="..\..\..\Dependencies\PooledObjects\Microsoft.CodeAnalysis.PooledObjects.projitems" Label="Shared" />
<Import Project="..\..\..\Dependencies\Collections\Microsoft.CodeAnalysis.Collections.projitems" Label="Shared" />
<Import Project="$(RepositoryEngineeringDir)targets\PackageDownloadAndReference.targets"/>
<Import Project="..\Shared\WorkspacesMSBuildShared.projitems" Label="Shared" />
</Project>
64 changes: 64 additions & 0 deletions src/Workspaces/MSBuild/BuildHost/SolutionFilterReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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;
using System.Collections.Immutable;
using System.IO;
using System.Text.Json;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.MSBuild;

internal static class SolutionFilterReader
{
public static bool IsSolutionFilterFilename(string filename)
{
return Path.GetExtension(filename).Equals(".slnf", StringComparison.OrdinalIgnoreCase);
}

private static string GetAbsolutePath(string path, string baseDirectory)
=> FileUtilities.NormalizeAbsolutePath(FileUtilities.ResolveRelativePath(path, baseDirectory) ?? path);

public static (string solutionFileName, ImmutableHashSet<string> projects) Read(string filterFilename)
{
using var document = JsonDocument.Parse(File.ReadAllText(filterFilename));
var solution = document.RootElement.GetProperty("solution");
// Convert directory separators to the platform's default, since that is what MSBuild provide us.
var solutionPath = solution.GetProperty("path").GetString()?.Replace('\\', Path.DirectorySeparatorChar);
if (solutionPath is null)
throw new Exception("No solution path found in the solution filter file.");

if (Path.GetDirectoryName(filterFilename) is not string baseDirectory)
throw new Exception("No directory could be found containing the solution filter.");

var solutionFilename = GetAbsolutePath(solutionPath, baseDirectory);

if (!File.Exists(solutionFilename))
throw new Exception($"The solution file '{solutionFilename}' does not exist.");

// The base directory for projects is the solution folder.
baseDirectory = Path.GetDirectoryName(solutionFilename)!;
RoslynDebug.AssertNotNull(baseDirectory);

var filterProjects = ImmutableHashSet.CreateBuilder<string>(StringComparer.OrdinalIgnoreCase);
foreach (var project in solution.GetProperty("projects").EnumerateArray())
{
// Convert directory separators to the platform's default, since that is what MSBuild provide us.
var projectPath = project.GetString()?.Replace('\\', Path.DirectorySeparatorChar);
if (projectPath is null)
{
continue;
}

// Fill the filter with the absolute project paths.
var absoluteProjectPath = GetAbsolutePath(projectPath, baseDirectory);
if (File.Exists(absoluteProjectPath))
{
filterProjects.Add(absoluteProjectPath);
}
}

return (solutionFilename, filterProjects.ToImmutable());
}
}
Loading
Loading