WSL icon indicating copy to clipboard operation
WSL copied to clipboard

Cannot use GUI apps/tools as Wayland socket doesn't exist in `XDG_RUNTIME_DIR`

Open mangkoran opened this issue 2 years ago • 49 comments

Windows Version

10.0.19044.4046

WSL Version

2.0.14.0

Are you using WSL 1 or WSL 2?

  • [X] WSL 2
  • [ ] WSL 1

Kernel Version

5.15.133.1-1

Distro Version

Arch

Other Software

  • wl-clipboard 2.2.1 (shouldn't be the root cause)
  • systemd 255.4-2

Repro Steps

  1. Install wl-clipboard -> sudo pacman -S wl-clipboard
  2. Run echo "foobar" | wl-copy

Expected Behavior

foobar is copied to Windows clipboard through Wayland

Actual Behavior

❯ echo "foobar" | wl-copy
Failed to connect to a Wayland server: No such file or directory
Note: WAYLAND_DISPLAY is set to wayland-0
Note: XDG_RUNTIME_DIR is set to /run/user/1000/
Please check whether /run/user/1000//wayland-0 socket exists and is accessible.

Diagnostic Logs

Not applicable

Additional Note

I couldn't open any GUI apps and any Wayland-related tools such as wl-copy.

❯ echo "foobar" | wl-copy
Failed to connect to a Wayland server: No such file or directory
Note: WAYLAND_DISPLAY is set to wayland-0
Note: XDG_RUNTIME_DIR is set to /run/user/1000/
Please check whether /run/user/1000//wayland-0 socket exists and is accessible.

The following are the content of /run/user/1000/

❯ ls -la /run/user/1000/
total 0
drwx------ 7 mangkoran mangkoran 220 Mar  7 08:38 ./
drwxr-xr-x 3 root      root       60 Mar  7 08:34 ../
srw-rw-rw- 1 mangkoran mangkoran   0 Mar  7 08:34 bus=
prw------- 1 mangkoran mangkoran   0 Mar  7 08:34 fish_universal_variables.notifier|
drwx------ 2 mangkoran mangkoran  60 Mar  7 08:34 gcr/
drwx------ 2 mangkoran mangkoran 160 Mar  7 08:34 gnupg/
drwxr-xr-x 2 mangkoran mangkoran  60 Mar  7 08:34 p11-kit/
srw-rw-rw- 1 mangkoran mangkoran   0 Mar  7 08:34 pipewire-0=
srw-rw-rw- 1 mangkoran mangkoran   0 Mar  7 08:34 pipewire-0-manager=
drwxr-xr-x 2 mangkoran mangkoran  60 Mar  7 08:34 pulse/
drwxr-xr-x 6 mangkoran mangkoran 160 Mar  7 08:34 systemd/

Based on that it seems wayland-0 socket is missing in XDG_RUNTIME_DIR. Hmm pretty weird I think as I don't disable guiApplications in .wslconfig.

Then I found https://github.com/microsoft/wslg/issues/1187. I proceed to check /mnt/wslg/runtime-dir/.

❯ ls -la /mnt/wslg/runtime-dir/
total 0
drwx------ 4 mangkoran mangkoran 120 Mar  7 09:24 ./
drwxrwxrwt 8 root      root      320 Mar  7 08:34 ../
drwx------ 3 mangkoran mangkoran  60 Mar  7 08:34 dbus-1/
drwx------ 2 mangkoran mangkoran  80 Mar  7 08:34 pulse/
srwxrwxrwx 1 mangkoran mangkoran   0 Mar  7 08:34 wayland-0=
-rw-rw---- 1 mangkoran mangkoran   0 Mar  7 08:34 wayland-0.lock

And yep there it is. So Wayland socket is currently not in XDG_RUNTIME_DIR. It seems the Wayland socket was previously symlinked in XDG_RUNTIME_DIR as in https://github.com/microsoft/WSL/issues/10205. Then I tried to link the Wayland socket.

~
❯ ln -s /mnt/wslg/runtime-dir/wayland-0* /run/user/1000/

~
❯ echo "foobar" | wl-copy

~
❯ echo $status
0

Although it works as intended, I think this isn't the default behavior/a regression.

Workaround

As a workaround, I created a simple systemd user oneshot service/unit.

❯ cat ~/.config/systemd/user/symlink-wayland-socket.service
[Unit]
Description=Symlink Wayland socket to XDG_RUNTIME_DIR

[Service]
Type=oneshot
ExecStart=/usr/bin/ln -s /mnt/wslg/runtime-dir/wayland-0      $XDG_RUNTIME_DIR
ExecStart=/usr/bin/ln -s /mnt/wslg/runtime-dir/wayland-0.lock $XDG_RUNTIME_DIR

[Install]
WantedBy=default.target

Enable it using the following command.

systemctl --user enable symlink-wayland-socket.service

[!TIP] For Ubuntu 24.04 user, please refer to additional workaround by @noopole https://github.com/microsoft/WSL/issues/11261#issuecomment-2233443300

Fix

Fixed in https://github.com/microsoft/WSL/releases/tag/2.5.1

Refs: https://github.com/microsoft/WSL/issues/11261#issuecomment-2719332094

mangkoran avatar Mar 07 '24 03:03 mangkoran

Can confirm with:

Versions

WSL version: 2.1.5.0
Kernel version: 5.15.146.1-2
WSLg version: 1.0.60
MSRDC version: 1.2.5105
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22631.3155
NAME="Fedora Linux"
VERSION="39 (Container Image)"
ID=fedora
VERSION_ID=39
VERSION_CODENAME=""
PLATFORM_ID="platform:f39"

Workaround

Exactly what @mangkoran mentioned by creating a oneshot systemd service.

dixonwille avatar Mar 14 '24 00:03 dixonwille

Many thanks, @mangkoran, for the details and the service script. Had the same issue with WSL 2.1.5 and Fedora 39 and the workaround fixed it.

stefan-oskamp avatar Mar 18 '24 19:03 stefan-oskamp

Here is another workaround according to this post. Create /etc/systemd/system/[email protected]/override.conf and add:

[Service]
ExecStart = sh -c "ln -fs /mnt/wslg/runtime-dir/* /run/user/"%i

This will link everything in /mnt/wslg/runtime-dir to /run/user/$UID. The reason why I don't mount /mnt/wslg/runtime-dir at /run/user/$UID like what the original post does is that it will cause [email protected]'s failure due to /run/user/$UID's incorrect owner

xuangeyouneihan avatar Mar 21 '24 10:03 xuangeyouneihan

sorry for asking this novice question but @mangkoran how do we enable this service. Thanks for the help

king-11 avatar Apr 06 '24 08:04 king-11

@king-11 It should be the following.

systemctl --user enable symlink-wayland-socket.service

I will add this to my top post.

PS: A quick Google search should help you faster 😉

mangkoran avatar Apr 06 '24 08:04 mangkoran

Although this issue is about Wayland, for anyone who found this becaus X apps don’t work either (like me), if you want to use X11 apps, too, you will also have to symlink /mnt/wslg/.X11-unix/X0 to /tmp/.X11-unix/X0.

gergelypolonkai avatar May 14 '24 09:05 gergelypolonkai

For me, X11 apps work but Wayland doesn't.

WSL info:

> wsl --version
WSL version: 2.1.5.0
Kernel version: 5.15.146.1-2
WSLg version: 1.0.60
MSRDC version: 1.2.5105
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22631.3593

Ubuntu info:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04 LTS
Release:        24.04
Codename:       noble
$ uname -a
Linux *** 5.15.146.1-microsoft-standard-WSL2 #1 SMP Thu Jan 11 04:09:03 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

and systemd is enabled.

tried:

$ echo "hello" | wl-copy
Failed to connect to a Wayland server: No such file or directory
Note: WAYLAND_DISPLAY is set to wayland-0
Note: XDG_RUNTIME_DIR is set to /run/user/1000/
Please check whether /run/user/1000//wayland-0 socket exists and is accessible.

As I fear the provided workaround may break my system(?) I didn't test it.

arf1372 avatar Jun 08 '24 20:06 arf1372

@arf1372 You could try the symlink command first (not the systemd service) and see if solves the issue on your machine.

ln -s /mnt/wslg/runtime-dir/wayland-0* /run/user/1000/

This command is harmless as it just symlinks and not removing any files. If anything goes wrong, you could restart WSL and everything will be reverted.

mangkoran avatar Jun 08 '24 23:06 mangkoran

For me, X11 apps work but Wayland doesn't.

WSL info:

> wsl --version
WSL version: 2.1.5.0
Kernel version: 5.15.146.1-2
WSLg version: 1.0.60
MSRDC version: 1.2.5105
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22631.3593

Ubuntu info:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04 LTS
Release:        24.04
Codename:       noble
$ uname -a
Linux *** 5.15.146.1-microsoft-standard-WSL2 #1 SMP Thu Jan 11 04:09:03 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

and systemd is enabled.

tried:

$ echo "hello" | wl-copy
Failed to connect to a Wayland server: No such file or directory
Note: WAYLAND_DISPLAY is set to wayland-0
Note: XDG_RUNTIME_DIR is set to /run/user/1000/
Please check whether /run/user/1000//wayland-0 socket exists and is accessible.

As I fear the provided workaround may break my system(?) I didn't test it.

Hey @arf1372 , could you tell how you configured X11?

kunalsinghdadhwal avatar Jul 03 '24 15:07 kunalsinghdadhwal

Ubuntu-24.04 set these files differently than Ubuntu-22.04:

image image

I don't think I ever faced this issue on Ubuntu 22.04. On 24.04, I can't even run ls on /mnt/wslg/runtime-dir/ without sudo, whereas 22.04 doesn't require it all.

tusharsnx avatar Jul 15 '24 09:07 tusharsnx

I don't think I ever faced this issue on Ubuntu 22.04. On 24.04, I can't even run ls on /mnt/wslg/runtime-dir/ without sudo, whereas 22.04 doesn't require it all.

To make it work under Ubuntu 24.04 I had to set rx permissions or change owner on runtime-dir, then I could create the simlink.

sudo chmod +rx /mnt/wslg/runtime-dir (or) chown 1002:1002 /mnt/wslg/runtime-dir
ln -s /mnt/wslg/runtime-dir/wayland-0* /run/user/1002/

(where 1002 is the current user.)

noopole avatar Jul 17 '24 14:07 noopole

WSLg bug: https://github.com/microsoft/wslg/issues/1244

alonbl avatar Aug 23 '24 18:08 alonbl

So, this may not be a satisfying work-around for everyone, but this poblem was bowing up my Neovim session, because WAYLAND_DISPLAY was set, causing Neovim to assume that wl-copy was in play and working properly.

I added this awful bodge to my .zshrc that may help someone so I am including it here. If WSL is detected, we unset WAYLAND_DISPLAY but also add a 'wway' alias to put it back if we actually WANT to run a GUI app :)

# Under WSL, we KINDA have Wayland, but not really, and that blows up Neovim. Ungh. :)
# Add an alias so if we actually WANT the pseudo wayland, we can haz.

if [[ $(uname -r | grep "microsoft") ]] then
        unset WAYLAND_DISPLAY
        alias wway='export WAYLAND_DISPLAY=wayland-0'
fi

Yay! My neovim works again :)

feoh avatar Sep 06 '24 20:09 feoh

Thank you @mangkoran

This is an issue for me on Ubuntu 24.04.1 LTS running on WSL2

WSL version: 2.2.4.0
Kernel version: 5.15.153.1-2
WSLg version: 1.0.61
MSRDC version: 1.2.5326
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.26091.1-240325-1447.ge-release
Windows version: 10.0.22631.4037

vanderzielj avatar Sep 17 '24 15:09 vanderzielj

So, this was also blowing up my copying while using Neovim (nvim) after my X410 trial ended:

$ echo abc | /usr/bin/wl-copy.orig
Failed to connect to a Wayland server: No such file or directory
Note: WAYLAND_DISPLAY is set to wayland-0
Note: XDG_RUNTIME_DIR is set to /run/user/1000/
Please check whether /run/user/1000//wayland-0 socket exists and is accessible.

My issue was not pasting into WSL, but copying from WSL and pasting into Windows, especially from nvim.

Unless you have disabled it, you should be able to run Windows binaries from within WSL 2. The workaround for clipboard access I have implemented is to wrapper Microsoft's bundled c:\Windows\system32\clip.exe binary in a script to copy:

#!/bin/bash
# the pbcopy script

tee <&0 | clip.exe
exit 0

For pasting, I opted to wrapper PowerShell and strip the extraneous appended carriage return or null:

#!/bin/bash
# the pbpaste script

powershell.exe Get-Clipboard | sed 's/\r$//' | sed -z '$ s/\n$//'
exit 0

Put these scripts in /usr/bin as /usr/bin/pbcopy and /usr/bin/pbpaste. Then rename wl-copy and wl-paste and symlink the pb scripts:

$ sudo mv /usr/bin/wl-copy{,.orig}
$ sudo mv /usr/bin/wl-paste{,.orig}
$ sudo ln -s /usr/bin/pbcopy /usr/bin/wl-copy
$ sudo ln -s /usr/bin/pbpaste /usr/bin/wl-paste

If you want to test this in nvim, go into visual mode, select some text, and press <Ctrl-R>"+y to yank the selection to the system clipboard. Then try pasting it in a Windows app.

Yes, you could just name the scripts wl-copy and wl-paste, but I use symlinks to avoid confusing myself six months from now.

The other benefit of symlinking is being able to use the pbcopy and pbpaste commands like I would in macOS. 😄

NOTE: This approach assumes you only want to copy/paste text. Unlike wl-copy/paste the MIME type is stripped/ignored.

shpankau avatar Sep 30 '24 00:09 shpankau

Thanks for reporting @mangkoran . I've got the same issue here with Fedora 40 under WSL2.

Doing the symlink command above fixed it for me for now.

paolomissagia avatar Oct 05 '24 22:10 paolomissagia

For me the symlink creation as explained in the "workaround" section of the initial description in this issue, together with the permission change @noopole mentioned, did the trick. Thanks a lot for compiling that information, @mangkoran and @noopole!

However, the permission change seems to be reverted after each reboot, so I wondered if someone already has come up with a good idea about how to make this permission change permanent or at least recreate it on startup. I was thinking of just another systemd oneshot service/unit running as root, but maybe there's more elegant ways?

tkonsta avatar Nov 15 '24 09:11 tkonsta

For me the symlink creation as explained in the "workaround" section of the initial description in this issue, together with the permission change @noopole mentioned, did the trick. Thanks a lot for compiling that information, @mangkoran and @noopole!

However, the permission change seems to be reverted after each reboot, so I wondered if someone already has come up with a good idea about how to make this permission change permanent or at least recreate it on startup. I was thinking of just another systemd oneshot service/unit running as root, but maybe there's more elegant ways?

:~$ cat /etc/systemd/user/symlink-wayland-socket.service
[Unit]
Description=Symlink Wayland socket to XDG_RUNTIME_DIR

[Service]
Type=oneshot
ExecStart=/usr/bin/ln -s /mnt/wslg/runtime-dir/wayland-0      $XDG_RUNTIME_DIR
ExecStart=/usr/bin/ln -s /mnt/wslg/runtime-dir/wayland-0.lock $XDG_RUNTIME_DIR

[Install]
WantedBy=default.target

codeart1st avatar Nov 15 '24 11:11 codeart1st

This is the same snippet, which had been already described as "workaround" in the initial description of this issue. However, I was referring to ideas about how to make the permission change permanent, which has been mentioned by @noopole above:

sudo chmod +rx /mnt/wslg/runtime-dir (or) chown 1002:1002 /mnt/wslg/runtime-dir
ln -s /mnt/wslg/runtime-dir/wayland-0* /run/user/1002/

tkonsta avatar Nov 15 '24 12:11 tkonsta

@feoh @shpankau I use Neovim in WSL too and there is no need to use Windows' Get-Clipboard or unset WAYLAND_DISPLAY if you follow the workaround.

  1. First make sure to follow the workaround in issue description
  2. Set vim.opt.clipboard = "unnamedplus" in Neovim (docs)
  3. Install wl-clipboard
  4. Reboot WSL by wsl --shutdown in PowerShell

This way you will have functional clipboard that interop/sync with Windows clipboard (yes you can yank and paste to and from Windows clipboard). Please let me know if it works for you ^_^

mangkoran avatar Nov 15 '24 16:11 mangkoran