Skip to content

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

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

Closed
peeveen opened this issue May 14, 2025 · 1 comment · Fixed by #46218

Comments

@peeveen
Copy link

peeveen commented May 14, 2025

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\
@dotnet-policy-service dotnet-policy-service bot added the untriaged Request triage from a team member label May 14, 2025
@ericstj
Copy link
Member

ericstj commented May 15, 2025

I think this might be fixed with #46218 @Forgind @dsplaisted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants