如何将 Target 嵌套在 .csproj 文件中的另一个 Target 中?

How to nest a Target within another Target in the .csproj file?

长话短说,我需要在 nuget 恢复任务完成后终止 Azure 管道 Ubuntu 代理上的 VBCSCompiler.exe。在 windows2019 代理上我不需要这样做但是在 ubuntu 我 运行 遇到了一个问题:

/home/vsts/work/1/s/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.8/build/net45/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props(17,5): 
warning MSB3021: Unable to copy file "/home/vsts/work/1/s/packages/Microsoft.Net.Compilers.2.4.0/build/../tools/csc.exe" to "/bin/roslyn/csc.exe". Access to the path '/bin/roslyn' is denied. [/home/vsts/work/1/s/Bobby.ProjectA/Bobby.ProjectA.csproj]

所以根据 Levi 在 中的说法,我需要在 .csproj 文件中添加 <Target Name="CheckIfShouldKillVBCSCompiler"> 行。我这样添加它们:

...
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
        Other similar extension points exist, see Microsoft.Common.targets.
   <Target Name="BeforeBuild">
   </Target>
   <Target Name="AfterBuild">
   </Target> -->
   <Target Name="CheckIfShouldKillVBCSCompiler">
     <PropertyGroup>
       <ShouldKillVBCSCompiler>true</ShouldKillVBCSCompiler>
     </PropertyGroup>
   </Target>
 </Project>

但这并没有对解锁 /bin/roslyn 路径起到任何作用。

我认为这必须添加到 BeforeBuild 目标行中(例如嵌套它们),所以我尝试这样做:

   <Target Name="BeforeBuild">
       <Target Name="CheckIfShouldKillVBCSCompiler">
         <PropertyGroup>
           <ShouldKillVBCSCompiler>true</ShouldKillVBCSCompiler>
         </PropertyGroup>
       </Target>
   </Target>

但我最终遇到了错误: error MSB4067: The element <PropertyGroup> beneath element <Target> is unrecognized.

我从this answer那里了解到,这是无法实现的。

目标不能嵌套在另一个目标中。相反,目标可以这样修改:

 <Target Name="CheckIfShouldKillVBCSCompiler"  BeforeTargets="build">
   <PropertyGroup>
     <ShouldKillVBCSCompiler>true</ShouldKillVBCSCompiler>
   </PropertyGroup>
 </Target>

但是,这无助于解决我最初遇到的构建问题。发现的是另一种方法