Skip to content

Commit 150c6fa

Browse files
committed
Fix issue where ASP.NET tests or tools could run on Stage 0 Microsoft.NETCore.App shared framework
1 parent 544faef commit 150c6fa

File tree

5 files changed

+106
-6
lines changed

5 files changed

+106
-6
lines changed

Directory.Build.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@
4545
RuntimeFrameworkVersion="$(MicrosoftNETCoreAppRuntimePackageVersion)" />
4646
</ItemGroup>
4747

48+
<Import Project="src\Layout\redist\targets\BuildToolsetTasks.targets" Condition="'$(MSBuildProjectName)' != 'toolset-tasks'"/>
49+
50+
51+
4852
</Project>

src/Layout/redist/redist.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
<EnableNETAnalyzers>false</EnableNETAnalyzers>
1212
</PropertyGroup>
1313

14-
<Import Project="targets\BuildToolsetTasks.targets" />
14+
<!-- This is currently imported in Directory.Build.targets -->
15+
<!--<Import Project="targets\BuildToolsetTasks.targets" />-->
16+
1517
<Import Project="targets\GetRuntimeInformation.targets" />
1618
<Import Project="targets\Version.targets" />
1719
<Import Project="targets\BundledSdks.targets" />

src/Layout/redist/targets/BuildToolsetTasks.targets

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,28 @@
44
<TaskTargetFramework>$(SdkTargetFramework)</TaskTargetFramework>
55
<TaskTargetFramework Condition="'$(MSBuildRuntimeType)' != 'Core'">net472</TaskTargetFramework>
66

7-
<ToolsetTaskDll>$(ArtifactsDir)tasks\bin\toolset-tasks\$(Configuration)\$(TaskTargetFramework)\toolset-tasks.dll</ToolsetTaskDll>
7+
<!--<ToolsetTaskDll>$(ArtifactsDir)tasks\bin\toolset-tasks\$(Configuration)\$(TaskTargetFramework)\toolset-tasks.dll</ToolsetTaskDll>-->
8+
<ToolsetTaskDll>$(ArtifactsDir)bin\toolset-tasks\$(Configuration)\$(TaskTargetFramework)\toolset-tasks.dll</ToolsetTaskDll>
89
<ToolsetTaskProject>$(RepoRoot)src\Layout\toolset-tasks\toolset-tasks.csproj</ToolsetTaskProject>
910
</PropertyGroup>
10-
<Target Name="BuildCoreSdkTasks" BeforeTargets="_CheckForInvalidConfigurationAndPlatform">
11-
<!-- Use a different ArtifactsDir for this invocation so that the tasks project can be part of the solution
12-
(for easy editing), but we don't hit problems with the tasks DLL being locked when we try to build the solution. -->
11+
12+
<!-- Right now we're using these tasks in all projects, so we build with a ProjectReference. Once https://github.com/dotnet/sdk/pull/17982
13+
is merged and flows through to stage 0, we can delete the PatchRuntimeConfig target and go back to the way it was. -->
14+
<ItemGroup>
15+
<ProjectReference Include="$(ToolsetTaskProject)" ReferenceOutputAssembly="false" SkipGetTargetFrameworkProperties="true" SetTargetFramework="TargetFramework=$(TaskTargetFramework)" />
16+
</ItemGroup>
17+
18+
<!--<Target Name="BuildCoreSdkTasks" BeforeTargets="_CheckForInvalidConfigurationAndPlatform">
19+
--><!-- Use a different ArtifactsDir for this invocation so that the tasks project can be part of the solution
20+
(for easy editing), but we don't hit problems with the tasks DLL being locked when we try to build the solution. --><!--
1321
1422
<MSBuild Projects="$(ToolsetTaskProject)"
1523
Properties="ArtifactsDir=$(ArtifactsDir)tasks\;Phase=Restore"
1624
Targets="Restore"/>
1725
1826
<MSBuild Projects="$(ToolsetTaskProject)"
1927
Properties="ArtifactsDir=$(ArtifactsDir)tasks\"/>
20-
</Target>
28+
</Target>-->
2129

2230
<UsingTask TaskName="ReplaceFileContents" AssemblyFile="$(ToolsetTaskDll)" />
2331
<UsingTask TaskName="Chmod" AssemblyFile="$(ToolsetTaskDll)" />
@@ -26,5 +34,20 @@
2634
<UsingTask TaskName="GetCurrentRuntimeInformation" AssemblyFile="$(ToolsetTaskDll)"/>
2735
<UsingTask TaskName="ZipFileCreateFromDirectory" AssemblyFile="$(ToolsetTaskDll)"/>
2836
<UsingTask TaskName="OverrideAndCreateBundledNETCoreAppPackageVersion" AssemblyFile="$(ToolsetTaskDll)"/>
37+
<UsingTask TaskName="AddBaseFrameworkToRuntimeConfig" AssemblyFile="$(ToolsetTaskDll)"/>
38+
39+
<Target Name="PatchRuntimeConfig" AfterTargets="GenerateBuildRuntimeConfigurationFiles"
40+
Condition="'$(MSBuildProjectName)' != 'toolset-tasks' And '$(MSBuildProjectName)' != 'HelixTasks' And Exists($(ProjectRuntimeConfigFilePath))">
41+
42+
<!-- Currently, GenerateRuntimeConfigurationFiles omits the reference to Microsoft.NETCore.App from the runtimeconfig file when there is a different
43+
shared framework. In this repo, this can cause the tests or tools such as dotnet-watch to use the stage 0 version of Microsoft.NETCore.App
44+
instead of the version specified in Versions.props. This can cause failures when there are changes to the base framework, as we compile against
45+
the one specified in Versions.props, but the project runs against an earlier version.
46+
47+
This task works around this by adding Microsoft.NETCore.App back to the list of runtimes in the runtimeconfig file if it's not there already. -->
48+
49+
<AddBaseFrameworkToRuntimeConfig RuntimeConfigPath="$(ProjectRuntimeConfigFilePath)" MicrosoftNetCoreAppVersion="$(VSRedistCommonNetCoreSharedFrameworkx6460PackageVersion)" />
50+
51+
</Target>
2952

3053
</Project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
//
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Linq;
9+
using Microsoft.Build.Framework;
10+
using Microsoft.Build.Utilities;
11+
using Microsoft.NET.Build.Tasks;
12+
using Newtonsoft.Json;
13+
using Newtonsoft.Json.Serialization;
14+
15+
namespace Microsoft.DotNet.Build.Tasks
16+
{
17+
public sealed class AddBaseFrameworkToRuntimeConfig : Task
18+
{
19+
[Required]
20+
public string RuntimeConfigPath { get; set; }
21+
22+
[Required]
23+
public string MicrosoftNetCoreAppVersion { get; set; }
24+
25+
public override bool Execute()
26+
{
27+
JsonSerializer serializer = new JsonSerializer();
28+
serializer.ContractResolver = new CamelCasePropertyNamesContractResolver();
29+
serializer.Formatting = Formatting.Indented;
30+
serializer.DefaultValueHandling = DefaultValueHandling.Ignore;
31+
32+
RuntimeConfig runtimeConfig;
33+
using (var sr = new StreamReader(RuntimeConfigPath))
34+
{
35+
runtimeConfig = serializer.Deserialize<RuntimeConfig>(new JsonTextReader(sr));
36+
}
37+
38+
IEnumerable<RuntimeConfigFramework> currentFrameworks = runtimeConfig.RuntimeOptions.Frameworks ?? Enumerable.Empty<RuntimeConfigFramework>();
39+
if (runtimeConfig.RuntimeOptions.Framework != null)
40+
{
41+
currentFrameworks = currentFrameworks.Prepend(runtimeConfig.RuntimeOptions.Framework);
42+
}
43+
44+
if (!currentFrameworks.Any(f => f.Name.Equals("Microsoft.NETCore.App", StringComparison.OrdinalIgnoreCase)))
45+
{
46+
var newFrameworks = currentFrameworks.Prepend(new RuntimeConfigFramework()
47+
{
48+
Name = "Microsoft.NETCore.App",
49+
Version = MicrosoftNetCoreAppVersion
50+
});
51+
52+
runtimeConfig.RuntimeOptions.Framework = null;
53+
runtimeConfig.RuntimeOptions.Frameworks = newFrameworks.ToList();
54+
55+
using (JsonTextWriter writer = new JsonTextWriter(new StreamWriter(File.Create(RuntimeConfigPath))))
56+
{
57+
serializer.Serialize(writer, runtimeConfig);
58+
}
59+
}
60+
61+
return true;
62+
}
63+
}
64+
}

src/Layout/toolset-tasks/toolset-tasks.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<TargetFrameworks>$(SdkTargetFramework);net472</TargetFrameworks>
44
<TargetFrameworks Condition=" '$([MSBuild]::IsOSPlatform(`Windows`))' == 'false' ">$(SdkTargetFramework)</TargetFrameworks>
55
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
6+
<RootNamespace>Microsoft.DotNet.Build.Tasks</RootNamespace>
67
</PropertyGroup>
78

89
<ItemGroup>
@@ -13,6 +14,12 @@
1314
<PackageReference Include="System.Reflection.Metadata" Version="1.4.2" />
1415
</ItemGroup>
1516

17+
<ItemGroup>
18+
<Compile Include="..\..\Tasks\Microsoft.NET.Build.Tasks\RuntimeConfig.cs" LinkBase="RuntimeConfig"/>
19+
<Compile Include="..\..\Tasks\Microsoft.NET.Build.Tasks\RuntimeOptions.cs" LinkBase="RuntimeConfig"/>
20+
<Compile Include="..\..\Tasks\Microsoft.NET.Build.Tasks\RuntimeConfigFramework.cs" LinkBase="RuntimeConfig"/>
21+
</ItemGroup>
22+
1623
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
1724
<Reference Include="System.Net.Http" />
1825
</ItemGroup>

0 commit comments

Comments
 (0)