question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional PackageReference version splitting analyzer presence fails

See original GitHub issue

If a project references two different versions of the same package in two different TargetFrameworks, and the package has added or removed a Roslyn analyzer, the analyzer will be added from both versions of the package, even the one where it’s not present.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net6.0;net472</TargetFrameworks>
    <RootNamespace>two_systemtextjson</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Text.Json" Version="4.7.0" Condition=" '$(TargetFramework)' == 'net472' " />
    <PackageReference Include="System.Text.Json" Version="6.0.0-preview.4.21253.7" Condition=" '$(TargetFramework)' == 'net6.0' " />
  </ItemGroup>

</Project>
two-systemtextjson on master .NET v6.0.100-preview.5.21302.13 🎯 net6.0;net472 
❯ dotnet build  
Microsoft (R) Build Engine version 17.0.0-preview-21302-02+018bed83d for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored S:\work\two-systemtextjson\two-systemtextjson.csproj (in 135 ms).
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
CSC : error CS0006: Metadata file 'C:\Users\raines\.nuget\packages\system.text.json\4.7.0\analyzers\dotnet\cs\System.Text.Json.SourceGeneration.dll' could not be found [S:\work\two-systemtextjson\two-systemtextjson.csproj]
  two-systemtextjson -> S:\work\two-systemtextjson\bin\Debug\net6.0\two-systemtextjson.dll

Build FAILED.

CSC : error CS0006: Metadata file 'C:\Users\raines\.nuget\packages\system.text.json\4.7.0\analyzers\dotnet\cs\System.Text.Json.SourceGeneration.dll' could not be found [S:\work\two-systemtextjson\two-systemtextjson.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:01.20

Because 4.7.0 of that package didn’t have an analyzer/source generator, the file isn’t found in that package version. The reference is only there because it was found in the other version.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:19 (11 by maintainers)

github_iconTop GitHub Comments

8reactions
Eiloncommented, Jun 14, 2021

We just ran into this with the newly-introduced analyzer in Microsoft.Extensions.Logging.Abstractions 6.0.0-preview.5.21301.5. We found a fairly easy workaround for this, as long as you don’t actually need the analyzer in the project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netstandard2.1;net6.0-android</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup Condition="'$(TargetFramework.Contains(net6.0))' == 'true'">
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0-preview.5.21301.5" />
  </ItemGroup>
  <ItemGroup Condition="'$(TargetFramework.Contains(net6.0))' != 'true'">
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
  </ItemGroup>

  <!-- Workaround for this bug (replace the analyzer name with the one you need to exclude (filename only, no extension) -->
  <Target Name="RemoveLoggingAnalyzer" BeforeTargets="CoreCompile">
    <ItemGroup>
      <Analyzer Remove="@(Analyzer)" Condition="%(FileName) == 'Microsoft.Extensions.Logging.Generators'" />
    </ItemGroup>
  </Target>
</Project>
4reactions
dsplaistedcommented, Jul 13, 2021

I went ahead and sent a PR to fix this: #18877

Thanks to @ericst for identifying where and what the fix was.

I still would really like to see NuGet fix this class of issue by treating analyzers as a proper asset type, though I recognize that’s a lot more complicated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

.net - Problems with conditional PackageReference & ...
The dependency version of a package is the version of the project when/if it packed itself. Give it a go, use only project...
Read more >
Creating packages
Learn how to create packages in Dart. ... To conditionally import or export, you need to check for the presence of dart:* libraries....
Read more >
Writing R Extensions
Such packages cannot be required to check the package: any tests which use them must be conditional on the presence of the package....
Read more >
Performance Improvements in .NET 7
NET 7 is fast. Really fast. This post deep-dives into hundreds of performance improvements that contributed to that reality.
Read more >
A comparison of the conditional inference survival forest ...
Despite the fact that the CIF survival model has been identified to reduce bias in covariate selection for splitting in survival forest models, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found