Skip to content

Commit

Permalink
[msbuild] Make the GetFileSystemEntries task actually cancellable. (#…
Browse files Browse the repository at this point in the history
…20209)

* Enumerate files and directories instead of fetching them all in one go. This
  is more efficient when there are a lot of files and directories to process,
  but most importantly it'll also make enumeration cancellable.
* Stop enumerating if cancellation was requested.
* Add a few logging statements.

This might at least help to get better diagnostic information if for some
reason this task takes too long to execute. Ref: #20183.
  • Loading branch information
rolfbjarne committed Mar 1, 2024
1 parent a8a0132 commit 8ce0574
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions msbuild/Xamarin.Localization.MSBuild/MSBStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,11 @@
</comment>
</data>

<data name="E7119" xml:space="preserve">
<value>Task execution was cancelled.</value>
<comment></comment>
</data>

<data name="E7120" xml:space="preserve">
<value>Unable to compute the remote generator properties. Please file an issue at https://github.com/xamarin/xamarin-macios/issues/new and attach the following file: {0}</value>
<comment>
Expand Down
14 changes: 12 additions & 2 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/GetFileSystemEntries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

using Xamarin.Localization.MSBuild;
using Xamarin.Messaging.Build.Client;

#nullable enable
Expand Down Expand Up @@ -38,6 +39,8 @@ public class GetFileSystemEntries : XamarinTask, ICancelableTask, ITaskCallback

#endregion

bool cancelled;

public override bool Execute ()
{
if (ShouldExecuteRemotely ())
Expand All @@ -48,10 +51,16 @@ public override bool Execute ()
foreach (var item in DirectoryPath) {
var path = item.ItemSpec.TrimEnd ('\\', '/');
var entriesFullPath = IncludeDirectories ?
Directory.GetFileSystemEntries (path, Pattern, searchOption) :
Directory.GetFiles (path, Pattern, searchOption);
Directory.EnumerateFileSystemEntries (path, Pattern, searchOption) :
Directory.EnumerateFiles (path, Pattern, searchOption);

Log.LogMessage (MessageImportance.Low, $"Searching for {(IncludeDirectories ? "files and directories" : "files")} in {path} with pattern '{Pattern}' and search option {searchOption}. Current directory: {Environment.CurrentDirectory}");

foreach (var entry in entriesFullPath) {
if (cancelled) {
Log.LogError (MSBStrings.E7119 /* Task execution was cancelled. */);
return false;
}
var recursiveDir = entry.Substring (path.Length + 1);
var newItem = new TaskItem (entry);
item.CopyMetadataTo (newItem);
Expand All @@ -70,6 +79,7 @@ public void Cancel ()
{
if (ShouldExecuteRemotely ())
BuildConnection.CancelAsync (BuildEngine4).Wait ();
cancelled = true;
}

public IEnumerable<ITaskItem> GetAdditionalItemsToBeCopied ()
Expand Down

8 comments on commit 8ce0574

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.