2

I have a Visual Studio solution where I am trying to add the Code Analyzers using build prop file in all projects. I have projects which depends on .NET Core as well as for framework. I have below Directory.Build.Prop file

<Project>
  <Choose>    
    <When Condition="$(UsingMicrosoftNETSdk) == 'true'">
      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
        <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
        <RunCodeAnalysis>true</RunCodeAnalysis>
      </PropertyGroup>
      <ItemGroup>
        <None Include="$(MSBuildThisFileDirectory).editorconfig" Link=".editorconfig" />
      </ItemGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.1">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
      </ItemGroup>
   </When>
    <Otherwise>
    </Otherwise>
  </Choose>
</Project>

Now, I am able to distinguish between the .Net core and framework projects using the property UsingMicrosoftNETSdk as all new core projects use SDK style of projects and that is not the case for Framework projects. But I am not exactly sure what should I add in the Otherwise section to add these analyzers to it.

As the used analyzer is platform independent, I am trying to use the package reference or reference element to add it. But it is not working as expected, and I see analyzer are not working, plus it messes up the other references in the Framework project( Get the warning sign on reference and project doesn't build) .

Also I think it has to do with package reference in framework project being managed in packages.config file, but I am not sure if we can apply something to it from build.props

Chaitanya Gadkari
  • 2,669
  • 4
  • 30
  • 54

1 Answers1

7

Actually, .NET Framework projects support PackageReference NuGet management format. See this official document.

And if you want to use Directory.Build.props file to install some NuGet packages with packages.config NuGet management format for .NET Framework projects, it is impossible because packages.config NuGet management format uses the packages.config to install packages, and proj file cannot modify packages.config file.

But only PackageReference NuGet management can use proj to install NuGet packages without packages.config file.

Therefore, the only consideration now is how to change the .NETt Framework project that has installed some NuGet packages using packages.config.

Suggestion

Before doing this, you could make a backup for all your projects.

1) So open all your .NET Framework projects on VS, and then right-click on every packages.config file-->click Migrate packages.config into PackageReference.

enter image description here

Then, all your .NET Framework projects could use PackageReference as .NET Core projects did.

2) Then, add these for .NET Framework projects on Directory.Build.Prop file like what you did for .NET Core.

<ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.1">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
 </ItemGroup>

Update 1

Before the process, you could make a backup for your solution.

==========================

For ASP.NET framework projects, it can use PackageReference. Just do some modification.

  • First, unload these projects on the Solution Explorer and then open every csproj file of the ASP.NET projects.

Remember to comment the ProjectTypeGuids property like this:

enter image description here

  • Second, then reload the projects and then you can migrate into PackageReference. Right-click on the packages.config and click Migrate packages.config into PackageReference.

When you finish this step, unload your project and make ProjectTypeGuids property back.

Try the following steps for every ASP.NET project.

=========================

For C++ projects, the NuGet package Microsoft.CodeAnalysis.NetAnalyzers is not suitable for them, so you have to make a judgement call to remove the C++ project from your current logic operation.

And C++ projects use PlatformToolset property to build projects, while .NET projects do not use it. So this is a good judgement condition.

Use this:

<Project>
  <Choose>    
    <!--exlcude c++ projects and all the net framework and net core projects can use PackageReference-->
    <When Condition="'$(PlatformToolset)'==''">
      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
        <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
        <RunCodeAnalysis>true</RunCodeAnalysis>
      </PropertyGroup>
      <ItemGroup>
        <None Include="$(MSBuildThisFileDirectory).editorconfig" Link=".editorconfig" />
      </ItemGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.1">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
      </ItemGroup>
   </When>
    <Otherwise>
    </Otherwise>
  </Choose>
</Project>
riQQ
  • 9,878
  • 7
  • 49
  • 66
Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • I am trying to do this, but one of the package says it is not eligible for this migration – Chaitanya Gadkari Dec 14 '20 at 11:35
  • Found the reason, its asp.net project... Migration is not currently available for C++ and ASP.NET projects. So, again, I am back to square one – Chaitanya Gadkari Dec 14 '20 at 14:59
  • Thanks for your feedback. And more info will help us resolve the issue more quickly. I have updated my answer, see `update 1`. More solutions has been provided. – Mr Qian Dec 15 '20 at 02:59
  • Hey, the package did get ported the way you suggested, but when I build the project, it all package reference seems to vanish from the list – Chaitanya Gadkari Dec 17 '20 at 12:51
  • Could it be because of runtime entries in web config? – Chaitanya Gadkari Dec 17 '20 at 17:02
  • The `packagereference` node is under csproj file. Did you know those nuget packages are missing on the reference list of solution explorer? If so, unload your project, then reload your project, use the detailed build log, and I can see the nuget packages under the output window. Make sure that there is no `ExcludeAssets` node under `packagereference` node of `csproj` file, make sure that there is no other restriction node on the `web.config` file. If this do not help, share the `web.config` and the `packagereference` node of csproj file with us. – Mr Qian Dec 18 '20 at 03:14