msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

dotnet msbuild vs msbuild - the former does not respect -v and -clp parameters, fails copy task.

Open MarkKharitonov opened this issue 5 months ago • 0 comments

Issue Description

I have 3 targets files which I created in order to investigate the behavior of the Copy task when facing temporary Access Denied:

  1. main.targets - cleans up, creates source-file.txt and msbuilds two other targets files in parallel
  2. locker.targets - creates locked-file.txt in the current directory, makes it read-only and waits for the specified time before clearing the attribute.
  3. copier.targets - waits for locked-file.txt to become available and read-only and then attempts to copy source-file.txt over locked-file.txt with the given amount of retries and delays between the retries.

The purpose was to test the effect of the MSBUILDALWAYSRETRY environment variable.

This is not an idle exercise, it simulates the situation we get when building our big application into a single shared bin directory consisting of multiple solutions and hundreds of nuget packages with sometimes conflicting versions of the same transitive package. There is a Microsoft support ticket on the subject 2503050040000167 (resolved).

Anyway, I am observing a weird behavior which I cannot explain:

msbuild - correct behavior

C:\work\copy-task [master]> $env:MSBUILDALWAYSRETRY
1
C:\work\copy-task [master]> msbuild /m /v:m -clp:ShowTimestamp .\main.targets
MSBuild version 17.14.8+a7a4d5af0 for .NET Framework
05:39:01.746     0>
05:39:01.820     3>[COPIER] Waiting for protected file: C:\work\copy-task\locked-file.txt
05:39:02.057     2>[LOCKER] Creating file with access restrictions: C:\work\copy-task\locked-file.txt
                     [LOCKER] File set to read-only. Copy attempts will get ACCESS DENIED for 8000ms...
05:39:02.061     3>[COPIER] Protected file ready after 241ms
C:\work\copy-task\Copier.targets(68,5): warning MSB3894: Got System.UnauthorizedAccessException: Access to the path 'C:\work\copy-task\locked-file.txt' is denied.
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.CopyFileWithLogging(FileState sourceFileState, FileState destinationFileState)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.DoCopyWithRetries(FileState sourceFileState, FileState destinationFileState, CopyFileWithState copy
                   File) copying "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt" and HR is -2147024891
C:\work\copy-task\Copier.targets(68,5): warning MSB3895: Retrying on ERROR_ACCESS_DENIED because environment variable MSBUILDALWAYSRETRY=1
C:\work\copy-task\Copier.targets(68,5): warning MSB3026: Could not copy "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt". Beginning retry 1 in 800ms. Access to the
                    path 'C:\work\copy-task\locked-file.txt' is denied.
C:\work\copy-task\Copier.targets(68,5): warning MSB3894: Got System.UnauthorizedAccessException: Access to the path 'C:\work\copy-task\locked-file.txt' is denied.
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.CopyFileWithLogging(FileState sourceFileState, FileState destinationFileState)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.DoCopyWithRetries(FileState sourceFileState, FileState destinationFileState, CopyFileWithState copy
                   File) copying "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt" and HR is -2147024891
C:\work\copy-task\Copier.targets(68,5): warning MSB3895: Retrying on ERROR_ACCESS_DENIED because environment variable MSBUILDALWAYSRETRY=1
C:\work\copy-task\Copier.targets(68,5): warning MSB3026: Could not copy "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt". Beginning retry 2 in 800ms. Access to the
                    path 'C:\work\copy-task\locked-file.txt' is denied.
C:\work\copy-task\Copier.targets(68,5): warning MSB3894: Got System.UnauthorizedAccessException: Access to the path 'C:\work\copy-task\locked-file.txt' is denied.
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.CopyFileWithLogging(FileState sourceFileState, FileState destinationFileState)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.DoCopyWithRetries(FileState sourceFileState, FileState destinationFileState, CopyFileWithState copy
                   File) copying "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt" and HR is -2147024891
C:\work\copy-task\Copier.targets(68,5): warning MSB3895: Retrying on ERROR_ACCESS_DENIED because environment variable MSBUILDALWAYSRETRY=1
C:\work\copy-task\Copier.targets(68,5): warning MSB3026: Could not copy "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt". Beginning retry 3 in 800ms. Access to the
                    path 'C:\work\copy-task\locked-file.txt' is denied.
C:\work\copy-task\Copier.targets(68,5): warning MSB3894: Got System.UnauthorizedAccessException: Access to the path 'C:\work\copy-task\locked-file.txt' is denied.
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.CopyFileWithLogging(FileState sourceFileState, FileState destinationFileState)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.DoCopyWithRetries(FileState sourceFileState, FileState destinationFileState, CopyFileWithState copy
                   File) copying "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt" and HR is -2147024891
C:\work\copy-task\Copier.targets(68,5): warning MSB3895: Retrying on ERROR_ACCESS_DENIED because environment variable MSBUILDALWAYSRETRY=1
C:\work\copy-task\Copier.targets(68,5): warning MSB3026: Could not copy "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt". Beginning retry 4 in 800ms. Access to the
                    path 'C:\work\copy-task\locked-file.txt' is denied.
C:\work\copy-task\Copier.targets(68,5): warning MSB3894: Got System.UnauthorizedAccessException: Access to the path 'C:\work\copy-task\locked-file.txt' is denied.
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.CopyFileWithLogging(FileState sourceFileState, FileState destinationFileState)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.DoCopyWithRetries(FileState sourceFileState, FileState destinationFileState, CopyFileWithState copy
                   File) copying "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt" and HR is -2147024891
C:\work\copy-task\Copier.targets(68,5): warning MSB3895: Retrying on ERROR_ACCESS_DENIED because environment variable MSBUILDALWAYSRETRY=1
C:\work\copy-task\Copier.targets(68,5): warning MSB3026: Could not copy "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt". Beginning retry 5 in 800ms. Access to the
                    path 'C:\work\copy-task\locked-file.txt' is denied.
C:\work\copy-task\Copier.targets(68,5): warning MSB3894: Got System.UnauthorizedAccessException: Access to the path 'C:\work\copy-task\locked-file.txt' is denied.
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.CopyFileWithLogging(FileState sourceFileState, FileState destinationFileState)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.DoCopyWithRetries(FileState sourceFileState, FileState destinationFileState, CopyFileWithState copy
                   File) copying "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt" and HR is -2147024891
C:\work\copy-task\Copier.targets(68,5): warning MSB3895: Retrying on ERROR_ACCESS_DENIED because environment variable MSBUILDALWAYSRETRY=1
C:\work\copy-task\Copier.targets(68,5): warning MSB3026: Could not copy "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt". Beginning retry 6 in 800ms. Access to the
                    path 'C:\work\copy-task\locked-file.txt' is denied.
C:\work\copy-task\Copier.targets(68,5): warning MSB3894: Got System.UnauthorizedAccessException: Access to the path 'C:\work\copy-task\locked-file.txt' is denied.
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.CopyFileWithLogging(FileState sourceFileState, FileState destinationFileState)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.DoCopyWithRetries(FileState sourceFileState, FileState destinationFileState, CopyFileWithState copy
                   File) copying "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt" and HR is -2147024891
C:\work\copy-task\Copier.targets(68,5): warning MSB3895: Retrying on ERROR_ACCESS_DENIED because environment variable MSBUILDALWAYSRETRY=1
C:\work\copy-task\Copier.targets(68,5): warning MSB3026: Could not copy "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt". Beginning retry 7 in 800ms. Access to the
                    path 'C:\work\copy-task\locked-file.txt' is denied.
C:\work\copy-task\Copier.targets(68,5): warning MSB3894: Got System.UnauthorizedAccessException: Access to the path 'C:\work\copy-task\locked-file.txt' is denied.
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.CopyFileWithLogging(FileState sourceFileState, FileState destinationFileState)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.DoCopyWithRetries(FileState sourceFileState, FileState destinationFileState, CopyFileWithState copy
                   File) copying "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt" and HR is -2147024891
C:\work\copy-task\Copier.targets(68,5): warning MSB3895: Retrying on ERROR_ACCESS_DENIED because environment variable MSBUILDALWAYSRETRY=1
C:\work\copy-task\Copier.targets(68,5): warning MSB3026: Could not copy "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt". Beginning retry 8 in 800ms. Access to the
                    path 'C:\work\copy-task\locked-file.txt' is denied.
C:\work\copy-task\Copier.targets(68,5): warning MSB3894: Got System.UnauthorizedAccessException: Access to the path 'C:\work\copy-task\locked-file.txt' is denied.
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.CopyFileWithLogging(FileState sourceFileState, FileState destinationFileState)
                   C:\work\copy-task\Copier.targets(68,5): warning MSB3894:    at Microsoft.Build.Tasks.Copy.DoCopyWithRetries(FileState sourceFileState, FileState destinationFileState, CopyFileWithState copy
                   File) copying "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt" and HR is -2147024891
C:\work\copy-task\Copier.targets(68,5): warning MSB3895: Retrying on ERROR_ACCESS_DENIED because environment variable MSBUILDALWAYSRETRY=1
C:\work\copy-task\Copier.targets(68,5): warning MSB3026: Could not copy "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt". Beginning retry 9 in 800ms. Access to the
                    path 'C:\work\copy-task\locked-file.txt' is denied.
05:39:10.067     2>[LOCKER] Read-only restriction removed - copy should now succeed
C:\work\copy-task [master]> cat .\locked-file.txt
This content will overwrite the locked file
C:\work\copy-task [master]>

dotnet build - incorrect behavior

C:\work\copy-task [master]> $env:MSBUILDALWAYSRETRY
1
C:\work\copy-task [master]> dotnet build -m -v:m -clp:ShowTimestamp .\main.targets
Restore complete (0.0s)
  Copier failed with 1 error(s) (0.8s)
    C:\work\copy-task\Copier.targets(68,5): error MSB3021: Unable to copy file "C:\work\copy-task\source-file.txt" to "C:\work\copy-task\locked-file.txt". Access to the path 'C:\work\copy-task\locked-file.txt' is denied.

Build failed with 1 error(s) in 9.0s
C:\work\copy-task [master]>

So not only does it ignore my verbosity command line argument, but it just fails.

I am attaching the 3 targets file in a zip.

copy-task.zip

Steps to Reproduce

  1. Unzip the archive.
  2. $env:MSBUILDALWAYSRETRY=1
  3. msbuild /m /v:m -clp:ShowTimestamp .\main.targets
  4. dotnet build -m -v:m -clp:ShowTimestamp .\main.targets

Expected Behavior

dotnet build behaves the same as msbuild.

Actual Behavior

dotnet build fails and ignores verbosity command line argument.

Analysis

No response

Versions & Configurations

C:\work\copy-task [master]> msbuild -version
MSBuild version 17.14.8+a7a4d5af0 for .NET Framework
17.14.8.25101
C:\work\copy-task [master]> dotnet --version
9.0.300
C:\work\copy-task [master]>

MarkKharitonov avatar Jun 09 '25 09:06 MarkKharitonov