Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get visual studio to reliably run something pre-build/compilation?

Im having problems getting MSBuild and VisualStudio to work the same based on the same csproj file.

I'm basically trying to make an installer program for a database. The database changes are in xml files in another directory. I need to get these into the program which then at run time will select which ones to apply by querying them.

My idea was to have a pre-build/compile step which zips up the directory and have that zip file as a resource when I run the program. I could also just compile the resource in a release script but this way seemed better as it makes it easier to debug the application.

I have added the following to the csproj file

<Target Name="PreBuildStep">
    <ItemGroup>
        <Patches Include="..\..\Database\**\*" />
    </ItemGroup>
</Target>
<Target DependsOnTargets="PreBuildStep" Name="BeforeBuild" Inputs="@(Patches)" Outputs="Resources\database.zip">
    <Exec Command="7z.bat a Resources\database.zip ..\..\Database -tzip" />
</Target>

This works when running the solution or project from MSBuild. The incremental build parameters (inputs + outputs) also appear to work - if I modify the input it recreates the output, if I don't it doesnt. Likewise if I delete the output file it recreates it.

I just added a batch file on my path to get 7z.bat - all that does is call the 7zip commandline (7z.exe).

The problem is that Visual Studio seems to not run MSBuild and it seems impossible to get it to reliably run anything - I've also tried overriding the depends on of Build and using pre-build events, all with the same behaviour.

Basically what happens is it will work occasionally but if I delete the output file then hitting F5 (to debug which has build checked in the configuration manager) or even ctrl+shift+b to build then it wont get recreated. If I do a rebuild or a clean and build then it works as expected.

Does anyone know how to get this to work? Is it a bug in VisualStudio that I should be reporting? It feels like VisualStudio doesnt call MSBuild unless it decides there have been some changes to code.

like image 943
JonnyRaa Avatar asked Dec 29 '25 23:12

JonnyRaa


1 Answers

Visual Studio uses a feature called "fast up-to-date check" to determine whether a project must be rebuilt. The reason for this is optimization. This process is faster than actual running the msbuild incremental build.

To disable this feature for a particular project add the following property into the project file:

<PropertyGroup>
  <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
</PropertyGroup>
like image 91
Stepan Burguchev Avatar answered Jan 01 '26 12:01

Stepan Burguchev