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

ApiDiff: Pass an ILog instance to the AssemblySymbolLoader constructor #45807

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

carlossanlop
Copy link
Member

This PR is part of the work needed to create an ApiDiff tool that reuses some of the code from Microsoft.DotNet.GenAPI. The idea is to make the larger PR smaller and make it easier to review: #45389

The purpose of this change is to construct AssemblySymbolLoader instances passing a predefined ILog instance to its constructor which will be necessary later.

@@ -35,7 +35,7 @@ public static void Run(ILog logger,
bool resolveAssemblyReferences = assemblyReferences?.Length > 0;

// Create, configure and execute the assembly loader.
AssemblySymbolLoader loader = new(resolveAssemblyReferences, respectInternals);
AssemblySymbolLoader loader = new(logger, resolveAssemblyReferences, respectInternals);
Copy link
Member

@ViktorHofer ViktorHofer Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the original PR, please rename this field to log. We use that name consistently in APICompat, i.e.

GenAPI was inconsistent in that regard as it was developed by multiple people from different teams and we probably didn't notice this during PR review.

Applies to all other changes in this PR as well.

@@ -196,8 +197,10 @@ public void RetargetableFlagSet(bool strictMode)
string leftAssembly = SymbolFactory.EmitAssemblyFromSyntax(syntax, publicKey: _publicKey);
string rightAssembly = SymbolFactory.EmitAssemblyFromSyntax(syntax);

IAssemblySymbol leftSymbol = new AssemblySymbolLoader().LoadAssembly(leftAssembly);
IAssemblySymbol rightSymbol = new AssemblySymbolLoader().LoadAssembly(rightAssembly);
ILog logger = new ConsoleLog(MessageImportance.High);
Copy link
Member

@ViktorHofer ViktorHofer Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already define a log instance in this file, for the rule factory:

private static readonly TestRuleFactory s_ruleFactory = new((settings, context) => new AssemblyIdentityMustMatch(new SuppressibleTestLog(), settings, context));

Please use that instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seem you're talking about the new SuppressibleTestLog() being passed to AssemblyIdentityMustMatch.

I assume you mean I should extract that instance, store it in a field, and reuse that everywhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not, then I'm struggling to understand how to obtain the logger out of s_ruleFactory, if that's possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you mean I should extract that instance, store it in a field, and reuse that everywhere?

Yes

using Microsoft.DotNet.Cli.Utils;

namespace Microsoft.DotNet.ApiSymbolExtensions.Tests
{
public class AssemblySymbolLoaderTests : SdkTest
{
private readonly ILog _logger = new ConsoleLog(MessageImportance.High);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These AssemblySymbolLoader unit tests should not use the real log implementation (it would log to the console). Please use a mock instead (probably via the Moq framework, I think we already do that in other places in our tests).

@@ -54,8 +54,10 @@ private void RunTest(string original,
}
attributeDataSymbolFilter.Add(accessibilitySymbolFilter);

ILog logger = new ConsoleLog(MessageImportance.Low);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should probably be a mock as well.

Comment on lines +394 to +426
/// <inheritdoc />
public void LogAllDiagnostics(string? headerMessage = null)
{
if (HasRoslynDiagnostics(out IReadOnlyList<Diagnostic> roslynDiagnostics))
{
if (!string.IsNullOrEmpty(headerMessage))
{
_logger.LogWarning(headerMessage!);
}

foreach (Diagnostic warning in roslynDiagnostics)
{
_logger.LogWarning(warning.Id, warning.ToString());
}
}
}

/// <inheritdoc />
public void LogAllWarnings(string? headerMessage = null)
{
if (HasLoadWarnings(out IReadOnlyList<AssemblyLoadWarning> loadWarnings))
{
if (!string.IsNullOrEmpty(headerMessage))
{
_logger.LogWarning(headerMessage!);
}

foreach (AssemblyLoadWarning warning in loadWarnings)
{
_logger.LogWarning(warning.DiagnosticId, warning.Message);
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these would be more suitable as extension methods for the IAssemblySymbolLoader interface but I don't have a strong opinion. @ericstj what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting - it seems like this would be the only caller of HasRoslynDiagnostics and HasLoadWarnings -- which then makes me wonder if those should even be on the interface.

It does seem like those interfaces existed to avoid putting knowledge of the logger in IAssemblySymbolLoader. AFAICT they've been around since the start: 86b7b93#diff-925bb31f91442e16a835b4db1ae8f5e3226c2d0cbf3865f8e7122a6564080ba8R46-R56

Maybe we didn't have log abstraction at the time? I wonder if we can just rethink these methods based on usage and testing requirements, given the current app architecture.

Copy link
Member

@ViktorHofer ViktorHofer Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we didn't have log abstraction at the time? I wonder if we can just rethink these methods based on usage and testing requirements, given the current app architecture.

Yes, we only later provided log abstractions that weren't coupled to APICompat specifics (suppressions).

I wonder if we can just rethink these methods based on usage and testing requirements, given the current app architecture.

Yes, I think that would be better than the current indirection that's happening (putting an AssemblyLoadWarning on a store, then reading from it to log it).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you suggest different method(s) on IAssemblySymbolLoader to replace Has* methods?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Infrastructure untriaged Request triage from a team member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants