Skip to content

Commit

Permalink
Enable LockCheck on .NET on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
rainersigwald committed Mar 3, 2022
1 parent 719247e commit 281021a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/Tasks.UnitTests/Copy_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -822,9 +822,11 @@ public void DoRetryWhenDestinationLocked()
engine.AssertLogContains("MSB3021"); // copy failed
engine.AssertLogContains("MSB3026"); // DID retry

#if !RUNTIME_TYPE_NETCORE && !MONO
engine.AssertLogContains(Process.GetCurrentProcess().Id.ToString()); // the file is locked by the current process
#endif
if (NativeMethodsShared.IsWindows)
{
engine.AssertLogContains(Process.GetCurrentProcess().Id.ToString()); // the file is locked by the current process
}

Assert.Equal(2, engine.Errors); // retries failed and the actual failure
Assert.Equal(10, engine.Warnings);
}
Expand Down
14 changes: 8 additions & 6 deletions src/Tasks/Copy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -899,20 +899,22 @@ private bool DoCopyWithRetries(FileState sourceFileState, FileState destinationF
private static string GetLockedFileMessage(string file)
{
string message = string.Empty;
#if !RUNTIME_TYPE_NETCORE && !MONO

try
{
var processes = LockCheck.GetProcessesLockingFile(file);
message = !string.IsNullOrEmpty(processes)
? ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("Copy.FileLocked", processes)
: String.Empty;
if (NativeMethodsShared.IsWindows)
{
var processes = LockCheck.GetProcessesLockingFile(file);
message = !string.IsNullOrEmpty(processes)
? ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("Copy.FileLocked", processes)
: String.Empty;
}
}
catch (Exception)
{
// Never throw if we can't get the processes locking the file.
}
#endif

return message;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Tasks/LockCheck.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Taken from https://github.com/cklutz/LockCheck, MIT license.
// Copyright (C) Christian Klutz

#if !RUNTIME_TYPE_NETCORE && !MONO

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;

#nullable disable

namespace Microsoft.Build.Tasks
{
[SupportedOSPlatform("windows")]
internal class LockCheck
{
[Flags]
Expand Down Expand Up @@ -355,5 +355,3 @@ private static Exception GetException(int res, string apiName, string message)
}
}
}

#endif

0 comments on commit 281021a

Please sign in to comment.