Skip to content

Commit c47af12

Browse files
authored
[APICompat - PackageValidation] Enable baseline assembly references (#32930)
1 parent 3750e65 commit c47af12

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

src/ApiCompat/Microsoft.DotNet.ApiCompatibility/Runner/ApiCompatRunner.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,8 @@ private IReadOnlyList<ElementContainer<IAssemblySymbol>> CreateAssemblySymbols(I
115115
ApiCompatRunnerOptions options,
116116
out bool resolvedExternallyProvidedAssemblyReferences)
117117
{
118-
// In order to enable reference support for baseline suppression we need a better way
119-
// to resolve references for the baseline package. Let's not enable it for now.
120118
string[] aggregatedReferences = metadataInformation.Where(m => m.References != null).SelectMany(m => m.References!).Distinct().ToArray();
121-
resolvedExternallyProvidedAssemblyReferences = !options.IsBaselineComparison && aggregatedReferences.Length > 0;
119+
resolvedExternallyProvidedAssemblyReferences = aggregatedReferences.Length > 0;
122120

123121
IAssemblySymbolLoader loader = _assemblySymbolLoaderFactory.Create(resolvedExternallyProvidedAssemblyReferences);
124122
if (resolvedExternallyProvidedAssemblyReferences)

src/ApiCompat/Microsoft.DotNet.PackageValidation/ApiCompatRunnerExtensions.cs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.IO;
67
using Microsoft.DotNet.ApiCompatibility;
78
using Microsoft.DotNet.ApiCompatibility.Logging;
@@ -25,21 +26,15 @@ public static void QueueApiCompatFromContentItem(this IApiCompatRunner apiCompat
2526
Package leftPackage,
2627
Package? rightPackage = null)
2728
{
29+
Debug.Assert(leftContentItems.Count > 0);
30+
Debug.Assert(rightContentItems.Count > 0);
31+
2832
// Don't enqueue duplicate items (if no right package is supplied and items match)
29-
if (rightPackage == null && ContentItemCollectionEquals(leftContentItems, rightContentItems))
33+
if (rightPackage is null && ContentItemCollectionEquals(leftContentItems, rightContentItems))
3034
{
3135
return;
3236
}
3337

34-
MetadataInformation[] left = new MetadataInformation[leftContentItems.Count];
35-
for (int leftIndex = 0; leftIndex < leftContentItems.Count; leftIndex++)
36-
{
37-
left[leftIndex] = GetMetadataInformation(log,
38-
leftPackage,
39-
leftContentItems[leftIndex],
40-
options.IsBaselineComparison ? Resources.Baseline + " " + leftContentItems[leftIndex].Path : null);
41-
}
42-
4338
MetadataInformation[] right = new MetadataInformation[rightContentItems.Count];
4439
for (int rightIndex = 0; rightIndex < rightContentItems.Count; rightIndex++)
4540
{
@@ -48,28 +43,46 @@ public static void QueueApiCompatFromContentItem(this IApiCompatRunner apiCompat
4843
rightContentItems[rightIndex]);
4944
}
5045

46+
MetadataInformation[] left = new MetadataInformation[leftContentItems.Count];
47+
for (int leftIndex = 0; leftIndex < leftContentItems.Count; leftIndex++)
48+
{
49+
left[leftIndex] = GetMetadataInformation(log,
50+
leftPackage,
51+
leftContentItems[leftIndex],
52+
displayString: options.IsBaselineComparison ? Resources.Baseline + " " + leftContentItems[leftIndex].Path : null,
53+
// Use the assembly references from the right package if the left package doesn't provide them.
54+
assemblyReferences: rightPackage is not null ? right[right.Length > leftIndex ? leftIndex : 0].References : null);
55+
}
56+
5157
apiCompatRunner.EnqueueWorkItem(new ApiCompatRunnerWorkItem(left, options, right));
5258
}
5359

5460
private static MetadataInformation GetMetadataInformation(ISuppressableLog log,
5561
Package package,
5662
ContentItem item,
57-
string? displayString = null)
63+
string? displayString = null,
64+
IEnumerable<string>? assemblyReferences = null)
5865
{
5966
displayString ??= item.Path;
60-
string[]? assemblyReferences = null;
6167

6268
if (item.Properties.TryGetValue("tfm", out object? tfmObj))
6369
{
6470
string targetFramework = ((NuGetFramework)tfmObj).GetShortFolderName();
6571

66-
if (package.AssemblyReferences != null && !package.AssemblyReferences.TryGetValue(targetFramework, out assemblyReferences))
72+
if (package.AssemblyReferences != null)
6773
{
68-
log.LogWarning(new Suppression(DiagnosticIds.SearchDirectoriesNotFoundForTfm) { Target = displayString },
69-
DiagnosticIds.SearchDirectoriesNotFoundForTfm,
70-
string.Format(Resources.MissingSearchDirectory,
71-
targetFramework,
72-
displayString));
74+
if (package.AssemblyReferences.TryGetValue(targetFramework, out string[]? assemblyReferencesRaw))
75+
{
76+
assemblyReferences = assemblyReferencesRaw;
77+
}
78+
else
79+
{
80+
log.LogWarning(new Suppression(DiagnosticIds.SearchDirectoriesNotFoundForTfm) { Target = displayString },
81+
DiagnosticIds.SearchDirectoriesNotFoundForTfm,
82+
string.Format(Resources.MissingSearchDirectory,
83+
targetFramework,
84+
displayString));
85+
}
7386
}
7487
}
7588

src/ApiCompat/Microsoft.DotNet.PackageValidation/Package.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public static Package Create(string? packagePath, Dictionary<string, string[]>?
208208
}
209209

210210
/// <summary>
211-
/// Finds the best compile time assset for a specific framework.
211+
/// Finds the best compile time asset for a specific framework.
212212
/// </summary>
213213
/// <param name="framework">The framework where the package needs to be installed.</param>
214214
/// <returns>A ContentItem representing the best compile time asset.</returns>

0 commit comments

Comments
 (0)