Skip to content
Merged
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
37 changes: 37 additions & 0 deletions src/Tasks/Microsoft.NET.Build.Tasks/ResolveFrameworkReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class ResolveFrameworkReferences : TaskBase
[Output]
public ITaskItem[] PackagesToDownload { get; set; }

[Output]
public ITaskItem[] LegacyFrameworkPackages { get; set; }

[Output]
public ITaskItem[] RuntimeFrameworks { get; set; }

Expand Down Expand Up @@ -80,6 +83,7 @@ protected override void ExecuteCore()
var frameworkReferenceMap = FrameworkReferences.ToDictionary(fr => fr.ItemSpec);

List<ITaskItem> packagesToDownload = new List<ITaskItem>();
List<ITaskItem> legacyFrameworkPackages = new List<ITaskItem>();
List<ITaskItem> runtimeFrameworks = new List<ITaskItem>();
List<ITaskItem> targetingPacks = new List<ITaskItem>();
List<ITaskItem> runtimePacks = new List<ITaskItem>();
Expand All @@ -91,6 +95,21 @@ protected override void ExecuteCore()
{
frameworkReferenceMap.TryGetValue(knownFrameworkReference.Name, out ITaskItem frameworkReference);

if (frameworkReference != null)
{
if (!string.IsNullOrEmpty(knownFrameworkReference.LegacyFrameworkPackages))
{
foreach (var packageAndVersion in knownFrameworkReference.LegacyFrameworkPackages.Split(';'))
{
var items = packageAndVersion.Split('/');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit:: So many allocations, but we already have an issue to speed up this task.

TaskItem packageToReference = new TaskItem(items[0]);
packageToReference.SetMetadata(MetadataKeys.Version, items[1]);

legacyFrameworkPackages.Add(packageToReference);
}
}
}

// Get the path of the targeting pack in the targeting pack root (e.g. dotnet/ref)
TaskItem targetingPack = new TaskItem(knownFrameworkReference.Name);
targetingPack.SetMetadata(MetadataKeys.PackageName, knownFrameworkReference.TargetingPackName);
Expand Down Expand Up @@ -182,6 +201,11 @@ protected override void ExecuteCore()
PackagesToDownload = packagesToDownload.ToArray();
}

if (legacyFrameworkPackages.Any())
{
LegacyFrameworkPackages = legacyFrameworkPackages.ToArray();
}

if (runtimeFrameworks.Any())
{
RuntimeFrameworks = runtimeFrameworks.ToArray();
Expand Down Expand Up @@ -352,6 +376,19 @@ public KnownFrameworkReference(ITaskItem item)

public string RuntimePackRuntimeIdentifiers => _item.GetMetadata("RuntimePackRuntimeIdentifiers");

public string LegacyFrameworkPackages
{
get
{
var packages = _item.GetMetadata("LegacyFrameworkPackages");
if (string.IsNullOrEmpty(packages))
{
packages = _item.GetMetadata("PackagesToReference");
}
return packages;
}
}

public NuGetFramework TargetFramework { get; }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,6 @@ Copyright (c) .NET Foundation. All rights reserved.
<PackageReference Condition="'%(PackageReference.Identity)' == 'Microsoft.NETCore.App'">
<Version Condition="'%(PackageReference.Version)' == ''">$(_TargetFrameworkVersionWithoutV)</Version>
</PackageReference>

<!-- Reference the Platforms package in order to supply the RID graph to NuGet.
This can be removed once we do https://github.com/dotnet/cli/issues/10528 or
https://github.com/NuGet/Home/issues/7351

We only do this here when there are PackageReferences. If the only references
are FrameworkReferences, then we do not need to provide this package to NuGet. -->
<PackageReference Include="Microsoft.NETCore.Platforms"
Version="$(BundledNETCorePlatformsPackageVersion)"
IsImplicitlyDefined="true"
Condition="'$(DisableImplicitFrameworkReferences)' != 'true'
and '$(DisableImplicitNETCorePlatformsReference)' != 'true'
and '$(TargetFrameworkIdentifier)' == '.NETCoreApp'
and '$(_TargetFrameworkVersionWithoutV)' != ''
and '$(_TargetFrameworkVersionWithoutV)' >= '3.0'"
PrivateAssets="All"
Publish="true" />
</ItemGroup>
</Target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Copyright (c) .NET Foundation. All rights reserved.
EnableTargetingPackDownload="$(EnableTargetingPackDownload)">

<Output TaskParameter="PackagesToDownload" ItemName="_PackageToDownload" />
<Output TaskParameter="LegacyFrameworkPackages" ItemName="_LegacyFrameworkPackage" />
<Output TaskParameter="RuntimeFrameworks" ItemName="RuntimeFramework" />
<Output TaskParameter="TargetingPacks" ItemName="TargetingPack" />
<Output TaskParameter="RuntimePacks" ItemName="RuntimePack" />
Expand Down Expand Up @@ -141,6 +142,16 @@ Copyright (c) .NET Foundation. All rights reserved.
ExcludeAssets="all" />
</ItemGroup>

<!-- Only add these "legacy" package references if there are already existing package references,
as they are only needed in those scenarios (for example supplying the RID graph to
NuGet, or preventing older versions of Framework packages from being used). -->
<ItemGroup Condition="'@(PackageReference)' != '' and '$(DisableImplicitFrameworkReferences)' != 'true'">
<PackageReference Include="@(_LegacyFrameworkPackage)"
IsImplicitlyDefined="true"
PrivateAssets="all"
ExcludeAssets="all" />
</ItemGroup>

</Target>

<UsingTask TaskName="GetPackageDirectory" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,5 +616,68 @@ static void Main(string[] args)
.Should()
.Pass();
}

[Fact(Skip = "https://github.com/dotnet/sdk/issues/3044")]
public void ReferenceLegacyContracts()
{
var testProject = new TestProject()
{
Name = "ReferencesLegacyContracts",
TargetFrameworks = "netcoreapp3.0",
IsSdkProject = true,
IsExe = true,
RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid("netcoreapp3.0")
};

// Dependencies on contracts from different 1.x "bands" can cause downgrades when building
// with a RuntimeIdentifier.
testProject.PackageReferences.Add(new TestPackageReference("System.IO.FileSystem", "4.0.1"));
testProject.PackageReferences.Add(new TestPackageReference("System.Reflection", "4.3.0"));


var testAsset = _testAssetsManager.CreateTestProject(testProject, testProject.Name)
.Restore(Log, testProject.Name);

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

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

[Fact]
public void ItHasNoPackageReferences()
{
var testProject = new TestProject()
{
Name = "NoPackageReferences",
TargetFrameworks = "netcoreapp3.0",
IsSdkProject = true,
IsExe = true
};

var testAsset = _testAssetsManager.CreateTestProject(testProject, testProject.Name)
.Restore(Log, testProject.Name);

string testDirectory = Path.Combine(testAsset.TestRoot, testProject.Name);

var getPackageReferences = new GetValuesCommand(
Log,
testDirectory,
testProject.TargetFrameworks,
"PackageReference",
GetValuesCommand.ValueType.Item);

getPackageReferences.Execute().Should().Pass();

List<string> packageReferences = getPackageReferences.GetValues();

packageReferences
.Should()
.BeEmpty();


}
}
}