'Distributing .editorConfig custom settings through nuget package

I am having an issue trying to implement compiler-based code analysis in my C# .NetFramework solution. I decided to use Microsoft.CodeAnalysis.NetAnalyzers nuget package with some custom .editorconfig ruleset that I do not want to keep directly in the consuming project. I created another nuget with the .editorconfig and the idea is to copy the file to each consuming project before it is built in order to trigger the analysis (during build).

I tried an approach (described here) where the .editorconfig is copied to csproj location in a beforeBuild task defined in .props file

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <PropertyGroup Condition="('$(Configuration)' == 'Debug')">
    <RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
  </PropertyGroup>  
  <ItemGroup>
    <EditorConfigFilesToCopy Include="$(MSBuildThisFileDirectory)..\content\.editorconfig" />
  </ItemGroup>
  <Target Name="CopyEditorConfig" BeforeTargets="BeforeBuild">
    <Message Text="Copying the .editorconfig file from '@(EditorConfigFilesToCopy)' to '$(MSBuildProjectDirectory)'"></Message>
      <Copy 
        SourceFiles="@(EditorConfigFilesToCopy)"
        DestinationFolder="$(MSBuildProjectDirectory)"
        SkipUnchangedFiles="true"
        UseHardlinksIfPossible="false" />
  </Target> 
</Project>

Unfortunately, the file seems to be copied too late as the msbuild ignores it during the build itself and does not fail due to CA violations as I would expect. If the file had been copied manually before running msbuild, it works.

Do You have any idea why it is like that and how to handle this issue properly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source