Skip to content

Commit

Permalink
Enable LockCheck on .NET on Windows (#7436)
Browse files Browse the repository at this point in the history
Fixes #7327 by supporting LockCheck on .NET 6 (behind a runtime OS
check).
  • Loading branch information
rainersigwald committed Apr 21, 2022
1 parent e0249f0 commit 4b78001
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 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
16 changes: 9 additions & 7 deletions src/Tasks/Copy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
Expand Down 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 && ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_4))
{
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 4b78001

Please sign in to comment.