You are debugging in Visual Studio, you stop the debugger and hit F5 again. Now Visual Studio is re-building several of your projects and it takes a long time before the debugger launches again. What gives?
Unnecessary builds in Visual Studio are a problem I’ve seen over and over in my projects, and it’s particularly annoying for large solutions. In theory, unless you make some code changes there should be no need to rebuild and running again should be super fast. And this type of micro-interruptions can be really disruptive to your flow.
Fix unnecessary builds in visual studio
Fortunately VS has a simple way of diagnosing and fixing this. Go to Tools -> Options -> Projects and Solutions -> Build and Run and change the MSBuild project output verbosity from Minimal to Diagnostic:
Now Build and look at the Output window. There will be *a lot* of output, but luckily the first line is all you need to look at. It’ll show the reason why a project is getting rebuilt. For example:
1>Project 'VSRebuild' is not up to date. Input file 'c:\users\documents\visual studio 2017\projects\vsrebuild\program.cs' is modified after output file ''.
In this case this is a perfectly valid reason to re-build (a code file changed). But if your projects keep getting re-built, here you can find the culprit. One of the most common ones is Resources or Content files being set to Copy always:
1>Project 'VSRebuild' is not up to date. Project item 'C:\Users\Documents\Visual Studio 2017\Projects\VSRebuild\Cursor1.cur' has 'Copy to Output Directory' attribute set to 'Copy always'.
You can fix this by editing the properties of the file and changing it to Copy if newer.
Build a couple of times and the second time VS should simply say the project is up to date and not build again.
Move fast
Moving fast is a core value in Facebook’s engineering culture and something that really resonated with me when I was there.
This process should help you diagnose problematic projects that keep re-building. Don’t forget to turn the build output verbosity back to Minimal once you are done. Otherwise your builds will take longer due to all the additional output being generated.
Bonus tip: If you have large projects or solutions you might be better off editing your project files in a text editor to grep/replace:
<Content Include="Cursor1.cur">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
With PreserveNewest.
Bonus tip 2: If you just want to run your project and don’t need to debug, get in the habit of using Ctrl-F5 (Start Without Debugging). Not only will your program start faster, but will execute faster as well without the debugger attached. You can always attach to it later on if needed (Debug->Attach To a Process).
Hope these tips help you avoid unnecessary builds in Visual Studio and make your development cycle faster. Knowing your IDE really well is part of your Technology Skills and a key element of being an efficient engineer. What are your favorite VS efficiency tips? Leave them in the comments!