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 for configurations remapping in solution files #1835

Merged
merged 16 commits into from
Sep 1, 2020
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
4 changes: 2 additions & 2 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static (ProjectFileInfo, ImmutableArray<MSBuildDiagnostic>, ProjectLoaded
return (null, ImmutableArray<MSBuildDiagnostic>.Empty, null);
}

var (projectInstance, diagnostics) = loader.BuildProject(filePath);
var (projectInstance, diagnostics) = loader.BuildProject(filePath, projectIdInfo?.ConfigurationsInSolution);
if (projectInstance == null)
{
return (null, diagnostics, null);
Expand All @@ -135,7 +135,7 @@ public static (ProjectFileInfo, ImmutableArray<MSBuildDiagnostic>, ProjectLoaded

public (ProjectFileInfo, ImmutableArray<MSBuildDiagnostic>, ProjectLoadedEventArgs) Reload(ProjectLoader loader)
{
var (projectInstance, diagnostics) = loader.BuildProject(FilePath);
var (projectInstance, diagnostics) = loader.BuildProject(FilePath, ProjectIdInfo?.ConfigurationsInSolution);
if (projectInstance == null)
{
return (null, diagnostics, null);
Expand Down
2 changes: 2 additions & 0 deletions src/OmniSharp.MSBuild/ProjectIdInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;

namespace OmniSharp.MSBuild
Expand All @@ -13,5 +14,6 @@ public ProjectIdInfo(ProjectId id, bool isDefinedInSolution)

public ProjectId Id { get; set; }
public bool IsDefinedInSolution { get; set; }
public Dictionary<string, string> ConfigurationsInSolution { get; set; }
deitry marked this conversation as resolved.
Show resolved Hide resolved
}
}
36 changes: 32 additions & 4 deletions src/OmniSharp.MSBuild/ProjectLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ private static Dictionary<string, string> CreateGlobalProperties(
return globalProperties;
}

public (MSB.Execution.ProjectInstance projectInstance, ImmutableArray<MSBuildDiagnostic> diagnostics) BuildProject(string filePath)
public (MSB.Execution.ProjectInstance projectInstance, ImmutableArray<MSBuildDiagnostic> diagnostics) BuildProject(
string filePath, Dictionary<string, string> configurationsInSolution)
{
deitry marked this conversation as resolved.
Show resolved Hide resolved
using (_sdksPathResolver.SetSdksPathEnvironmentVariable(filePath))
{
var evaluatedProject = EvaluateProjectFileCore(filePath);
var evaluatedProject = EvaluateProjectFileCore(filePath, configurationsInSolution);

SetTargetFrameworkIfNeeded(evaluatedProject);

Expand Down Expand Up @@ -115,10 +116,37 @@ public MSB.Evaluation.Project EvaluateProjectFile(string filePath)
}
}

private MSB.Evaluation.Project EvaluateProjectFileCore(string filePath)
private MSB.Evaluation.Project EvaluateProjectFileCore(string filePath, Dictionary<string, string> projectConfigurationsInSolution = null)
{
var localProperties = new Dictionary<string, string>(_globalProperties);
if (projectConfigurationsInSolution != null
&& localProperties.TryGetValue(PropertyNames.Configuration, out string solutionConfiguration))
{
if (!localProperties.TryGetValue(PropertyNames.Platform, out string solutionPlatform))
{
solutionPlatform = "Any CPU";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there are any other alternate values for Any CPU we should be aware of?

}

var solutionSelector = $"{solutionConfiguration}|{solutionPlatform}.ActiveCfg";
_logger.LogDebug($"Found configuration `{solutionSelector}` in solution for '{filePath}'.");

if (projectConfigurationsInSolution.TryGetValue(solutionSelector, out string projectSelector))
{
var splitted = projectSelector.Split('|');
if (splitted.Length == 2)
{
var projectConfiguration = splitted[0];
localProperties[PropertyNames.Configuration] = projectConfiguration;
// NOTE: Solution often defines configuration as `Any CPU` wheras project relies on `AnyCPU`
var projectPlatform = splitted[1].Replace("Any CPU", "AnyCPU");
localProperties[PropertyNames.Platform] = projectPlatform;
_logger.LogInformation($"Using configuration from solution: `{projectConfiguration}|{projectPlatform}`");
}
}
}

// Evaluate the MSBuild project
var projectCollection = new MSB.Evaluation.ProjectCollection(_globalProperties);
var projectCollection = new MSB.Evaluation.ProjectCollection(localProperties);

var toolsVersion = _options.ToolsVersion;
if (string.IsNullOrEmpty(toolsVersion) || Version.TryParse(toolsVersion, out _))
Expand Down
26 changes: 26 additions & 0 deletions src/OmniSharp.MSBuild/ProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,28 @@ public void Initalize(IConfiguration configuration)
var processedProjects = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var result = new List<(string, ProjectIdInfo)>();

var projectConfigurationsInSln = new Dictionary<ProjectId, Dictionary<string, string>>();
foreach (var globalSection in solutionFile.GlobalSections)
{
if (globalSection.Name == "ProjectConfigurationPlatforms")
{
_logger.LogDebug($"Parsing ProjectConfigurationPlatforms of '{solutionFilePath}'.");
foreach (var entry in globalSection.Properties)
{
var guid = Guid.Parse(entry.Name.Substring(0, 38));
var projId = ProjectId.CreateFromSerialized(guid);
var solutionConfig = entry.Name.Substring(39);

if (!projectConfigurationsInSln.TryGetValue(projId, out var dict))
{
dict = new Dictionary<string, string>();
projectConfigurationsInSln.Add(projId, dict);
}
dict.Add(solutionConfig, entry.Value);
}
}
}

foreach (var project in solutionFile.Projects)
{
if (project.IsNotSupported)
Expand All @@ -182,6 +204,10 @@ public void Initalize(IConfiguration configuration)
if (string.Equals(Path.GetExtension(projectFilePath), ".csproj", StringComparison.OrdinalIgnoreCase))
{
var projectIdInfo = new ProjectIdInfo(ProjectId.CreateFromSerialized(new Guid(project.ProjectGuid)), true);
if (projectConfigurationsInSln.TryGetValue(projectIdInfo.Id, out var configurations))
{
projectIdInfo.ConfigurationsInSolution = configurations;
}
result.Add((projectFilePath, projectIdInfo));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\Lib\Lib.csproj" />
</ItemGroup>

<PropertyGroup>
<Configurations>Debug1;Release1</Configurations>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release1|AnyCPU'">
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug1|AnyCPU'">
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace App
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Lib
{
public class Class1
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Configurations>Debug2;Release2</Configurations>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release2|AnyCPU'">
<TargetFramework>netstandard1.3</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug2|AnyCPU'">
<TargetFramework>netstandard1.3</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "App\App.csproj", "{632DFE45-B56E-4158-8F27-45E2BA0BAFCF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lib", "Lib\Lib.csproj", "{CE41561B-5D13-4688-8686-EEFF744BE8B5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
DebugSln|Any CPU = DebugSln|Any CPU
ReleaseSln|Any CPU = ReleaseSln|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{632DFE45-B56E-4158-8F27-45E2BA0BAFCF}.DebugSln|Any CPU.ActiveCfg = Debug1|Any CPU
{632DFE45-B56E-4158-8F27-45E2BA0BAFCF}.DebugSln|Any CPU.Build.0 = Debug1|Any CPU
{632DFE45-B56E-4158-8F27-45E2BA0BAFCF}.ReleaseSln|Any CPU.ActiveCfg = Release1|Any CPU
{632DFE45-B56E-4158-8F27-45E2BA0BAFCF}.ReleaseSln|Any CPU.Build.0 = Release1|Any CPU
{CE41561B-5D13-4688-8686-EEFF744BE8B5}.DebugSln|Any CPU.ActiveCfg = Debug2|Any CPU
{CE41561B-5D13-4688-8686-EEFF744BE8B5}.DebugSln|Any CPU.Build.0 = Debug2|Any CPU
{CE41561B-5D13-4688-8686-EEFF744BE8B5}.ReleaseSln|Any CPU.ActiveCfg = Debug2|Any CPU
{CE41561B-5D13-4688-8686-EEFF744BE8B5}.ReleaseSln|Any CPU.Build.0 = Debug2|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D5E71A6E-0C92-4CB1-8260-00DE6C30724F}
EndGlobalSection
EndGlobal
25 changes: 25 additions & 0 deletions tests/OmniSharp.MSBuild.Tests/WorkspaceInformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,31 @@ public async Task TwoProjectsWithSolution()
}
}

[Fact]
public async Task TwoProjectsWithSolutionAndCustomConfigurations()
{
var configData = new Dictionary<string, string> { [$"MsBuild:{nameof(Options.MSBuildOptions.Configuration)}"] = "ReleaseSln" };
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("TwoProjectsWithSolutionAndCustomConfigurations"))
using (var host = CreateMSBuildTestHost(testProject.Directory, configurationData: configData))
{
var workspaceInfo = await host.RequestMSBuildWorkspaceInfoAsync();

Assert.Equal("TwoProjectsWithSolutionAndCustomConfigurations.sln", Path.GetFileName(workspaceInfo.SolutionPath));
Assert.NotNull(workspaceInfo.Projects);
Assert.Equal(2, workspaceInfo.Projects.Count);

var firstProject = workspaceInfo.Projects[0];
Assert.Equal("App.csproj", Path.GetFileName(firstProject.Path));
Assert.Equal(".NETCoreApp,Version=v2.1", firstProject.TargetFramework);
Assert.Equal("netcoreapp2.1", firstProject.TargetFrameworks[0].ShortName);

var secondProject = workspaceInfo.Projects[1];
Assert.Equal("Lib.csproj", Path.GetFileName(secondProject.Path));
Assert.Equal(".NETStandard,Version=v1.3", secondProject.TargetFramework);
Assert.Equal("netstandard1.3", secondProject.TargetFrameworks[0].ShortName);
}
}

[Fact]
public async Task TwoProjectWithGeneratedFile()
{
Expand Down