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

Add TargetFramework awareness to NuGet detector #1266

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add FrameworkPackages lookup
  • Loading branch information
ericstj committed Oct 8, 2024
commit ae22db9713f1ebd08a638a02a1b72bd524968567
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,107 @@ namespace Microsoft.ComponentDetection.Detectors.NuGet;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using global::NuGet.Frameworks;
using global::NuGet.Versioning;

/// <summary>
/// Represents a set of packages that are provided by a specific framework.
/// At the moment this only represents the packages that are provided by the Microsoft.NETCore.App framework.
/// We could extend this to represent the packages provided by other frameworks like Microsoft.AspNetCore.App and Microsoft.WindowsDesktop.App.
/// </summary>
/// <param name="framework">framework that this instance applies to.</param>
internal sealed partial class FrameworkPackages(NuGetFramework framework) : IEnumerable<KeyValuePair<string, NuGetVersion>>, IEnumerable
internal sealed partial class FrameworkPackages : IEnumerable<KeyValuePair<string, NuGetVersion>>, IEnumerable
{
static Dictionary<NuGetFramework, FrameworkPackages> _frameworkPackages;
private static readonly Dictionary<NuGetFramework, FrameworkPackages> FrameworkPackagesByFramework = new();

static FrameworkPackages()
{
FrameworkPackagesByFramework[NETStandard20] = NETStandard20Packages;
FrameworkPackagesByFramework[NETStandard21] = NETStandard21Packages;
FrameworkPackagesByFramework[NETCoreApp20] = NETCoreApp20Packages;
FrameworkPackagesByFramework[NETCoreApp21] = NETCoreApp21Packages;
FrameworkPackagesByFramework[NETCoreApp30] = NETCoreApp30Packages;
FrameworkPackagesByFramework[NETCoreApp31] = NETCoreApp31Packages;
FrameworkPackagesByFramework[NETCoreApp50] = NETCoreApp50Packages;
FrameworkPackagesByFramework[NETCoreApp60] = NETCoreApp60Packages;
FrameworkPackagesByFramework[NETCoreApp70] = NETCoreApp70Packages;
FrameworkPackagesByFramework[NETCoreApp80] = NETCoreApp80Packages;
FrameworkPackagesByFramework[NETCoreApp90] = NETCoreApp90Packages;
}

public FrameworkPackages(NuGetFramework framework) => this.Framework = framework;

public FrameworkPackages(NuGetFramework framework, FrameworkPackages frameworkPackages)
: this(frameworkPackages.Framework) => this.Packages = new(frameworkPackages.Packages);

public NuGetFramework Framework { get; } = framework;
public NuGetFramework Framework { get; }

// Adapted from https://github.com/dotnet/sdk/blob/c3a8f72c3a5491c693ff8e49e7406136a12c3040/src/Tasks/Common/ConflictResolution/PackageOverride.cs#L52-L68
public Dictionary<string, NuGetVersion> Packages { get; } = new Dictionary<string, NuGetVersion>(StringComparer.OrdinalIgnoreCase);

private void Add(string id, string version)
public static FrameworkPackages GetFrameworkPackages(NuGetFramework framework)
{
this.Packages.Add(id, NuGetVersion.Parse(version));
if (FrameworkPackagesByFramework.TryGetValue(framework, out var frameworkPackages))
{
return frameworkPackages;
}

var frameworkPackagesFromPack = LoadFrameworkPackagesFromPack(framework);

return FrameworkPackagesByFramework[framework] = frameworkPackagesFromPack ?? new FrameworkPackages(framework);
}

private static IEnumerable<Tuple<string, NuGetVersion>> CreateOverriddenPackages(string overriddenPackagesString)
private static FrameworkPackages LoadFrameworkPackagesFromPack(NuGetFramework framework)
{
if (!string.IsNullOrEmpty(overriddenPackagesString))
if (framework.Framework != FrameworkConstants.FrameworkIdentifiers.NetCoreApp)
{
return null;
}

// packs location : %ProgramFiles%\dotnet\packs
var packsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "dotnet", "packs", "Microsoft.NETCore.App.Ref");
var packVersionPattern = $"{framework.Version.Major}.{framework.Version.Minor}.*";
var packDirectories = Directory.GetDirectories(packsFolder, packVersionPattern);
var packageOverridesFile = packDirectories
.Select(d => (Overrides: Path.Combine(d, "data", "PackageOverrides.txt"), Version: ParseVersion(Path.GetFileName(d))))
.Where(d => File.Exists(d.Overrides))
.OrderByDescending(d => d.Version)
.FirstOrDefault().Overrides;

if (packageOverridesFile == null)
{
overriddenPackagesString = overriddenPackagesString.Trim();
var overriddenPackagesAndVersions = overriddenPackagesString.Split(new char[] { ';', '\r', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var overriddenPackagesAndVersion in overriddenPackagesAndVersions)
return null;
}

var frameworkPackages = new FrameworkPackages(framework);
var packageOverrides = File.ReadAllLines(packageOverridesFile);

foreach (var packageOverride in packageOverrides)
{
var packageOverrideParts = packageOverride.Trim().Split('|');

if (packageOverrideParts.Length == 2)
{
var trimmedOverriddenPackagesAndVersion = overriddenPackagesAndVersion.Trim();
var separatorIndex = trimmedOverriddenPackagesAndVersion.IndexOf('|');
if (separatorIndex != -1)
{
var versionString = trimmedOverriddenPackagesAndVersion[(separatorIndex + 1)..];
var overriddenPackage = trimmedOverriddenPackagesAndVersion[..separatorIndex];
if (NuGetVersion.TryParse(versionString, out var version))
{
yield return Tuple.Create(overriddenPackage, version);
}
}
var packageId = packageOverrideParts[0];
var packageVersion = ParseVersion(packageOverrideParts[1]);

frameworkPackages.Packages[packageId] = packageVersion;
}
}

return frameworkPackages;

static NuGetVersion ParseVersion(string versionString) => NuGetVersion.TryParse(versionString, out var version) ? version : null;
}

private void Add(string id, string version)
{
this.Packages.Add(id, NuGetVersion.Parse(version));
}

public bool IsAFrameworkComponent(string id, NuGetVersion version) => this.Packages.TryGetValue(id, out var frameworkPackageVersion) && frameworkPackageVersion >= version;

IEnumerator<KeyValuePair<string, NuGetVersion>> IEnumerable<KeyValuePair<string, NuGetVersion>>.GetEnumerator() => this.Packages.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace Microsoft.ComponentDetection.Detectors.NuGet;

using global::NuGet.Frameworks;

/// <summary>
/// Framework packages for net5.0.
/// </summary>
internal partial class FrameworkPackages
{
internal static NuGetFramework NETCoreApp50 { get; } = NuGetFramework.Parse("net5.0");

internal static FrameworkPackages NETCoreApp50Packages { get; } = new(NETCoreApp50, NETCoreApp31Packages)
{
{ "Microsoft.CSharp", "4.7.0" },
{ "runtime.debian.8-x64.runtime.native.System", "4.3.0" },
{ "runtime.debian.8-x64.runtime.native.System.IO.Compression", "4.3.0" },
{ "runtime.debian.8-x64.runtime.native.System.Net.Http", "4.3.0" },
{ "runtime.debian.8-x64.runtime.native.System.Net.Security", "4.3.0" },
{ "runtime.debian.8-x64.runtime.native.System.Security.Cryptography", "4.3.0" },
{ "runtime.fedora.23-x64.runtime.native.System", "4.3.0" },
{ "runtime.fedora.23-x64.runtime.native.System.IO.Compression", "4.3.0" },
{ "runtime.fedora.23-x64.runtime.native.System.Net.Http", "4.3.0" },
{ "runtime.fedora.23-x64.runtime.native.System.Net.Security", "4.3.0" },
{ "runtime.fedora.23-x64.runtime.native.System.Security.Cryptography", "4.3.0" },
{ "runtime.fedora.24-x64.runtime.native.System", "4.3.0" },
{ "runtime.fedora.24-x64.runtime.native.System.IO.Compression", "4.3.0" },
{ "runtime.fedora.24-x64.runtime.native.System.Net.Http", "4.3.0" },
{ "runtime.fedora.24-x64.runtime.native.System.Net.Security", "4.3.0" },
{ "runtime.fedora.24-x64.runtime.native.System.Security.Cryptography", "4.3.0" },
{ "runtime.opensuse.13.2-x64.runtime.native.System", "4.3.0" },
{ "runtime.opensuse.13.2-x64.runtime.native.System.IO.Compression", "4.3.0" },
{ "runtime.opensuse.13.2-x64.runtime.native.System.Net.Http", "4.3.0" },
{ "runtime.opensuse.13.2-x64.runtime.native.System.Net.Security", "4.3.0" },
{ "runtime.opensuse.13.2-x64.runtime.native.System.Security.Cryptography", "4.3.0" },
{ "runtime.opensuse.42.1-x64.runtime.native.System", "4.3.0" },
{ "runtime.opensuse.42.1-x64.runtime.native.System.IO.Compression", "4.3.0" },
{ "runtime.opensuse.42.1-x64.runtime.native.System.Net.Http", "4.3.0" },
{ "runtime.opensuse.42.1-x64.runtime.native.System.Net.Security", "4.3.0" },
{ "runtime.opensuse.42.1-x64.runtime.native.System.Security.Cryptography", "4.3.0" },
{ "runtime.osx.10.10-x64.runtime.native.System", "4.3.0" },
{ "runtime.osx.10.10-x64.runtime.native.System.IO.Compression", "4.3.0" },
{ "runtime.osx.10.10-x64.runtime.native.System.Net.Http", "4.3.0" },
{ "runtime.osx.10.10-x64.runtime.native.System.Net.Security", "4.3.0" },
{ "runtime.osx.10.10-x64.runtime.native.System.Security.Cryptography", "4.3.0" },
{ "runtime.rhel.7-x64.runtime.native.System", "4.3.0" },
{ "runtime.rhel.7-x64.runtime.native.System.IO.Compression", "4.3.0" },
{ "runtime.rhel.7-x64.runtime.native.System.Net.Http", "4.3.0" },
{ "runtime.rhel.7-x64.runtime.native.System.Net.Security", "4.3.0" },
{ "runtime.rhel.7-x64.runtime.native.System.Security.Cryptography", "4.3.0" },
{ "runtime.ubuntu.14.04-x64.runtime.native.System", "4.3.0" },
{ "runtime.ubuntu.14.04-x64.runtime.native.System.IO.Compression", "4.3.0" },
{ "runtime.ubuntu.14.04-x64.runtime.native.System.Net.Http", "4.3.0" },
{ "runtime.ubuntu.14.04-x64.runtime.native.System.Net.Security", "4.3.0" },
{ "runtime.ubuntu.14.04-x64.runtime.native.System.Security.Cryptography", "4.3.0" },
{ "runtime.ubuntu.16.04-x64.runtime.native.System", "4.3.0" },
{ "runtime.ubuntu.16.04-x64.runtime.native.System.IO.Compression", "4.3.0" },
{ "runtime.ubuntu.16.04-x64.runtime.native.System.Net.Http", "4.3.0" },
{ "runtime.ubuntu.16.04-x64.runtime.native.System.Net.Security", "4.3.0" },
{ "runtime.ubuntu.16.04-x64.runtime.native.System.Security.Cryptography", "4.3.0" },
{ "runtime.ubuntu.16.10-x64.runtime.native.System", "4.3.0" },
{ "runtime.ubuntu.16.10-x64.runtime.native.System.IO.Compression", "4.3.0" },
{ "runtime.ubuntu.16.10-x64.runtime.native.System.Net.Http", "4.3.0" },
{ "runtime.ubuntu.16.10-x64.runtime.native.System.Net.Security", "4.3.0" },
{ "runtime.ubuntu.16.10-x64.runtime.native.System.Security.Cryptography", "4.3.0" },
{ "System.Buffers", "4.5.1" },
{ "System.Collections.Immutable", "5.0.0" },
{ "System.ComponentModel.Annotations", "5.0.0" },
{ "System.Diagnostics.DiagnosticSource", "5.0.0" },
{ "System.Formats.Asn1", "5.0.0" },
{ "System.Net.Http.Json", "5.0.0" },
{ "System.Reflection.DispatchProxy", "4.7.1" },
{ "System.Reflection.Metadata", "5.0.0" },
{ "System.Runtime.CompilerServices.Unsafe", "5.0.0" },
{ "System.Text.Encoding.CodePages", "5.0.0" },
{ "System.Text.Encodings.Web", "5.0.0" },
{ "System.Text.Json", "5.0.0" },
{ "System.Threading.Channels", "5.0.0" },
{ "System.Threading.Tasks.Dataflow", "5.0.0" },
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Microsoft.ComponentDetection.Detectors.NuGet;

using global::NuGet.Frameworks;

/// <summary>
/// Framework packages for net6.0.
/// </summary>
internal partial class FrameworkPackages
{
internal static NuGetFramework NETCoreApp60 { get; } = NuGetFramework.Parse("net6.0");

internal static FrameworkPackages NETCoreApp60Packages { get; } = new(NETCoreApp60, NETCoreApp50Packages)
{
{ "Microsoft.Win32.Registry", "5.0.0" },
{ "System.Collections.Immutable", "6.0.0" },
{ "System.Diagnostics.DiagnosticSource", "6.0.0" },
{ "System.Formats.Asn1", "6.0.0" },
{ "System.IO.FileSystem.AccessControl", "5.0.0" },
{ "System.IO.Pipes.AccessControl", "5.0.0" },
{ "System.Net.Http.Json", "6.0.0" },
{ "System.Reflection.Metadata", "6.0.0" },
{ "System.Runtime.CompilerServices.Unsafe", "6.0.0" },
{ "System.Security.AccessControl", "6.0.0" },
{ "System.Security.Cryptography.Cng", "5.0.0" },
{ "System.Security.Cryptography.OpenSsl", "5.0.0" },
{ "System.Security.Principal.Windows", "5.0.0" },
{ "System.Text.Encoding.CodePages", "6.0.0" },
{ "System.Text.Encodings.Web", "6.0.0" },
{ "System.Text.Json", "6.0.0" },
{ "System.Threading.Channels", "6.0.0" },
{ "System.Threading.Tasks.Dataflow", "6.0.0" },
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Microsoft.ComponentDetection.Detectors.NuGet;

using global::NuGet.Frameworks;

/// <summary>
/// Framework packages for net7.0.
/// </summary>
internal partial class FrameworkPackages
{
internal static NuGetFramework NETCoreApp70 { get; } = NuGetFramework.Parse("net7.0");

internal static FrameworkPackages NETCoreApp70Packages { get; } = new(NETCoreApp70, NETCoreApp60Packages)
{
{ "System.Collections.Immutable", "7.0.0" },
{ "System.Diagnostics.DiagnosticSource", "7.0.0" },
{ "System.Formats.Asn1", "7.0.0" },
{ "System.Net.Http.Json", "7.0.0" },
{ "System.Reflection.Metadata", "7.0.0" },
{ "System.Security.AccessControl", "6.0.1" },
{ "System.Text.Encoding.CodePages", "7.0.0" },
{ "System.Text.Encodings.Web", "7.0.0" },
{ "System.Text.Json", "7.0.0" },
{ "System.Threading.Channels", "7.0.0" },
{ "System.Threading.Tasks.Dataflow", "7.0.0" },
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Microsoft.ComponentDetection.Detectors.NuGet;

using global::NuGet.Frameworks;

/// <summary>
/// Framework packages for net8.0.
/// </summary>
internal partial class FrameworkPackages
{
internal static NuGetFramework NETCoreApp80 { get; } = NuGetFramework.Parse("net8.0");

internal static FrameworkPackages NETCoreApp80Packages { get; } = new(NETCoreApp80, NETCoreApp70Packages)
{
{ "System.Diagnostics.DiagnosticSource", "7.0.2" },
{ "System.Net.Http.Json", "7.0.1" },
{ "System.Reflection.Metadata", "7.0.2" },
{ "System.Text.Json", "7.0.4" },
};
}
Loading