visual studio – How to have multiple PreBuild events in VS 2022 Microsoft.NET.Sdk.Web project


I have the situation where I have two different PreBuild events: One to be executed always, anotherone to be exectued only if configuration is ‘Debug’.

In the csproj file I defined it as follow (look the <Target> tags):

    other things ...
      <Generator>PublicResXFileCodeGenerator</Generator>
      <LastGenOutput>CAResources.Designer.cs</LastGenOutput>
      <CustomToolNamespace>CheckAct.Resources</CustomToolNamespace>
    </EmbeddedResource>
  </ItemGroup>

  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="npm install" />
    </Target>
  <Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="'$(Configuration)' == 'Debug'">
      <Exec Command="powershell -Command &quot;&amp; {$filePath="$(ProjectDir)appsettings.Development.local-debug.json";if (!(Test-Path -Path $filePath)) {$machineName=$env:COMPUTERNAME;$jsonContent="{\&quot;ConfigurationServiceEndpoint\&quot;:\&quot;https://"+$machineName+'/api/configurationservice/\&quot;,\&quot;Primus\&quot;:{\&quot;Authority\&quot;:\&quot;https://'+$machineName+'/auth\&quot;}}';Set-Content -Path $filePath -Value $jsonContent -Encoding UTF8;}}&quot;" />
  </Target>

</Project>

As you can see, I’ve defined two PreBuild events. This works fine if I compile in Visual Studio (Debug and release). But when I try to compile over DevOps (release), then I get many errors that ‘JQuery”https://stackoverflow.com/”$’ cannot be found and the log tells me, that TS is complied against TSConfig while without the second build event, the log tells that its compiling against core.

However, it looks, somehow the compile behaves differntly with the second PreBuild event just defined (which is actually not executed due condition).
Does anybody know how to resolve this?

Leave a Reply

Your email address will not be published. Required fields are marked *