Skip to content

DependencyContent.*.RuntimeLibraries is returning libraries that are not used during runtime (code analyzers, build tasks, etc) #48944

@peeveen

Description

@peeveen

Obtaining a list of runtime libraries via DependencyContext.Default.RuntimeLibraries returns a list that includes non-runtime libraries such as code analyzers, custom MSBuild task assemblies, etc.

Starting from a basic dotnet new console app (.net 9) ...
I added these references:

  <ItemGroup>
   <PackageReference Include="Microsoft.Extensions.DependencyModel" Version="9.0.5" />
   <PackageReference Include="SonarAnalyzer.CSharp" Version="*">
      <ExcludeAssets>runtime</ExcludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

Program.cs looks like this:

using Microsoft.Extensions.DependencyModel;

var runtimeLibraries = DependencyContext.Default?.RuntimeLibraries;
if (runtimeLibraries != null) {
	var codeAnalyzerIsRuntimeLibrary = runtimeLibraries.Any(r => r.Name.Contains("SonarAnalyzer"));
	if (codeAnalyzerIsRuntimeLibrary)
		throw new InvalidOperationException("SonarAnalyzer should NOT be considered as a runtime library.");
}

The exception is always thrown.
The code analyzer assembly is NOT being copied to the output folder (which is good and correct).
But the code analyzer assembly IS being included in the .deps.json file (which I don't think is right).

.NET SDK:
 Version:           9.0.300
 Commit:            15606fe0a8
 Workload version:  9.0.300-manifests.af4147de
 MSBuild version:   17.14.5+edd3bbf37

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.26100
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\9.0.300\

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions