Home > Blockchain >  Visual Studio constantly triggers MSBuild Targets
Visual Studio constantly triggers MSBuild Targets

Time:12-12

I want to execute a text template before my MSBuild project in Visual Studio. I have added the following to my project file:

<Target Name="TransformOnBuild" BeforeTargets="ResolveProjectReferences">
 <PropertyGroup>
  <_TransformExe>$(MSBuildExtensionsPath)\..\Common7\IDE\TextTransform.exe</_TransformExe>
  <_TextTransform>$(ProjectDir)AssemblyInfo.tt</_TextTransform>
  <_TextTransformResult>$(ProjectDir)AssemblyInfo.cs</_TextTransformResult>
 </PropertyGroup>
 <Exec Command="del &quot;$(_TextTransformResult)&quot;" />
 <Exec Command="&quot;$(_TransformExe)&quot; &quot;$(_TextTransform)&quot; -out &quot;$(_TextTransformResult)&quot;" />
</Target>

This simply deletes my AssemblyInfo.cs and regenerates it from AssemblyInfo.tt.

I use BeforeTargets="ResolveProjectReferences" because I need this file regenerated before any of the referenced projects get built.

Basically, this already works but I have noticed something strange: When I have this in my project file while Visual Studio is open, the AssemblyInfo.cs file constantly dissappears and then reappears. To me it looks like VS repeatedly executes my build target in the background. Of course I don't want it to behave like this. I want it to regenerate the file only when I start a build.

Is there any way to achieve my goal without generating constant CPU load and annoying file-wobbling in the explorer? Maybe a different base target than ResolveProjectReferences?

I use Visual Studio Professional 2022, Version 17.2.6

CodePudding user response:

Update based on latest comments.

You could also try Condition="'$(DesignTimeBuild)' != 'true'". Details/Background.


If you can live withit never being run inside Visual Studio, you can add this condition to the target element:

Condition="'$(BuildingInsideVisualStudio)' != 'true'"

Otherwise you can try this:

<Target Name="TransformOnBuild" BeforeTargets="ResolveProjectReferences"
   Inputs="$(MSBuildProjectDirectory)AssemblyInfo.tt"
   Outputs="$(MSBuildProjectDirectory)AssemblyInfo.cs">
 <PropertyGroup>
 
 <!-- ... -->

</Target>

You can learn more about Inputs/Outputs here. Basically, in this case, it means that the target will only be run, when AssemblyInfo.tt is newer than AssemblyInfo.cs.

Note that VS (for intellisence, etc.) will run targets in the background.

  • Related