0

Once I added Fody.Costura to my project, my post build event that was copying the resulting assembly into a different location started failing with access denied message. That makes sense since Costura uses MSBuild to embed the assemblies. Is there a way to force my post builds to execute after Costura is finished? Example of a post build command:

copy /Y "$(TargetPath)" "%ALLUSERSPROFILE%\Autodesk\Revit\Addins\2019\HOK-Addin.bundle\Contents"
konrad
  • 3,544
  • 4
  • 36
  • 75
  • Not likely to be Fody that causes this problem. Temporarily disable the installed anti-malware product and try again. Do document what you use. – Hans Passant Oct 08 '18 at 20:55
  • Actually, I was able to fix this by doing a custom `Target` and using the `Copy` routine. I just made sure that I set my `AfterTargets` to `AfterBuild;NonWinFodyTarget` and that did the trick. Before that I was using just the standard post build event that must have been executing before Fody finished its job, and files were still locked. – konrad Oct 08 '18 at 21:09
  • The Copy build task is a lot smarter, it will retry the copy repeatedly on an "access denied" failure. You are not the first programmer with anti-malware induced trouble. Please do document what you use, these kind of questions don't stop coming until we learn what's good and what's crap-to-avoid. – Hans Passant Oct 08 '18 at 21:56
  • I am curious why you think its caused by anti malware software. Post Build Commands worked fine before i added Fody. The issue is with locked/denied access to the file, likely because Fody is using it when command fires. I agree that Tasks are much better, but i doubt that anti-malware is involved. Care to explain your thinking on this? – konrad Oct 08 '18 at 23:28

1 Answers1

1

Basically the solution to my own question is the following.

  <Target Name="CopyFiles" AfterTargets="AfterBuild;NonWinFodyTarget">
    <Message Text="Signing file..." Importance="high" />
    <Exec Command="&quot;C:\Program Files (x86)\Windows Kits\10\bin\10.0.17134.0\x64\signtool.exe&quot; sign /c &quot;Code Signing - DTM&quot; /v &quot;$(TargetPath)&quot;" />
    <Message Text="Copy files..." Importance="high" />
    <Message Text="$(TargetPath) &gt; $(ALLUSERSPROFILE)\Autodesk\Revit\Addins\$(Configuration)\HOK-Addin.bundle\Contents" Importance="high" />
    <Message Text="$(TargetDir)$(TargetName).addin &gt; $(ALLUSERSPROFILE)\Autodesk\Revit\Addins\$(Configuration)" Importance="high" />
    <Copy SourceFiles="$(TargetPath)" DestinationFolder="$(ALLUSERSPROFILE)\Autodesk\Revit\Addins\$(Configuration)\HOK-Addin.bundle\Contents" ContinueOnError="true" />
    <Copy SourceFiles="$(TargetDir)$(TargetName).addin" DestinationFolder="$(ALLUSERSPROFILE)\Autodesk\Revit\Addins\$(Configuration)" ContinueOnError="true" />
  </Target>

What I did, was to replace the standatd Post Build Command that runs Command Line routines, with a MSBuild Target and a Task.Giving it flags to run after Build is finished and Fody is done merging assemblies resolves my issue.

What also helps is the fact that Tasks have flags like ContinueOnError="true" that allow the task to keep trying until the file is available (if that was the issue) as opposed to command line utilities that would just fail.

Cheers!

konrad
  • 3,544
  • 4
  • 36
  • 75