Visual Studio C# projects force a rebuild when switching from debug to release and vice-versa
Asked Answered
I

2

10

It seems Visual Studio 2012 C# projects force a rebuild every time you switch from Debug to Release or vice-versa, even if nothing has changed in the project. This does not happen for a standalone project -- but if you add a single project reference to a class library it starts exhibiting the behavior every time you change to a different build configuration. I've tracked it down to the generation of the force.build file (in obj\Debug or obj\Release) whenever the configuration selection changes. Why is this? Can it be disabled? I would think that switching configurations should not in and of itself require a rebuild. I cannot find any mention of the "force.build" file and/or how to disable this "feature" in any documentation.

To recreate the issue:

  1. Click File/New Project...
  2. Create a new Visual C# -> Console Application. Leave the generated code as-is.
  3. Right-click on the new solution in the solution explorer and select Add -> New Project...
  4. Choose Visual C# -> Class Library. Leave the generated code as-is.
  5. Right-click on the ConsoleApplication1 in the solution explorer and select Add Reference...
  6. Check the box next to ClassLibrary1 in Solution -> Projects and hit OK.
  7. Build the project in Debug configuration. It builds, as expected.
  8. Switch to Release configuration and build again. It builds, as expected.
  9. Switch back to Debug and build again. It builds, and it shouldn't. It's already been built, right? All we did was change the target configuration selection.

Is this a bug? Is there a workaround?

Iorgos answered 5/10, 2015 at 19:13 Comment(1)
You mis-interpret what is going on. There's an extra layer inside the IDE that micro-optimizes builds. It never even starts MSBuild when it knows that building is not necessary. Kinda matters, MSBuild looks at a lot of files so isn't very fast on a machine with a slow disk. But every micro-optimization has its limits, it just remembers the last build. Nothing actually gets built, MSBuild goes through all the motions but then reports "1 up-to-date" and no files actually get changed. Takes a fraction of a second with an SSD. Not a bug, you don't have a real problem.Mirthless
T
2

I admit i've never paid much attention to it, but i tried just for curiosity (Visual Studio 2012 Update 4).

With a single project like you described, i tried switching configurations, and appearently, when i change the configuration, the main project builds again:

========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

But if i look at the output folder, no file is being updated (the changed time of each file it's the same as the previous build), except for the vshost.exe (i disabled it, but got the same message). I tried then to set the build log to be more verbose (Tools - Options Projects and Solutions - build and Run - MSBuild project build output verbosity - Change from "Minimal" to "Normal" or "Detailed"):

enter image description here

Then i got the following output:

1>------ Build started: Project: ConsoleApplication1, Configuration: Release Any CPU ------
1>Build started 06/10/2015 00:26:50.
1>GenerateTargetFrameworkMonikerAttribute:
1>Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
1>CoreCompile:
1>Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files.
1>_CopyAppConfigFile:
1>Skipping target "_CopyAppConfigFile" because all output files are up-to-date with respect to the input files.
1>CopyFilesToOutputDirectory:
1>  ConsoleApplication1 -> c:\users\user\documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\bin\Release\ConsoleApplication1.exe
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:00.09
========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

So it seems that the project doesn't really get rebuilded every time. If you find instead that everything gets rebuilded (check the changed time on output dir files), then try to increase the build log verbosity as i described and see what is happening. Hope it helps.

Truncated answered 5/10, 2015 at 22:36 Comment(1)
The build procedure does initiate again. For projects with time consuming pre-build or post-build tasks, this becomes a real problem. It seems there is no prescribed answer here.Sarcastic
T
1

It indeed seems to be a bug (VS 2017 and VS 2019):
https://developercommunity.visualstudio.com/t/changing-solution-configuration-rebuilds-dependent/726161

A file build.force in the obj folder is created when switching configuration (x86/x64 or Debug/Release). The creation of this file is wrong. The link above describes the issue and the response from Microsoft ("will not be fixed, move to .Net Core").

Tonsillotomy answered 13/3, 2021 at 21:57 Comment(1)
Thanks, very helpful information.Arlindaarline

© 2022 - 2024 — McMap. All rights reserved.