Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[msbuild] Detect problems with long paths, and try to show a bit more helpful information. Fixes #20053. #20298

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions msbuild/Xamarin.Localization.MSBuild/MSBStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1554,4 +1554,8 @@
</comment>
</data>

<data name="E7122" xml:space="preserve">
<value>A path exceeding max path was detected. Enabling long paths in Windows may help. For more information see https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation.</value>
</data>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Enabling long path/Enabling long paths/ ?


</root>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Build.Utilities;
using Xamarin.Localization.MSBuild;
using Xamarin.Messaging.Build.Client;
using Xamarin.Utils;

namespace Xamarin.MacDev.Tasks {
public class CollectBundleResources : XamarinTask, ICancelableTask {
Expand Down Expand Up @@ -50,6 +51,10 @@ public override bool Execute ()

// But execute locally
return ExecuteImpl ();
} catch (PathTooLongException ptle) when (Environment.OSVersion.Platform == PlatformID.Win32NT && !PathUtils.OSSupportsLongPaths) {
Log.LogError (MSBStrings.E7122 /* A path exceeding max path was detected. Enabling long paths in Windows may help. For more information see https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation. */);
Log.LogErrorFromException (ptle); // report the original exception too.
return false;
} catch (Exception ex) {
Log.LogErrorFromException (ex);

Expand Down
23 changes: 23 additions & 0 deletions tools/common/PathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,28 @@ public static bool IsSymlinkOrContainsSymlinks (string directoryOrFile)

return path.Replace ('\\', '/');
}

public static bool OSSupportsLongPaths {
get {
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
return true;

return IsLongPathsEnabledRegistry;
}
}

const string WINDOWS_FILE_SYSTEM_REGISTRY_KEY = @"SYSTEM\CurrentControlSet\Control\FileSystem";
const string WINDOWS_LONG_PATHS_ENABLED_VALUE_NAME = "LongPathsEnabled";

static bool IsLongPathsEnabledRegistry {
get {
using (var fileSystemKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (WINDOWS_FILE_SYSTEM_REGISTRY_KEY)) {
if (fileSystemKey is null)
return false;
var longPathsEnabledValue = fileSystemKey.GetValue (WINDOWS_LONG_PATHS_ENABLED_VALUE_NAME, 0);
return Convert.ToInt32 (longPathsEnabledValue) == 1;
}
}
}
}
}
Loading