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 Microsoft.Extensions.AuditReports output path #4945

Merged
merged 10 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 7 additions & 1 deletion docs/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ if desired.
| `EXTEXP0016` | Hosting integration testing experiments |
| `EXTEXP0017` | Contextual options experiments |


# LoggerMessage

| Diagnostic ID | Description |
Expand Down Expand Up @@ -108,3 +107,10 @@ if desired.
| `METGEN017` | Gauge is not supported yet |
| `METGEN018` | Xml comment was not parsed correctly |
| `METGEN019` | A metric class has cycles in its type hierarchy |

## AuditReports

| Diagnostic ID | Description |
| :---------------- | :---------- |
| `AUDREPGEN000` | MetricsReports generator couldn't resolve output path for the report |
| `AUDREPGEN001` | ComplianceReports generator couldn't resolve output path for the report |
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"sdk": {
"version": "8.0.101"
"version": "8.0.200"
RussKie marked this conversation as resolved.
Show resolved Hide resolved
},
"tools": {
"dotnet": "8.0.101",
"dotnet": "8.0.200",
"runtimes": {
"dotnet/x64": [
"6.0.22"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.Gen.Shared;
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Gen.ComplianceReports;

Expand Down Expand Up @@ -55,7 +57,7 @@ public void Execute(GeneratorExecutionContext context)

if (!GeneratorUtilities.ShouldGenerateReport(context, GenerateComplianceReportsMSBuildProperty))
{
// By default, compliance reports are only generated only during build time and not during design time to prevent the file being written on every keystroke in VS.
// By default, compliance reports are generated only during build time and not during design time to prevent the file being written on every keystroke in VS.
return;
}

Expand All @@ -80,10 +82,20 @@ public void Execute(GeneratorExecutionContext context)

if (_directory == null)
{
_ = context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(ComplianceReportOutputPathMSBuildProperty, out _directory);
if (string.IsNullOrWhiteSpace(_directory))
if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(ComplianceReportOutputPathMSBuildProperty, out _directory) ||
string.IsNullOrWhiteSpace(_directory))
{
// no valid output path
// Report diagnostic:
var diagnostic = new DiagnosticDescriptor(
DiagnosticIds.AuditReports.AUDREPGEN001,
"ComplianceReports generator couldn't resolve output path for the report. It won't be generated.",
"MSBuild property <ComplianceReportOutputPath> is missing or not set. The report won't be generated.",
nameof(DiagnosticIds.AuditReports),
DiagnosticSeverity.Info,
isEnabledByDefault: true,
helpLinkUri: string.Format(CultureInfo.InvariantCulture, DiagnosticIds.UrlFormat, DiagnosticIds.AuditReports.AUDREPGEN001));

context.ReportDiagnostic(Diagnostic.Create(diagnostic, location: null));
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.Gen.Metrics.Model;
using Microsoft.Gen.Shared;
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Gen.MetricsReports;

Expand All @@ -22,10 +24,6 @@ public class MetricsReportsGenerator : ISourceGenerator
private const string CurrentProjectPath = "build_property.projectdir";
private const string FileName = "MetricsReport.json";

private string? _compilationOutputPath;
private string? _currentProjectPath;
private string? _reportOutputPath;

public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(ClassDeclarationSyntaxReceiver.Create);
Expand All @@ -35,8 +33,9 @@ public void Execute(GeneratorExecutionContext context)
{
context.CancellationToken.ThrowIfCancellationRequested();

var receiver = context.SyntaxReceiver as ClassDeclarationSyntaxReceiver;
if (receiver == null || receiver.ClassDeclarations.Count == 0 || !GeneratorUtilities.ShouldGenerateReport(context, GenerateMetricDefinitionReport))
if (context.SyntaxReceiver is not ClassDeclarationSyntaxReceiver receiver ||
receiver.ClassDeclarations.Count == 0 ||
!GeneratorUtilities.ShouldGenerateReport(context, GenerateMetricDefinitionReport))
{
return;
}
Expand All @@ -52,13 +51,24 @@ public void Execute(GeneratorExecutionContext context)

var options = context.AnalyzerConfigOptions.GlobalOptions;

var path = (_reportOutputPath != null || options.TryGetValue(ReportOutputPath, out _reportOutputPath))
? _reportOutputPath
var path = TryRetrieveOptionsValue(options, ReportOutputPath, out var reportOutputPath)
? reportOutputPath!
: GetDefaultReportOutputPath(options);

if (string.IsNullOrWhiteSpace(path))
{
// Report diagnostic. Tell that it is either <MetricDefinitionReportOutputPath> missing or <CompilerVisibleProperty Include="OutputPath"/> visibility to compiler.
// Report diagnostic:
var diagnostic = new DiagnosticDescriptor(
DiagnosticIds.AuditReports.AUDREPGEN000,
"MetricsReports generator couldn't resolve output path for the report. It won't be generated.",
"Both <MetricsReportOutputPath> and <OutputPath> MSBuild properties are not set. The report won't be generated.",
nameof(DiagnosticIds.AuditReports),
DiagnosticSeverity.Info,
isEnabledByDefault: true,
helpLinkUri: string.Format(CultureInfo.InvariantCulture, DiagnosticIds.UrlFormat, DiagnosticIds.AuditReports.AUDREPGEN000));

context.ReportDiagnostic(Diagnostic.Create(diagnostic, location: null));

return;
}

Expand Down Expand Up @@ -90,18 +100,25 @@ private static ReportedMetricClass[] MapToCommonModel(IReadOnlyList<MetricType>
return reportedMetrics.ToArray();
}

private string GetDefaultReportOutputPath(AnalyzerConfigOptions options)
private static bool TryRetrieveOptionsValue(AnalyzerConfigOptions options, string name, out string? value)
=> options.TryGetValue(name, out value) && !string.IsNullOrWhiteSpace(value);

private static string GetDefaultReportOutputPath(AnalyzerConfigOptions options)
{
if (_currentProjectPath != null && _compilationOutputPath != null)
if (!TryRetrieveOptionsValue(options, CompilationOutputPath, out var compilationOutputPath))
{
return _currentProjectPath + _compilationOutputPath;
return string.Empty;
}

_ = options.TryGetValue(CompilationOutputPath, out _compilationOutputPath);
_ = options.TryGetValue(CurrentProjectPath, out _currentProjectPath);
// If <OutputPath> is absolute - return it right away:
if (Path.IsPathRooted(compilationOutputPath))
{
return compilationOutputPath!;
}

return string.IsNullOrWhiteSpace(_currentProjectPath) || string.IsNullOrWhiteSpace(_compilationOutputPath)
? string.Empty
: _currentProjectPath + _compilationOutputPath;
// Get <ProjectDir> and combine it with <OutputPath> if the former isn't empty:
return TryRetrieveOptionsValue(options, CurrentProjectPath, out var currentProjectPath)
? Path.Combine(currentProjectPath!, compilationOutputPath!)
: string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<CompilerVisibleProperty Include="ComplianceReportOutputPath" />
<CompilerVisibleProperty Include="GenerateMetricsReport" />
<CompilerVisibleProperty Include="MetricsReportOutputPath" />
<CompilerVisibleProperty Include="OutputPath" />
</ItemGroup>
</Project>

This file was deleted.

6 changes: 6 additions & 0 deletions src/Shared/DiagnosticIds/DiagnosticIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ internal static class Metrics
internal const string METGEN018 = nameof(METGEN018);
internal const string METGEN019 = nameof(METGEN019);
}

internal static class AuditReports
{
internal const string AUDREPGEN000 = nameof(AUDREPGEN000);
internal const string AUDREPGEN001 = nameof(AUDREPGEN001);
}
}

#pragma warning restore S1144
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static async Task<IReadOnlyList<Diagnostic>> RunGenerator(string code, string ou
{
Assembly.GetAssembly(typeof(ILogger))!,
Assembly.GetAssembly(typeof(LoggerMessageAttribute))!,
Assembly.GetAssembly(typeof(Microsoft.Extensions.Compliance.Classification.DataClassification))!,
Assembly.GetAssembly(typeof(Extensions.Compliance.Classification.DataClassification))!,
},
new[]
{
Expand All @@ -120,9 +120,10 @@ public async Task MissingDataClassificationSymbol()
{
Source,
},
new OptionsProvider()).ConfigureAwait(false);
new OptionsProvider())
.ConfigureAwait(false);

Assert.Equal(0, d.Count);
Assert.Empty(d);
}

private sealed class Options : AnalyzerConfigOptions
Expand Down
Loading
Loading