Skip to content

Commit 9f5c51c

Browse files
authored
Generate pruning data correctly (#50348)
1 parent c2ecc0c commit 9f5c51c

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ Copyright (c) .NET Foundation. All rights reserved.
5252
</Target>
5353

5454
<!-- TODO: https://github.com/dotnet/sdk/issues/49917 Remove the framework based condition when the data for netcoreapp2.1 and below is fixed-->
55+
<!-- Package pruning is expected to be enable for all TFMs for multi-targeted projects, so still generate the pruning data when RestoreEnablePackagePruning is '' which implies a multi-targeted project. -->
5556
<Target Name="AddPrunePackageReferences" BeforeTargets="CollectPrunePackageReferences"
5657
DependsOnTargets="ProcessFrameworkReferences"
57-
Condition="'$(RestoreEnablePackagePruning)' == 'true'
58+
Condition="('$(RestoreEnablePackagePruning)' == 'true' OR '$(RestoreEnablePackagePruning)' == '')
5859
AND (('$(TargetFrameworkIdentifier)' == '.NETCoreApp'
5960
AND '$(TargetFrameworkVersion)' != ''
6061
AND $([MSBuild]::VersionGreaterThanOrEquals('$(TargetFrameworkVersion)', '3.0')))

test/Microsoft.NET.Build.Tests/GivenThatWeWantToResolveConflicts.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@ public void PlatformPackagesCanBePruned(bool prunePackages)
331331
[InlineData("net451", false)]
332332
[InlineData("net462")]
333333
[InlineData("net481")]
334-
public void PrunePackageDataSucceeds(string targetFramework, bool shouldPrune = true)
334+
[InlineData("net9.0", true, "")]
335+
[InlineData("netstandard2.1", true, "")]
336+
public void PrunePackageDataSucceeds(string targetFramework, bool shouldPrune = true, string enablePackagePruning = "True")
335337
{
336338
var nugetFramework = NuGetFramework.Parse(targetFramework);
337339

@@ -342,7 +344,7 @@ List<KeyValuePair<string,string>> GetPrunedPackages(string frameworkReference)
342344
TargetFrameworks = targetFramework
343345
};
344346

345-
testProject.AdditionalProperties["RestoreEnablePackagePruning"] = "True";
347+
testProject.AdditionalProperties["RestoreEnablePackagePruning"] = enablePackagePruning;
346348

347349
if (!string.IsNullOrEmpty(frameworkReference))
348350
{
@@ -444,6 +446,49 @@ public void TransitiveFrameworkReferencesDoNotAffectPruning()
444446

445447
}
446448

449+
[CoreMSBuildOnlyTheory]
450+
[InlineData("net10.0;net9.0", true)]
451+
[InlineData("net10.0;net8.0", true)]
452+
[InlineData("net6.0;net7.0", false)]
453+
public void WithMultitargetedProjects_PruningsDefaultsAreApplies(string frameworks, bool prunePackages)
454+
{
455+
var referencedProject = new TestProject("ReferencedProject")
456+
{
457+
TargetFrameworks = frameworks,
458+
IsExe = false
459+
};
460+
referencedProject.PackageReferences.Add(new TestPackageReference("System.Text.Json", "6.0.0"));
461+
referencedProject.AdditionalProperties["RestoreEnablePackagePruning"] = "false";
462+
463+
var testProject = new TestProject()
464+
{
465+
TargetFrameworks = frameworks,
466+
};
467+
468+
testProject.ReferencedProjects.Add(referencedProject);
469+
470+
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: prunePackages.ToString());
471+
472+
var buildCommand = new BuildCommand(testAsset);
473+
474+
buildCommand.Execute().Should().Pass();
475+
476+
var assetsFilePath = Path.Combine(buildCommand.GetBaseIntermediateDirectory().FullName, "project.assets.json");
477+
var lockFile = LockFileUtilities.GetLockFile(assetsFilePath, new NullLogger());
478+
479+
foreach(var lockFileTarget in lockFile.Targets)
480+
{
481+
if (prunePackages)
482+
{
483+
lockFileTarget.Libraries.Should().NotContain(library => library.Name.Equals("System.Text.Json", StringComparison.OrdinalIgnoreCase));
484+
}
485+
else
486+
{
487+
lockFileTarget.Libraries.Should().Contain(library => library.Name.Equals("System.Text.Json", StringComparison.OrdinalIgnoreCase));
488+
}
489+
}
490+
}
491+
447492
static List<KeyValuePair<string, string>> ParsePrunePackageReferenceJson(string json)
448493
{
449494
List<KeyValuePair<string, string>> ret = new();

0 commit comments

Comments
 (0)