Skip to content

Commit

Permalink
Avoid warning for unused indirect project references (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfederm authored Sep 9, 2023
1 parent 467da78 commit dd16a92
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/E2ETests/E2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ public void UnusedProjectReference()
});
}

[TestMethod]
public void UnusedTransitiveProjectReference()
{
RunMSBuild(
projectFile: "Library/Library.csproj",
expectedWarnings: new[]
{
// Only the Dependency gets the warning. Library doesn't get penalized for a transitive dependency.
new Warning("RT0002: ProjectReference ../TransitiveDependency/TransitiveDependency.csproj can be removed", "Dependency/Dependency.csproj"),
});
}

[TestMethod]
public void UnusedDirectAndTransitiveProjectReference()
{
RunMSBuild(
projectFile: "Library/Library.csproj",
expectedWarnings: new[]
{
// Both the Library and Dependency get the warning since both directly referenced it.
new Warning("RT0002: ProjectReference ../TransitiveDependency/TransitiveDependency.csproj can be removed", "Dependency/Dependency.csproj"),
new Warning("RT0002: ProjectReference ../TransitiveDependency/TransitiveDependency.csproj can be removed", "Library/Library.csproj"),
});
}

[TestMethod]
public void UsedReferenceHintPath()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Dependency
{
public static class Foo
{
public static string Bar() => "Bar";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../TransitiveDependency/TransitiveDependency.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Library
{
public static class Foo
{
public static string Bar() => Dependency.Foo.Bar();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../Dependency/Dependency.csproj" />
<ProjectReference Include="../TransitiveDependency/TransitiveDependency.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TransitiveDependency
{
public static class Foo
{
public static string Baz() => "Baz";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Dependency
{
public static class Foo
{
public static string Bar() => "Bar";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../TransitiveDependency/TransitiveDependency.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Library
{
public static class Foo
{
public static string Bar() => Dependency.Foo.Bar();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../Dependency/Dependency.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TransitiveDependency
{
public static class Foo
{
public static string Baz() => "Baz";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>

</Project>
10 changes: 10 additions & 0 deletions src/Tasks/CollectDeclaredReferencesTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ public override bool Execute()
{
foreach (ITaskItem projectReference in ProjectReferences)
{
// Weirdly, NuGet restore is actually how transitive project references are determined and they're
// added to to project.assets.json and collected via the IncludeTransitiveProjectReferences target.
// This also adds the NuGetPackageId metadata, so use that as a signal that it's transitive.
bool isTransitiveDependency = !string.IsNullOrEmpty(projectReference.GetMetadata("NuGetPackageId"));
if (isTransitiveDependency)
{
// Ignore transitive project references since the project doesn't have direct control over them.
continue;
}

string projectReferenceAssemblyName = new AssemblyName(projectReference.GetMetadata("FusionName")).Name;
string referenceProjectFile = projectReference.GetMetadata("OriginalProjectReferenceItemSpec");

Expand Down

0 comments on commit dd16a92

Please sign in to comment.