Visual Studio Rebuilds unmodified projects

前端 未结 17 1861
夕颜
夕颜 2020-12-04 08:49

So, as the title reads, I have a VS2010 solution with ~50 projects in it right now. If I make a change to a \"top level\" project that nothing references then VS still rebui

相关标签:
17条回答
  • 2020-12-04 09:08

    I had the same issue in VS 2015.

    What did the trick for me is:

    1. One project was referencing itself copy in some other project bin (magic, yes). This kind of stuff could be found when switching to diagnostic build output (in build options) and then trying to build projects one by one from the top of projects hierarchy - if you see the project that rebuilds even if nothing has been changed then see it's references.
    2. I've changed all "copy always" files in all projects to "copy if newer". Basically, in all .csproj files replace <CopyToOutputDirectory>Always</CopyToOutputDirectory> to <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    3. Then I've disabled NTFS tunneling as described in this article with this powershell script:

    New-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "MaximumTunnelEntries" -Value 0 -PropertyType "DWord"

    After that I needed on rebuild and it seems working for now.

    0 讨论(0)
  • 2020-12-04 09:10

    While I don't think this is a fix, it is a workaround that has worked for my situation...

    I originally had about 5 projects out of 50 that contained a Resources section. These projects would always be rebuilt and thus anything that they depended on would also be rebuilt. One of those 5 projects was a "base" level library that 48 of the other projects referenced, thus 96% of my project would be rebuilt every time even if it didn't need it.

    My workaround was to use dependency injection, interfaces, and a dedicated "Resources" project. Instead of having those 5 projects reference their own Resources object, I created an interface in each project that would supply the desired resources. Then, the classes that needed those resources would require that interface be passed in during their creation in the constructor (constructor injection).

    I then created a separate "Resources" project that had an actual Resources section like normal. This project only contained the resources themselves, and a class for each interface that was needed to provide those resources via an interface. This project would reference every other project that had a resource dependency and implement the interface that the project needed.

    Finally, in my "Top Level" project which nothing referenced (and where the exe was actually built and my composition root lives) I referenced the "Resources" project, wired up the DI, and away we went.

    This means that only two projects (the "Resources" and the "Top Level") will be rebuilt every time, and if I do a partial build (Shift-F6) then they won't get rebuilt at all.

    Again, not a great work around, but with 48 projects being built every time a build would take about 3 minutes, so I was losing 30 to 90 minutes a day with needless rebuilds. It took awhile to refactor, but I think it was a good investment.

    Here is a simplified diagram. Note that the dependencies from Main.exe to Proj1 and Proj2 are not shown in order to reduce clutter.

    Diagram of solution

    With this design, I can do a build of Proj1 or Proj2 without triggering a full rebuild, since they don't have any dependencies on a Resources section. Only Main knows about the Resources implementation.

    0 讨论(0)
  • 2020-12-04 09:17

    In my case the culprit was "Copy Local" setting of a referenced dll set to true and "Copy to Output Directory" setting a file set to Copy always.

    0 讨论(0)
  • 2020-12-04 09:19

    Here is an answer from VS2010 always rebuilds solution?

    This issue is solved by changing the project files, cleaning solution, deleting all bin folders by hand, restarting Visual studio and rebuilding everything.

    0 讨论(0)
  • 2020-12-04 09:19

    As others have noticed, a likely reason is that CopyToOutputDirectory is set to Always. This can be fixed simultaneously in all project files by applying the powershell script below:

    $folder = "C:\My\Solution\Folder"
    $csvFiles = Get-ChildItem $folder *.csproj -rec
    foreach ($file in $csvFiles)
    {
        (Get-Content $file.PSPath -Encoding UTF8 -Raw) |
        Foreach-Object { $_ -replace "<CopyToOutputDirectory>Always</CopyToOutputDirectory>", "<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>" } |
        Set-Content $file.PSPath -Encoding UTF8 -NoNewline
    }
    
    0 讨论(0)
提交回复
热议问题