Skip to content

Commit

Permalink
Fix regression in selecting default RuntimeIdentifier
Browse files Browse the repository at this point in the history
Fixes #3495
  • Loading branch information
dsplaisted committed Aug 16, 2019
1 parent 1b601bf commit fac52f3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ public sealed class ResolvePackageAssets : TaskBase
[Output]
public ITaskItem[] ApphostsForShimRuntimeIdentifiers { get; private set; }

[Output]
public ITaskItem[] PackagesReferenced { get; private set; }

/// <summary>
/// Messages from the assets file.
/// These are logged directly and therefore not returned to the targets (note private here).
Expand Down Expand Up @@ -306,6 +309,7 @@ private void ReadItemGroups()
FrameworkReferences = reader.ReadItemGroup();
NativeLibraries = reader.ReadItemGroup();
PackageFolders = reader.ReadItemGroup();
PackagesReferenced = reader.ReadItemGroup();
ResourceAssemblies = reader.ReadItemGroup();
RuntimeAssemblies = reader.ReadItemGroup();
RuntimeTargets = reader.ReadItemGroup();
Expand Down Expand Up @@ -769,6 +773,7 @@ private void WriteItemGroups()
WriteItemGroup(WriteFrameworkReferences);
WriteItemGroup(WriteNativeLibraries);
WriteItemGroup(WritePackageFolders);
WriteItemGroup(WritePackagesReferenced);
WriteItemGroup(WriteResourceAssemblies);
WriteItemGroup(WriteRuntimeAssemblies);
WriteItemGroup(WriteRuntimeTargets);
Expand Down Expand Up @@ -1091,6 +1096,17 @@ private void WritePackageFolders()
}
}

private void WritePackagesReferenced()
{
foreach (var library in _runtimeTarget.Libraries)
{
if (library.IsPackage())
{
WriteItem(library.Name);
}
}
}

private void WriteResourceAssemblies()
{
WriteItems(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,19 @@ Copyright (c) .NET Foundation. All rights reserved.
AfterTargets="ResolvePackageAssets"
BeforeTargets="CoreCompile"
Condition="'$(_UsingDefaultPlatformTarget)' == 'true' and
'$(_UsingDefaultRuntimeIdentifier)' == 'true' and
'@(NativeCopyLocalItems)' == ''">
<PropertyGroup>
'$(_UsingDefaultRuntimeIdentifier)' == 'true'">

<ItemGroup>
<_ReferencedPlatformPackage Include="@(PackagesReferenced)" Condition="'%(Identity)' == 'Microsoft.NETCore.Platforms'" />
</ItemGroup>

<!-- If there were no native assets, or if the Microsoft.NETCore.Platforms package was not referenced, then revert to AnyCPU.
The reason we use AnyCPU if Microsoft.NETCore.Platforms is not referenced is to preserve behavior from before 3.0 when
the RuntimeIdentifier graph wasn't included in the SDK, and assets where the RuntimeIdentifier didn't match exactly wouldn't
be picked up unless the platforms package was referenced. -->

<PropertyGroup Condition="('@(NativeCopyLocalItems)' == '' or
'@(_ReferencedPlatformPackage)' == '')">
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
</Target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<Output TaskParameter="CompileTimeAssemblies" ItemName="ResolvedCompileFileDefinitions" />
<Output TaskParameter="TransitiveProjectReferences" ItemName="_TransitiveProjectReferences" />
<Output TaskParameter="PackageFolders" ItemName="AssetsFilePackageFolder" />
<Output TaskParameter="PackagesReferenced" ItemName="PackagesReferenced" />
</ResolvePackageAssets>

<ItemGroup Condition="'$(UseAppHostFromAssetsFile)' == 'true'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,41 @@ public void It_builds_a_simple_desktop_app()
});
}

// Windows only because default RuntimeIdentifier only applies when current OS is Windows
[WindowsOnlyTheory]
[InlineData(false, "AnyCPU")]
[InlineData(true, "x86")]
public void RuntimeIdentifierIsOnlyInferredIfPlatformsPackageIsReferenced(bool referencePlatformPackage, string expectedPlatform)
{
var testProject = new TestProject()
{
Name = "AutoRuntimeIdentifierTest",
TargetFrameworks = "net472",
IsSdkProject = true,
IsExe = true
};

testProject.PackageReferences.Add(new TestPackageReference("Microsoft.DiasymReader.Native", "1.7.0"));
if (referencePlatformPackage)
{
testProject.PackageReferences.Add(new TestPackageReference("Microsoft.NETCore.Platforms", "2.1.0"));
}

var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: referencePlatformPackage.ToString())
.Restore(Log, testProject.Name);

var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));

buildCommand.Execute()
.Should()
.Pass();

var getValueCommand = new GetValuesCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name), testProject.TargetFrameworks, "PlatformTarget");

getValueCommand.Execute().Should().Should();
getValueCommand.GetValues().Single().Should().Be(expectedPlatform);
}

[WindowsOnlyTheory]
// If we don't set platformTarget and don't use native dependency, we get working AnyCPU app.
[InlineData("defaults", null, false, "Native code was not used (MSIL)")]
Expand Down

0 comments on commit fac52f3

Please sign in to comment.