Skip to content

Commit

Permalink
Detect LongPath enabled on Windows (#1168)
Browse files Browse the repository at this point in the history
  • Loading branch information
veler authored May 26, 2024
1 parent 8d4ea06 commit 9e465af
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ function ExecSafe([scriptblock] $cmd) {
Set-StrictMode -Version 2.0; $ErrorActionPreference = "Stop"; $ConfirmPreference = "None"; trap { Write-Error $_ -ErrorAction Continue; exit 1 }
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent

# Verify LongPath is enabled
# Forked from https://github.com/dotnet/roslyn/blob/c8eecdb9563127988b3cb564a493eae9ef254a88/eng/build.ps1#L607
$regKeyProperty = Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem -Name "LongPathsEnabled" -ErrorAction Ignore
if (($null -eq $regKeyProperty) -or ($regKeyProperty.LongPathsEnabled -ne 1)) {
Write-Host -ForegroundColor Yellow "Warning: LongPath is not enabled, you may experience build errors. You can avoid these by enabling LongPath. You can enable it by running `"tools/enable-long-paths.reg`". More information on https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=powershell#enable-long-paths-in-windows-10-version-1607-and-later"
}

# Install .NET
ExecSafe { & $PSScriptRoot\tools\Install-DotNet.ps1 -RootFolder $PSScriptRoot }

Expand Down
11 changes: 11 additions & 0 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="_DevToysLongPathEnabled"
BeforeTargets="CoreCompile"
Condition="'$([MSBuild]::IsOsPlatform(Windows))' and '$(MSBuildThisFileDirectory.Length)' &gt; 25">
<!---
Validation for LongPath support (source https://github.com/dotnet/roslyn/blob/c8eecdb9563127988b3cb564a493eae9ef254a88/eng/build.ps1#L607)
https://docs.microsoft.com/en-us/archive/blogs/jeremykuhne/net-4-6-2-and-long-paths-on-windows-10#enabling-win32-long-path-support
-->

<Warning
Condition="'$(registry:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem@LongPathsEnabled)'!='1'"
Text="LongPath is not enabled, you may experience build errors. You can avoid these by enabling LongPath. You can enable it by running `tools/enable-long-paths.reg`. More information on https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=powershell#enable-long-paths-in-windows-10-version-1607-and-later"/>
</Target>
</Project>
4 changes: 4 additions & 0 deletions tools/enable-long-paths.reg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
"LongPathsEnabled"=dword:00000001

0 comments on commit 9e465af

Please sign in to comment.