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 option to ignore baseline TFMs #36669

Merged
merged 4 commits into from
Nov 10, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public static void Run(Func<ISuppressionEngine, ISuppressibleLog> logFactory,
string? baselinePackagePath,
string? runtimeGraph,
IReadOnlyDictionary<NuGetFramework, IEnumerable<string>>? packageAssemblyReferences,
IReadOnlyDictionary<NuGetFramework, IEnumerable<string>>? baselinePackageAssemblyReferences)
IReadOnlyDictionary<NuGetFramework, IEnumerable<string>>? baselinePackageAssemblyReferences,
string[]? baselinePackageFrameworksToIgnore)
{
// Initialize the service provider
ApiCompatServiceProvider serviceProvider = new(logFactory,
Expand Down Expand Up @@ -70,7 +71,8 @@ public static void Run(Func<ISuppressionEngine, ISuppressibleLog> logFactory,
enableStrictMode: enableStrictModeForBaselineValidation,
enqueueApiCompatWorkItems: runApiCompat,
executeApiCompatWorkItems: false,
baselinePackage: Package.Create(baselinePackagePath, baselinePackageAssemblyReferences)));
Package.Create(baselinePackagePath, baselinePackageAssemblyReferences),
baselinePackageFrameworksToIgnore));
}

if (runApiCompat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public class ValidatePackageTask : TaskBase
/// </summary>
public ITaskItem[]? BaselinePackageAssemblyReferences { get; set; }

/// <summary>
/// A set of target frameworks to ignore from the baseline package.
/// The framework string must exactly match the folder name in the baseilne package.
/// </summary>
public string[]? BaselinePackageFrameworksToIgnore { get; set; }

public override bool Execute()
{
RoslynResolver roslynResolver = RoslynResolver.Register(RoslynAssembliesPath!);
Expand Down Expand Up @@ -153,15 +159,16 @@ protected override void ExecuteCore()
BaselinePackageTargetPath,
RuntimeGraph,
ParsePackageAssemblyReferences(PackageAssemblyReferences),
ParsePackageAssemblyReferences(BaselinePackageAssemblyReferences));
ParsePackageAssemblyReferences(BaselinePackageAssemblyReferences),
BaselinePackageFrameworksToIgnore);
}

private static Dictionary<NuGetFramework, IEnumerable<string>>? ParsePackageAssemblyReferences(ITaskItem[]? packageAssemblyReferences)
{
if (packageAssemblyReferences == null || packageAssemblyReferences.Length == 0)
if (packageAssemblyReferences is null || packageAssemblyReferences.Length == 0)
return null;

Dictionary<NuGetFramework, IEnumerable<string>>? packageAssemblyReferencesDict = new(packageAssemblyReferences.Length);
Dictionary<NuGetFramework, IEnumerable<string>> packageAssemblyReferencesDict = new(packageAssemblyReferences.Length);
foreach (ITaskItem taskItem in packageAssemblyReferences)
{
string targetFrameworkMoniker = taskItem.GetMetadata("TargetFrameworkMoniker");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ static int Main(string[] args)
Arity = ArgumentArity.ZeroOrMore,
HelpName = "tfm=file1,file2,..."
};
CliOption<string[]?> baselinePackageFrameworksToIgnoreOption = new("--baseline-package-frameworks-to-ignore")
{
Description = "A set of target frameworks to ignore from the baseline package. The framework string must exactly match the folder name in the baseline package.",
AllowMultipleArgumentsPerToken = true,
Arity = ArgumentArity.ZeroOrMore,
};

CliCommand packageCommand = new("package", "Validates the compatibility of package assets");
packageCommand.Arguments.Add(packageArgument);
Expand All @@ -281,6 +287,7 @@ static int Main(string[] args)
packageCommand.Options.Add(baselinePackageOption);
packageCommand.Options.Add(packageAssemblyReferencesOption);
packageCommand.Options.Add(baselinePackageAssemblyReferencesOption);
packageCommand.Options.Add(baselinePackageFrameworksToIgnoreOption);
packageCommand.SetAction((ParseResult parseResult) =>
{
// If a roslyn assemblies path isn't provided, use the compiled against version from a subfolder.
Expand Down Expand Up @@ -309,6 +316,7 @@ static int Main(string[] args)
string? runtimeGraph = parseResult.GetValue(runtimeGraphOption);
Dictionary<NuGetFramework, IEnumerable<string>>? packageAssemblyReferences = parseResult.GetValue(packageAssemblyReferencesOption);
Dictionary<NuGetFramework, IEnumerable<string>>? baselinePackageAssemblyReferences = parseResult.GetValue(baselinePackageAssemblyReferencesOption);
string[]? baselinePackageFrameworksToIgnore = parseResult.GetValue(baselinePackageFrameworksToIgnoreOption);

SuppressibleConsoleLog logFactory(ISuppressionEngine suppressionEngine) => new(suppressionEngine, verbosity);
ValidatePackage.Run(logFactory,
Expand All @@ -330,7 +338,8 @@ static int Main(string[] args)
baselinePackage,
runtimeGraph,
packageAssemblyReferences,
baselinePackageAssemblyReferences);
baselinePackageAssemblyReferences,
baselinePackageFrameworksToIgnore);

roslynResolver.Unregister();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ public void Validate(PackageValidatorOption options)

ApiCompatRunnerOptions apiCompatOptions = new(options.EnableStrictMode, isBaselineComparison: true);

// Iterate over all target frameworks in the package.
foreach (NuGetFramework baselineTargetFramework in options.BaselinePackage.FrameworksInPackage)
{
// Skip target frameworks excluded from the baseline package.
if (options.BaselinePackageFrameworksToIgnore is not null &&
options.BaselinePackageFrameworksToIgnore.Contains(baselineTargetFramework.GetShortFolderName()))
{
continue;
}

// Retrieve the compile time assets from the baseline package
IReadOnlyList<ContentItem>? baselineCompileAssets = options.BaselinePackage.FindBestCompileAssetForFramework(baselineTargetFramework);
if (baselineCompileAssets != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public readonly struct PackageValidatorOption(Package package,
bool enableStrictMode = false,
bool enqueueApiCompatWorkItems = true,
bool executeApiCompatWorkItems = true,
Package? baselinePackage = null)
Package? baselinePackage = null,
string[]? baselinePackageFrameworksToIgnore = null)
{
/// <summary>
/// The latest package that should be validated.
Expand All @@ -39,5 +40,14 @@ public readonly struct PackageValidatorOption(Package package,
/// The baseline package to validate the latest package.
/// </summary>
public Package? BaselinePackage { get; } = baselinePackage;

/// <summary>
/// A set of frameworks to ignore from the baseline package.
/// Entries are stored with invariant culture and ignored casing.
/// </summary>
public HashSet<string>? BaselinePackageFrameworksToIgnore { get; } =
baselinePackageFrameworksToIgnore is not null ?
new HashSet<string>(baselinePackageFrameworksToIgnore, StringComparer.InvariantCultureIgnoreCase) :
null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ Copyright (c) .NET Foundation. All rights reserved.
SuppressionOutputFile="$(ApiCompatSuppressionOutputFile)"
BaselinePackageTargetPath="$(_packageValidationBaselinePath)"
RoslynAssembliesPath="$(RoslynAssembliesPath)"
PackageAssemblyReferences="@(PackageValidationReferencePath)" />
PackageAssemblyReferences="@(PackageValidationReferencePath)"
BaselinePackageFrameworksToIgnore="@(PackageValidationBaselineFrameworkToIgnore)" />

<MakeDir Directories="$([System.IO.Path]::GetDirectoryName('$(ApiCompatValidatePackageSemaphoreFile)'))" />
<Touch Files="$(ApiCompatValidatePackageSemaphoreFile)" AlwaysCreate="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ public class BaselinePackageValidatorTests
[Fact]
public void TfmDroppedInLatestVersion()
{
string[] previousFilePaths = new[]
{
@"ref/netcoreapp3.1/TestPackage.dll",
@"ref/netstandard2.0/TestPackage.dll"
};
Package baselinePackage = new(string.Empty, "TestPackage", "1.0.0", previousFilePaths, null);

string[] currentFilePaths = new[]
{
@"ref/netcoreapp3.1/TestPackage.dll"
};
Package package = new(string.Empty, "TestPackage", "2.0.0", currentFilePaths, null);
Package baselinePackage = new(string.Empty, "TestPackage", "1.0.0",
[
@"lib/netcoreapp3.1/TestPackage.dll",
@"lib/netstandard2.0/TestPackage.dll"
]);
Package package = new(string.Empty, "TestPackage", "2.0.0", [ @"lib/netcoreapp3.1/TestPackage.dll" ]);

(SuppressibleTestLog log, BaselinePackageValidator validator) = CreateLoggerAndValidator();

validator.Validate(new PackageValidatorOption(package,
Expand All @@ -44,5 +39,26 @@ public void TfmDroppedInLatestVersion()
Assert.NotEmpty(log.errors);
Assert.Contains(DiagnosticIds.TargetFrameworkDropped + " " + string.Format(Resources.MissingTargetFramework, ".NETStandard,Version=v2.0"), log.errors);
}

[Fact]
public void BaselineFrameworksExcluded()
{
Package baselinePackage = new(string.Empty, "TestPackage", "1.0.0",
[
@"lib/netcoreapp3.1/TestPackage.dll",
@"lib/netstandard2.0/TestPackage.dll"
]);
Package package = new(string.Empty, "TestPackage", "2.0.0", [ @"lib/netstandard2.0/TestPackage.dll" ]);

(SuppressibleTestLog log, BaselinePackageValidator validator) = CreateLoggerAndValidator();

validator.Validate(new PackageValidatorOption(package,
enableStrictMode: false,
enqueueApiCompatWorkItems: false,
baselinePackage: baselinePackage,
baselinePackageFrameworksToIgnore: [ "netcoreapp3.1" ]));

Assert.Empty(log.errors);
}
}
}