-
Notifications
You must be signed in to change notification settings - Fork 18
Warn when direct package reference is unused #121
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
Open
stan-sz
wants to merge
4
commits into
dfederm:main
Choose a base branch
from
stan-sz:issue-119
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| namespace ReferenceTrimmer.Shared; | ||
|
|
||
| internal readonly record struct DeclaredReference(string AssemblyPath, DeclaredReferenceKind Kind, string Spec); | ||
| internal readonly record struct DeclaredReference(string AssemblyPath, DeclaredReferenceKind Kind, string Spec, string AdditionalSpec); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's "additional spec"? There's only one "spec" (Include value) |
||
|
|
||
| internal enum DeclaredReferenceKind { Reference, ProjectReference, PackageReference } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,7 +129,7 @@ public override bool Execute() | |
|
|
||
| if (referencePath is not null) | ||
| { | ||
| declaredReferences.Add(new DeclaredReference(referencePath, DeclaredReferenceKind.Reference, referenceSpec)); | ||
| declaredReferences.Add(new DeclaredReference(referencePath, DeclaredReferenceKind.Reference, referenceSpec, string.Empty)); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -162,7 +162,7 @@ public override bool Execute() | |
| string projectReferenceAssemblyPath = Path.GetFullPath(projectReference.ItemSpec); | ||
| string referenceProjectFile = projectReference.GetMetadata("OriginalProjectReferenceItemSpec"); | ||
|
|
||
| declaredReferences.Add(new DeclaredReference(projectReferenceAssemblyPath, DeclaredReferenceKind.ProjectReference, referenceProjectFile)); | ||
| declaredReferences.Add(new DeclaredReference(projectReferenceAssemblyPath, DeclaredReferenceKind.ProjectReference, referenceProjectFile, string.Empty)); | ||
| } | ||
| } | ||
| else | ||
|
|
@@ -199,9 +199,9 @@ public override bool Execute() | |
| continue; | ||
| } | ||
|
|
||
| foreach (string assemblyPath in packageInfo.CompileTimeAssemblies) | ||
| foreach (var assemblyPath in packageInfo.CompileTimeAssemblies) | ||
| { | ||
| declaredReferences.Add(new DeclaredReference(assemblyPath, DeclaredReferenceKind.PackageReference, packageReference.ItemSpec)); | ||
| declaredReferences.Add(new DeclaredReference(assemblyPath.Item2, DeclaredReferenceKind.PackageReference, packageReference.ItemSpec, assemblyPath.Item1)); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -347,7 +347,7 @@ private Dictionary<string, PackageInfo> GetPackageInfos() | |
| packageInfoBuilders.Add(packageId, packageInfoBuilder); | ||
| } | ||
|
|
||
| packageInfoBuilder.AddCompileTimeAssemblies(nugetLibraryAssemblies); | ||
| packageInfoBuilder.AddCompileTimeAssemblies(nugetLibrary.Name, nugetLibraryAssemblies); | ||
| packageInfoBuilder.AddBuildFiles(buildFiles); | ||
|
|
||
| // Recurse though dependents | ||
|
|
@@ -476,7 +476,7 @@ private static bool IsSuppressed(ITaskItem item, string warningId) | |
| return false; | ||
| } | ||
|
|
||
| // File format: tab-separated fields (AssemblyPath, Kind, Spec), one reference per line. | ||
| // File format: tab-separated fields (AssemblyPath, Kind, Spec, AdditionalSpec), one reference per line. | ||
| // Keep in sync with ReadDeclaredReferences in ReferenceTrimmerAnalyzer.cs. | ||
| private static void SaveDeclaredReferences(IReadOnlyList<DeclaredReference> declaredReferences, string filePath) | ||
| { | ||
|
|
@@ -497,6 +497,8 @@ private static void SaveDeclaredReferences(IReadOnlyList<DeclaredReference> decl | |
| writer.Append(kindString); | ||
| writer.Append(fieldDelimiter); | ||
| writer.Append(reference.Spec); | ||
| writer.Append(fieldDelimiter); | ||
| writer.Append(reference.AdditionalSpec); | ||
| writer.AppendLine(); | ||
| } | ||
|
|
||
|
|
@@ -515,18 +517,18 @@ private static void SaveDeclaredReferences(IReadOnlyList<DeclaredReference> decl | |
|
|
||
| private sealed class PackageInfoBuilder | ||
| { | ||
| private List<string>? _compileTimeAssemblies; | ||
| private List<Tuple<string, string>>? _compileTimeAssemblies; | ||
| private List<string>? _buildFiles; | ||
|
|
||
| public void AddCompileTimeAssemblies(List<string> compileTimeAssemblies) | ||
| public void AddCompileTimeAssemblies(string packageName, List<string> compileTimeAssemblies) | ||
| { | ||
| if (compileTimeAssemblies.Count == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _compileTimeAssemblies ??= new(compileTimeAssemblies.Count); | ||
| _compileTimeAssemblies.AddRange(compileTimeAssemblies); | ||
| _compileTimeAssemblies.AddRange(compileTimeAssemblies.Select(assemblyPath => Tuple.Create(packageName, assemblyPath))); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid the allocation. A value tuple would be best. |
||
| } | ||
|
|
||
| public void AddBuildFiles(List<string> buildFiles) | ||
|
|
@@ -542,11 +544,11 @@ public void AddBuildFiles(List<string> buildFiles) | |
|
|
||
| public PackageInfo ToPackageInfo() | ||
| => new( | ||
| (IReadOnlyCollection<string>?)_compileTimeAssemblies ?? Array.Empty<string>(), | ||
| (IReadOnlyCollection<string>?)_buildFiles ?? Array.Empty<string>()); | ||
| (IReadOnlyCollection<Tuple<string, string>>?)_compileTimeAssemblies ?? [], | ||
| (IReadOnlyCollection<string>?)_buildFiles ?? []); | ||
| } | ||
|
|
||
| private readonly record struct PackageInfo( | ||
| IReadOnlyCollection<string> CompileTimeAssemblies, | ||
| IReadOnlyCollection<Tuple<string, string>> CompileTimeAssemblies, | ||
| IReadOnlyCollection<string> BuildFiles); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Tests/TestData/UnusedPackageReferenceWithMetaPackage/Test/Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using System; | ||
| using System.Collections.Frozen; | ||
|
|
||
| namespace Test | ||
| { | ||
| public class Foo | ||
| { | ||
| public static FrozenSet<int> SomeSet() => FrozenSet.ToFrozenSet(Array.Empty<int>()); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/Tests/TestData/UnusedPackageReferenceWithMetaPackage/Test/Test.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="MSTest.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ExplicitUsings>enable</ExplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="MSTest" Version="4.1.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
9 changes: 9 additions & 0 deletions
9
src/Tests/TestData/UnusedPackageReferenceWithSdk/Test/Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using Castle.Core.Logging; | ||
|
|
||
| namespace Test | ||
| { | ||
| public class Foo | ||
| { | ||
| public static ILogger Logger() => NullLogger.Instance; | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/Tests/TestData/UnusedPackageReferenceWithSdk/Test/Test.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="MSTest.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ExplicitUsings>enable</ExplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Moq" Version="4.20.72" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the whole point of the change, to have a slightly different error message?
Its also unclear on how this relates to #119
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a proposed implementation to reuse RT0003. I'm open for creating RT0004 for this case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem isn't the diagnostic number, the problem is that I dont understand what this change does beyond add this suffix to the message.
Can you add a PR description to explain things?