Skip to content

Commit

Permalink
Add warning when Authors == AssemblyName
Browse files Browse the repository at this point in the history
The SDKs set this default in Microsoft.NET.DefaultAssemblyInfo.targets, but it's not really a very useful default (like the Description one).

So we flag this as a warning now.

We take over NG0102 since that wasn't active in any way (analyzers never get 4k chars of a property anyway).
  • Loading branch information
kzu committed Mar 1, 2023
1 parent 6e6ff26 commit c668502
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 44 deletions.
5 changes: 5 additions & 0 deletions src/CodeAnalysis/CodeAnalysis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<IsPackable>false</IsPackable>
<PackFolder>analyzers/dotnet</PackFolder>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,4 +17,8 @@
<PackageReference Include="Devlooped.SponsorLink" Version="0.9.9" />
</ItemGroup>

<ItemGroup>
<None Update="NuGetizer.CodeAnalysis.targets" PackFolder="build" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
40 changes: 22 additions & 18 deletions src/CodeAnalysis/MetadataAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ThisAssembly;
Expand All @@ -25,15 +24,6 @@ class Descriptors
description: Strings.DefaultDescription.Description,
helpLinkUri: "https://learn.microsoft.com/en-us/nuget/reference/nuspec#description");

public static readonly DiagnosticDescriptor LongDescription = new(
Strings.LongDescription.ID,
Strings.LongDescription.Title,
Strings.LongDescription.Message,
Category,
DiagnosticSeverity.Error,
true,
helpLinkUri: "https://learn.microsoft.com/en-us/nuget/reference/nuspec#description");

public static readonly DiagnosticDescriptor MissingIcon = new(
Strings.MissingIcon.ID,
Strings.MissingIcon.Title,
Expand Down Expand Up @@ -122,11 +112,20 @@ class Descriptors
true,
description: SourceLinkEmbed.Description,
helpLinkUri: "https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/sourcelink");

public static readonly DiagnosticDescriptor DefaultAuthors = new(
Strings.DefaultAuthors.ID,
Strings.DefaultAuthors.Title,
Strings.DefaultAuthors.Message,
Category,
DiagnosticSeverity.Warning,
true,
description: Strings.DefaultAuthors.Description,
helpLinkUri: "https://learn.microsoft.com/en-us/nuget/reference/nuspec#authors");
}

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
Descriptors.DefaultDescription,
Descriptors.LongDescription,
Descriptors.MissingIcon,
Descriptors.MissingReadme,
Descriptors.MissingLicense,
Expand All @@ -135,7 +134,8 @@ class Descriptors
Descriptors.MissingRepositoryUrl,
Descriptors.MissingProjectUrl,
Descriptors.MissingSourceLink,
Descriptors.MissingSourceEmbed);
Descriptors.MissingSourceEmbed,
Descriptors.DefaultAuthors);

public override void Initialize(AnalysisContext context)
{
Expand All @@ -157,13 +157,17 @@ public override void Initialize(AnalysisContext context)
var sourceLinkEnabled = options.TryGetValue("build_property.EnableSourceLink", out var enableSLProp) &&
"true".Equals(enableSLProp, StringComparison.OrdinalIgnoreCase);
if (options.TryGetValue("build_property.Description", out var description))
if (options.TryGetValue("build_property.Description", out var description) &&
description == DefaultDescription.DefaultValue)
{
ctx.ReportDiagnostic(Diagnostic.Create(Descriptors.DefaultDescription, null));
}
if (options.TryGetValue("build_property.Authors", out var authors) &&
options.TryGetValue("build_property.AssemblyName", out var assembly) &&
authors == assembly)
{
if (description == DefaultDescription.DefaultValue)
ctx.ReportDiagnostic(Diagnostic.Create(Descriptors.DefaultDescription, null));
// There is really no way of getting such a long text in the diagnostic. We actually get an empty string.
else if (description.Length > 4000)
ctx.ReportDiagnostic(Diagnostic.Create(Descriptors.LongDescription, null));
ctx.ReportDiagnostic(Diagnostic.Create(Descriptors.DefaultAuthors, null));
}
string? packageIcon = default;
Expand Down
24 changes: 24 additions & 0 deletions src/CodeAnalysis/NuGetizer.CodeAnalysis.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<ItemGroup>
<CompilerVisibleProperty Include="IsPacking" />
<CompilerVisibleProperty Include="PackageId" />
<CompilerVisibleProperty Include="Description" />
<CompilerVisibleProperty Include="PackageIcon" />
<CompilerVisibleProperty Include="PackageIconUrl" />
<CompilerVisibleProperty Include="PackageReadmeFile" />
<CompilerVisibleProperty Include="PackageLicenseExpression" />
<CompilerVisibleProperty Include="PackageLicenseFile" />
<CompilerVisibleProperty Include="PackageLicenseUrl" />
<CompilerVisibleProperty Include="SourceControlInformationFeatureSupported" />
<CompilerVisibleProperty Include="RepositoryCommit" />
<CompilerVisibleProperty Include="RepositoryUrl" />
<CompilerVisibleProperty Include="PackageProjectUrl" />
<CompilerVisibleProperty Include="EmbedUntrackedSources" />
<CompilerVisibleProperty Include="EnableSourceLink" />

<CompilerVisibleProperty Include="Authors" />
<CompilerVisibleProperty Include="AssemblyName" />
</ItemGroup>

</Project>
23 changes: 14 additions & 9 deletions src/CodeAnalysis/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,6 @@
<data name="DefaultDescription_Title" xml:space="preserve">
<value>Default description detected</value>
</data>
<data name="LongDescription_ID" xml:space="preserve">
<value>NG0102</value>
</data>
<data name="LongDescription_Message" xml:space="preserve">
<value>The Description property used for the package must be up to 4000 characters long.</value>
</data>
<data name="LongDescription_Title" xml:space="preserve">
<value>Provided description is too long</value>
</data>
<data name="MissingIcon_Description" xml:space="preserve">
<value>The PackageIcon project property can specify the path to a JPEG or PNG file to use as the package icon, which will be automatically packed properly.</value>
</data>
Expand Down Expand Up @@ -246,4 +237,18 @@
<data name="SourceLinkEmbed_Description" xml:space="preserve">
<value>When EmbedUntrackedSources is set to 'true', Source Link will embed in your PDB the items that participated in the compile, but not are included in source control.</value>
</data>
<data name="DefaultAuthors_ID" xml:space="preserve">
<value>NG0102</value>
</data>
<data name="DefaultAuthors_Title" xml:space="preserve">
<value>Default authors detected</value>
</data>
<data name="DefaultAuthors_Message" xml:space="preserve">
<value>Authors metadata matches the project's AssemblyName. The value should match the profile name(s) of the actual package author(s).</value>
</data>
<data name="DefaultAuthors_Description" xml:space="preserve">
<value>Authors project property should contain a comma-separated list of packages authors, matching the profile names on nuget.org.
These are displayed in the NuGet Gallery on nuget.org and are used to cross-reference packages by the same authors.</value>
<comment>Long-form description</comment>
</data>
</root>
19 changes: 2 additions & 17 deletions src/NuGetizer.Tasks/NuGetizer.Shared.targets
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Copyright (c) .NET Foundation. All rights reserved.

<!-- Adds compatibility targets with Pack SDK -->
<Import Project="NuGetizer.Compatibility.props" />
<!-- Adds CodeAnalysis props and items needed for diagnostics -->
<Import Project="NuGetizer.CodeAnalysis.targets" />

<PropertyGroup>
<!-- Whether to infer package contents -->
Expand Down Expand Up @@ -60,23 +62,6 @@ Copyright (c) .NET Foundation. All rights reserved.
<ItemGroup>
<!-- This is the capability that CPS in .NETStandard/.NETCore uses to enable the Pack context menu. Unify with that -->
<ProjectCapability Include="Pack" Condition="'$(IsPackable)' == 'true'" />

<!-- Properties for the analyzers -->
<CompilerVisibleProperty Include="IsPacking" />
<CompilerVisibleProperty Include="PackageId" />
<CompilerVisibleProperty Include="Description" />
<CompilerVisibleProperty Include="PackageIcon" />
<CompilerVisibleProperty Include="PackageIconUrl" />
<CompilerVisibleProperty Include="PackageReadmeFile" />
<CompilerVisibleProperty Include="PackageLicenseExpression" />
<CompilerVisibleProperty Include="PackageLicenseFile" />
<CompilerVisibleProperty Include="PackageLicenseUrl" />
<CompilerVisibleProperty Include="SourceControlInformationFeatureSupported" />
<CompilerVisibleProperty Include="RepositoryCommit" />
<CompilerVisibleProperty Include="RepositoryUrl" />
<CompilerVisibleProperty Include="PackageProjectUrl" />
<CompilerVisibleProperty Include="EmbedUntrackedSources" />
<CompilerVisibleProperty Include="EnableSourceLink" />
</ItemGroup>

<!-- Extends the built-in GetTargetPathWithTargetPlatformMoniker output to signal that the project has been nugetized -->
Expand Down

0 comments on commit c668502

Please sign in to comment.