dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.21k stars 1.35k forks source link

[Feature Request]: Add `CopyExact` to compliment `PreserveNewest` and `CopyAlways` #9350

Open zivkan opened 11 months ago

zivkan commented 11 months ago

Summary

PreserveNewest makes a lot of sense for projects being compiled, however, it causes problems when changing package versions to a package which is older than the one previously used. Currently, it's necessary to clean the output directory to make this scenario work.

Background and Motivation

Root cause of this issue: https://github.com/NuGet/Home/issues/12955

The customer is running dotnet publish on their sln file, with the -o parameter, so all projects in the solution are being published to the same directory. MSBuild is doing the right thing and publishing projects in order of the project graph. The first project references a package with an older package version, but was compiled & published more recently than the newer package version. Therefore, whatever.dll from the lower package version has a newer timestamp than the same dll from the higher package version.

I don't know if it's the .NET SDK or msbuild, but in any case PreserveNewest is being used, and as a result, the lower assembly version is being retained in the publish directory, leading to a runtime failure.

Proposed Feature

MSBuild should introduce a new "copy to" semantic for "copy if the timestamps are not an exact match". Afterwards, I imagine that the .NET SDK and NuGet.BuildTools need to be modified to use the new enum value for assemblies (and other assets?) coming from packages, to ensure that when older packages are used, the files from the currently referenced package is used.

An alternative is that assets from packages should use CopyAlways, but that might harm build performance when the copy is unnecessary.

Alternative Designs

No response

AR-May commented 11 months ago

MSBuild triage: Note for the design: be aware about app config file. Do not opt in the suggested behavior for it.

KalleOlaviNiemitalo commented 5 months ago

This is the same as https://github.com/dotnet/msbuild/issues/8743.