title: WSL2 - Linux relative symlinks broken when accessed from windows only for the \\wsl$\ mount-point
body: <h1>Problem</h1>
  <p>I am really struggling with <strong>relative symlinks</strong> on <strong>wsl2</strong> when they are created in the linux-native filesystem and I want to <strong>access the files via the share point <code>\\wsl$\distro-name\whatever</code></strong> - They are simply broken.</p>
  <h1>Environment</h1>
  <p>I have <code>wsl2</code> activated in my Windows10. I have an <code>Ubuntu-20.04</code>:</p>
  <p><a href="https://i.stack.imgur.com/1NNyW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1NNyW.png" alt="Ubuntu in WSL2" /></a></p>
  <h1>Impact in my coding workflow</h1>
  <p>Broken symlinks <strong>forbid me</strong> to seamlessly &quot;execute in wsl2&quot; while &quot;edit from an IDE in Windows&quot;.</p>
  <p>Real use case (but not limited to): Developing two intelaced projects: A repo with an application and another repo that lives aside with a library. The application symlinks the library:</p>
  <ul>
  <li>Main program in <code>/files/repos/my-nice-app</code></li>
  <li>Library also in <code>/files/repos/my-nice-lib</code></li>
  <li><code>my-nice-app/libs/my-nice-lib</code> is a symlink to <code>../../my-nice-lib</code></li>
  <li>Intelligent IDE in windows, operating on the application opening <code>\\wsl$\Ubuntu-20.04\files\repos\my-nice-app</code></li>
  </ul>
  <p>With this setup, the location <code>\\wsl$\Ubuntu-20.04\files\repos\my-nice-app\libs\my-nice-lib</code> is expected to map to <code>\\wsl$\Ubuntu-20.04\files\repos\my-nice-lib</code>.</p>
  <p>But it does not work. All the code-completion in the IDE is messed up, because the symlink does not de-map well and the IDE can't read the classes and definitions of the library.</p>
  <h1>How to reproduce a working example</h1>
  <h2>Working example. Step 1 - Preparation</h2>
  <p>Whenever I <strong>create a symlink from the linux</strong> in the NTFS filesystem it is <strong>properly decoded in windows</strong>.</p>
  <p>Same the oposite side: If I <strong>create the link from windows</strong> (both with CMD and <code>mklink</code> or Powershell with <code>New-Item</code>) they are <strong>properly decoded in linux</strong>.</p>
  <p>Imagine this scenario:</p>
  <ol>
  <li><p>I have this dir: <code>/mnt/c/tmp</code> which corresponds to <code>C:\tmp</code>.</p>
  </li>
  <li><p>I put some contents into a file <code>original.txt</code>. I use the linux bash for that.
  <a href="https://i.stack.imgur.com/yuAmY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/yuAmY.png" alt="Create content on NTFS" /></a></p>
  </li>
  <li><p>From the linux, I do a relative symlink <code>linux.txt</code> pointing to <code>original.txt</code>.
  <a href="https://i.stack.imgur.com/ebhKK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ebhKK.png" alt="Create link from linux on NTFS" /></a></p>
  </li>
  <li><p>I then do it from windows. From a CMD with the <code>mklink</code> command:
  <a href="https://i.stack.imgur.com/zIW0v.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zIW0v.png" alt="Create link from CMD on NTFS" /></a></p>
  </li>
  <li><p>I can even do the symlink in the windows-side with the <code>New-Item</code> command from an elevated powershell
  <a href="https://i.stack.imgur.com/CAtHb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/CAtHb.png" alt="Create link from PowerShell on NTFS" /></a></p>
  </li>
  </ol>
  <p>Up to here I should have one file <code>original.txt</code> and three links <code>linux.txt</code>, <code>cmd.txt</code> and <code>powershell.txt</code></p>
  <h2>Working example. Step 2 - Listing symlinks</h2>
  <p><strong>Success:</strong> I do see all them listed in each of the 3 shells: linux, cmd and powershell:</p>
  <p><a href="https://i.stack.imgur.com/HGW3U.png" rel="noreferrer"><img src="https://i.stack.imgur.com/HGW3U.png" alt="Listing in NTFS" /></a></p>
  <p>Here in Linux (1 in the image) we see they are symlinks, as well as from the CMD (2 in the image) and in the powershell (3 in the image).</p>
  <p>Both Linux and CMD also report the &quot;demapping&quot; (4 in the image). As <code>cmd.txt</code> and <code>linux.txt</code> are both relative symlinks there's no magic to do behind, just understand that they are links and done.</p>
  <p>Powershell for some reason which I don't care for this question, promoted the relative symlink to an absolute one. This shows up a very interesting effect:</p>
  <p><strong>Somebody behind the scenes</strong> must be doing some kind of translation work, which is being done well in this case (5 in the image): While from linux <code>powershell.txt</code> it is pointing to a path starting with <code>/mnt/c/...</code> the windows interpreter is seeing it as pointing to <code>C:\...</code>.</p>
  <h2>Working example. Step 3 - Accessing the contents via symlinks</h2>
  <p>Now time to see if I can <code>cat</code> (<code>type</code> in windows) the contents of all of them...</p>
  <p><a href="https://i.stack.imgur.com/SRyJZ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SRyJZ.png" alt="Accessing in NTFS" /></a></p>
  <p>No explanation here needed. All 9 combinations (3 creation methods x 3 consumption methods) including the relative and absolute links, all work perfectly.</p>
  <p>Now time for the rule-breakers...</p>
  <h1>How to reproduce a failing example</h1>
  <p>I'll do the <strong>very exact same process</strong> but instead of doing it on <code>/mnt/c/tmp</code> I'll do it on <code>/tmp</code> and in windows, instead of accessing it from <code>C:\tmp</code> I'll access it from <code>\\wsl$\Ubuntu-20.04\tmp</code>.</p>
  <p>Let's start...</p>
  <h2>Failing example. Step 1 - preparation</h2>
  <p>I start by the linux. Navigating to <code>/tmp</code> and creating some dummy content on the <code>WSL2</code> filesystem. I continue by doing the symlink.</p>
  <p><a href="https://i.stack.imgur.com/cVZBM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/cVZBM.png" alt="Creating content wsl2" /></a></p>
  <p>When I try to go there with the CMD I really can't because it complains of being an UNC path:</p>
  <p><a href="https://i.stack.imgur.com/RssDl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/RssDl.png" alt="Navigating UNC on CMD" /></a></p>
  <p>I'll change my strategy and I'll do a net mount to have a drive letter, see if CMD likes it more. I'll use <code>W:</code> for the <code>WSL2</code> filesystem. In the image: 1 = I create it, 2 = I check it is created, 3 = I navigate into <code>tmp</code> on the WSL2.</p>
  <p><a href="https://i.stack.imgur.com/o3AGq.png" rel="noreferrer"><img src="https://i.stack.imgur.com/o3AGq.png" alt="Creating W:" /></a></p>
  <p>But now... oh surprise!!! When I try to do a symlink from the CMD... it denies the access:</p>
  <p><a href="https://i.stack.imgur.com/jDdQk.png" rel="noreferrer"><img src="https://i.stack.imgur.com/jDdQk.png" alt="Linking from CMD on WSL2" /></a></p>
  <p>Let's try with an elevated PowerShell...</p>
  <p>In this image I can see I can properly navigate to the UNC path (1 in the image) but when trying to create the link... boom... 2 in the image: &quot;Symbolic links are not compatible with the specified path&quot;:</p>
  <p><a href="https://i.stack.imgur.com/SfSW6.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SfSW6.png" alt="Linking from PowerShell on WSL2" /></a></p>
  <p>So there's only ONE way to create the symlinks in the WSL2: From inside the linux. Let's see how can we list it and access it.</p>
  <h2>Failing example. Step 2 - listing + accessing</h2>
  <p>Starting off, linux can see linux links (of course):</p>
  <p><a href="https://i.stack.imgur.com/zX3jp.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zX3jp.png" alt="Linux accessing WSL2" /></a></p>
  <p>But when moving to CMD, the listing shows &quot;JUNCTION&quot; instead of &quot;SYMLINK&quot; as it showed it on the NTFS and additionally when trying to access it, it breaks:</p>
  <p><a href="https://i.stack.imgur.com/LUwsY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LUwsY.png" alt="CMD accessing WSL2" /></a></p>
  <p>Finally when moving into Powershell the behaviour is similar: It it sees &quot;it is there&quot; but the contents can't be accessed:</p>
  <p><a href="https://i.stack.imgur.com/1Y3Px.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1Y3Px.png" alt="PowerShell accessing WSL2" /></a></p>
  <h1>Final considerations</h1>
  <ul>
  <li>I'm even not asking for absolute path conversion (as demonstrated that in the NTFS works). I'm just happy with relative links</li>
  <li>I've done this with files. But it also fails with directories.</li>
  </ul>
  <h1>Soooooooooo Question</h1>
  <p>How can I have a properly working symlink on <code>WSL2</code> working well both in the linux side and the windows side?</p>
  <p>If it's a bug, what module is it? The kernel? The WSL itself? The P9 protocol? I'd be more than happy to contribute but I'd even don't know what project should I contribute to.</p>
  <h1>Investigation done so far</h1>
  <p>I have deepely read in full all of these:</p>
  <ul>
  <li><a href="https://docs.docker.com/docker-for-windows/wsl/" rel="noreferrer">https://docs.docker.com/docker-for-windows/wsl/</a></li>
  <li><a href="https://medium.com/@ragin/development-under-windows-under-linux-with-wsl2-intellij-860daf601b61" rel="noreferrer">https://medium.com/@ragin/development-under-windows-under-linux-with-wsl2-intellij-860daf601b61</a></li>
  <li><a href="https://docs.microsoft.com/es-es/windows/wsl/tutorials/wsl-git" rel="noreferrer">https://docs.microsoft.com/es-es/windows/wsl/tutorials/wsl-git</a></li>
  <li><a href="https://docs.microsoft.com/es-es/windows/win32/fileio/hard-links-and-junctions?redirectedfrom=MSDN" rel="noreferrer">https://docs.microsoft.com/es-es/windows/win32/fileio/hard-links-and-junctions?redirectedfrom=MSDN</a></li>
  <li><a href="https://docs.microsoft.com/es-es/windows/win32/fileio/creating-symbolic-links" rel="noreferrer">https://docs.microsoft.com/es-es/windows/win32/fileio/creating-symbolic-links</a></li>
  <li><a href="https://www.docker.com/blog/new-docker-desktop-wsl2-backend/" rel="noreferrer">https://www.docker.com/blog/new-docker-desktop-wsl2-backend/</a></li>
  </ul>
  <p>and many more, but still no luck.</p>