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

Fix NullReferenceException in dotnet store #11422

Merged
merged 1 commit into from
May 7, 2020
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
36 changes: 36 additions & 0 deletions src/Assets/TestProjects/TargetManifests/PrunePackages.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
</ItemGroup>

<Target Name="AddPackagesToPrune" AfterTargets="PrepforRestoreForComposeStore">
<ItemGroup>
<PackageToPrune Include="Microsoft.CSharp" />
<PackageToPrune Include="System.Collections" />
<PackageToPrune Include="System.Diagnostics.Debug" />
<PackageToPrune Include="System.Dynamic.Runtime" />
<PackageToPrune Include="System.Globalization" />
<PackageToPrune Include="System.IO" />
<PackageToPrune Include="System.Linq" />
<PackageToPrune Include="System.Linq.Expressions" />
<PackageToPrune Include="System.ObjectModel" />
<PackageToPrune Include="System.Reflection" />
<PackageToPrune Include="System.Reflection.Extensions" />
<PackageToPrune Include="System.Resources.ResourceManager" />
<PackageToPrune Include="System.Runtime" />
<PackageToPrune Include="System.Runtime.Extensions" />
<PackageToPrune Include="System.Runtime.Serialization.Primitives" />
<PackageToPrune Include="System.Text.Encoding" />
<PackageToPrune Include="System.Text.Encoding.Extensions" />
<PackageToPrune Include="System.Text.RegularExpressions" />
<PackageToPrune Include="System.Threading" />
<PackageToPrune Include="System.Threading.Tasks" />
<PackageToPrune Include="System.Xml.ReaderWriter" />
<PackageToPrune Include="System.Xml.XDocument" />
</ItemGroup>
<PropertyGroup>
<PackagesToPrune>$(PackagesToPrune);@(PackageToPrune)</PackagesToPrune>
</PropertyGroup>
</Target>
</Project>
2 changes: 1 addition & 1 deletion src/Tasks/Microsoft.NET.Build.Tasks/FilterResolvedFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected override void ExecuteCore()
var pkgName = packageItem.ItemSpec;
if (!string.IsNullOrEmpty(pkgName))
{
packageClosure.UnionWith(projectContext.GetTransitiveList(pkgName));
packageClosure.UnionWith(projectContext.GetTransitiveList(pkgName, ignoreIfNotFound: true));
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/Tasks/Microsoft.NET.Build.Tasks/ProjectContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Build.Framework;
using Microsoft.DotNet.PlatformAbstractions;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using System;
Expand Down Expand Up @@ -145,9 +146,13 @@ public IEnumerable<LockFileTargetLibrary> GetRuntimeLibraries(IEnumerable<string
return runtimeLibraries.Filter(allExclusionList).ToArray();
}

internal IEnumerable<PackageIdentity> GetTransitiveList(string package)
internal IEnumerable<PackageIdentity> GetTransitiveList(string package, bool ignoreIfNotFound = false)
{
LockFileTargetLibrary platformLibrary = _lockFileTarget.GetLibrary(package);
if (platformLibrary == null && ignoreIfNotFound)
{
return Enumerable.Empty<PackageIdentity>();
}
IEnumerable<LockFileTargetLibrary> runtimeLibraries = _lockFileTarget.Libraries;
Dictionary<string, LockFileTargetLibrary> libraryLookup =
runtimeLibraries.ToDictionary(e => e.Name, StringComparer.OrdinalIgnoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,41 @@ public void It_stores_when_targeting_netcoreapp3(bool isExe)
});
}

[CoreMSBuildOnlyFact]
public void DotnetStoreWithPrunedPackages()
{
const string TargetFramework = "netcoreapp3.1";

TestAsset targetManifestsAsset = _testAssetsManager
.CopyTestAsset("TargetManifests")
.WithSource();

var outputFolder = Path.Combine(targetManifestsAsset.TestRoot, "o");
var workingDir = Path.Combine(targetManifestsAsset.TestRoot, "w");

var composeStore = new ComposeStoreCommand(Log, targetManifestsAsset.TestRoot, "PrunePackages.xml")
.Execute(
$"/p:TargetFramework={TargetFramework}",
$"/p:RuntimeIdentifier={EnvironmentInfo.GetCompatibleRid(TargetFramework)}",
$"/p:ComposeDir={outputFolder}",
$"/p:ComposeWorkingDir={workingDir}",
"/p:PreserveComposeWorkingDir=true",
"/p:DoNotDecorateComposeDir=true",
"/p:CreateProfilingSymbols=false"
);

composeStore.Should().Pass();

new DirectoryInfo(outputFolder).GetDirectories()
.Select(d => d.Name)
.Should().BeEquivalentTo(
"fluentassertions",
"newtonsoft.json",
"system.configuration.configurationmanager",
"system.security.cryptography.protecteddata");

}

private static HashSet<PackageIdentity> ParseStoreArtifacts(string path)
{
return new HashSet<PackageIdentity>(
Expand Down