-
Notifications
You must be signed in to change notification settings - Fork 40
Detect package references containing dll's that were not used during compilation #234
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
012746c
Detect package references containing dll's that were not used during …
pakrym d2a8f8c
Nits
pakrym 4f7c9cd
Revert src/Internal.AspNetCore.Sdk/Internal.AspNetCore.Sdk.csproj
pakrym 99a88e0
Fix net46 build
pakrym 41c2309
More PP's
pakrym 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
82 changes: 82 additions & 0 deletions
82
src/Internal.AspNetCore.BuildTools.Tasks/FindUnusedReferences.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,82 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection.Metadata; | ||
using System.Reflection.PortableExecutable; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Microsoft.AspNetCore.BuildTools | ||
{ | ||
#if SDK | ||
public class Sdk_FindUnusedReferences : Task | ||
#elif BuildTools | ||
public class FindUnusedReferences : Task | ||
#else | ||
#error This must be built either for an SDK or for BuildTools | ||
#endif | ||
{ | ||
/// <summary> | ||
/// IntermediateAssembly from CoreCompile | ||
/// </summary> | ||
[Required] | ||
public string Assembly { get; set; } | ||
|
||
/// <summary> | ||
/// ReferencePath from CoreCompile | ||
/// </summary> | ||
[Required] | ||
public ITaskItem[] References { get; set; } | ||
|
||
/// <summary> | ||
/// FileDefinitions from RunResolvePackageDependencies | ||
/// </summary> | ||
[Required] | ||
public ITaskItem[] Packages { get; set; } | ||
|
||
/// <summary> | ||
/// PackageDependencies from RunResolvePackageDependencies | ||
/// </summary> | ||
[Required] | ||
public ITaskItem[] Files { get; set; } | ||
|
||
[Output] | ||
public ITaskItem[] UnusedReferences { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
var references = new HashSet<string>(References.Select(item => item.ItemSpec), StringComparer.OrdinalIgnoreCase); | ||
var referenceFiles = Files.Where(file => references.Contains(file.GetMetadata("ResolvedPath"))) | ||
.ToDictionary(item => Path.GetFileNameWithoutExtension(item.GetMetadata("ResolvedPath")), StringComparer.OrdinalIgnoreCase); | ||
|
||
var directReferences = new HashSet<string>( | ||
Packages.Where(p => string.IsNullOrEmpty(p.GetMetadata("ParentPackage"))).Select(i => i.ItemSpec), | ||
StringComparer.OrdinalIgnoreCase); | ||
|
||
using (var fileStream = File.OpenRead(Assembly)) | ||
using (var reader = new PEReader(fileStream)) | ||
{ | ||
var metadataReader = reader.GetMetadataReader(); | ||
foreach (AssemblyReferenceHandle assemblyReferenceHandle in metadataReader.AssemblyReferences) | ||
{ | ||
var assemblyReference = metadataReader.GetAssemblyReference(assemblyReferenceHandle); | ||
var name = metadataReader.GetString(assemblyReference.Name); | ||
if (referenceFiles.TryGetValue(name, out var fileItem)) | ||
{ | ||
var packageName = fileItem.GetMetadata("PackageName") + "/" + fileItem.GetMetadata("PackageVersion"); | ||
directReferences.Remove(packageName); | ||
referenceFiles.Remove(name); | ||
} | ||
|
||
} | ||
} | ||
|
||
UnusedReferences = referenceFiles.Values.Where(f => directReferences.Any(r => f.ItemSpec.StartsWith(r))).ToArray(); | ||
return true; | ||
} | ||
} | ||
} |
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
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
15 changes: 15 additions & 0 deletions
15
src/Internal.AspNetCore.Sdk/build/FindUnusedReferences.targets
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,15 @@ | ||
<!-- | ||
WARNING: These targets are intended for building Microsoft's ASP.NET Core repos and are not intended | ||
for use outside of Microsoft. | ||
--> | ||
<Project> | ||
<Target Name="FindUnusedReferences" | ||
AfterTargets="CoreCompile" | ||
DependsOnTargets="RunResolvePackageDependencies" | ||
Condition=" '$(EnableFindUnusedReferences)' == 'true' "> | ||
<Sdk_FindUnusedReferences Assembly="@(IntermediateAssembly)" References="@(ReferencePath)" Packages="@(PackageDependencies)" Files="@(FileDefinitions)" > | ||
<Output TaskParameter="UnusedReferences" ItemName="UnusedReferences" /> | ||
</Sdk_FindUnusedReferences> | ||
<Warning Condition="'@(UnusedReferences)' != ''" Text="Unused reference in $(MSBuildProjectFile)/$(TargetFramework) %(UnusedReferences.Identity) from package %(UnusedReferences.PackageName)" /> | ||
</Target> | ||
</Project> |
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
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.
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.
Would it be possible to make this a verifier rule? We really don't have a good way to suppress individual warnings here and I rather not spend time every compilation doing extra work.
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.
Not really, I need list of things that got passed to compiler and it's not easy to infer from assets.json anymore :(.