Git checkouts fail on Windows with "Filename too long error: unable to create file" errors
Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.
Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
When Bamboo checks out changes from repositories using the Windows Git.exe executable, the task fails and the following appears in the job log
java.lang.RuntimeException : com.atlassian.bamboo.plugins.stash.repository.StashRepositoryException: com.atlassian.bamboo.repository.RepositoryException: Checkout to revision <hash> has failed.command 'C:\Program Files\Git\cmd\git.exe' checkout -f master failed with code 1. Working directory was [<job working directory>].,
stderr: error: unable to create file <filename>: Filename too long error: unable to create file
Cause
According to the msysgit wiki on GitHub and the related fix this error, Filename too long, comes from a Windows API limitation of file paths having 260 characters or fewer.
Solution
To resolve this issue, we could change the Windows default 260 character limit in the Windows registry or by configuring the core.longpaths workaround in git config.
The solution applies to the Windows system that runs the build and not necessarily only to the Bamboo Server. If you have Bamboo Remote or Elastic Agents running on Windows, please be mindful that the steps below need to be followed on every agent that could possibly run such builds
Starting in Windows 10 (Windows Server 2016), version 1607, MAX_PATH limitations have been removed from common Win32 file and directory functions. However, you must opt-in to the new behaviour. We need to set the Windows default 260 character limit by updating the registry as mentioned here.
Go to the registry key Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled
(Type: REG_DWORD) must exist and be set to 1.
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" `
-Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force
The git configuration setting can be used for Windows versions lower than 2016 and Windows 10.
This setting is disabled by default by git to prevent users from checking out files that Windows Explorer, cmd/bash or some IDE cannot handle.
For Git configuration run the following command from GitBash or the Git CMD prompt:
git config --system core.longpaths true
This will allow file paths of 4096 characters.
Some users have reported the --system parameter does not work, but the --global one does, which would change the command to:
git config --global core.longpaths true