Subject: bug#65812: 29.1; Emacs client on Windows can't connect to the daemon on WSL over TCP

From: Eli Zaretskii <[email-masked]>
Date: Fri, 08 Sep 2023 10:24:25 +0300

> Date: Thu, 7 Sep 2023 15:10:59 -0400
> From: user@pontos
> 
> I'm trying to connect Emacs client running on Windows over TCP to the Emacs
> server running inside WSL.

This is currently not supported, due to the various quirks and kludges
we need to support the client-server protocol on MS-Windows.  the
patch below attempts to support this by adding one more kludge.

> # Inside WSL shell:
> (cat <<EOF
> (setq server-host "WSL_IP_ADDRESS_HERE")
> (setq server-port 12345)
> (setq server-use-tcp t)
> (server-start)
> EOF
> ) > server-config.el
> emacs -Q -l server-config.el --daemon
> 
> # On Windows
> # Map network drive \\wsl.localhost\Ubuntu as, e.g. W:
> # Then in PowerShell:
> $env:EMACS_SERVER_FILE=W:\home\user\.emacs.d\server\server
> & 'C:\Program Files\Emacs\emacs-29.1\bin\emacsclientw.exe' -c -n
> 
> Client will first display message window saying that it connected to the
> remote socket, and after clicking [OK] button, error message appears
> saying "*ERROR*: Could not open file: CONOUT$".

Please try the patch below and tell if it solves your problem.  The
patch should be applied to the Emacs you run inside WSL, and you will
need to byte-compile the patched server.el there, ensure the produced
server.elc file replaces the one you installed with the original Emacs
on WSL, and if Emacs you run on WSL also uses native-compilation, make
sure the stale .eln file is not loaded (it should happen
automatically, but for good measure just delete the old .eln file).

diff --git a/lisp/server.el b/lisp/server.el
index 104fc06..ee80b93 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1262,9 +1262,12 @@ server-process-filter
                  ;; choice there.)  In daemon mode on Windows, we can't
                  ;; make tty frames, so force the frame type to GUI
                  ;; there too.
-                 (when (and (eq system-type 'windows-nt)
-                            (or (daemonp)
-                                (eq window-system 'w32)))
+                 (when (or (and (eq system-type 'windows-nt)
+                                (or (daemonp)
+                                    (eq window-system 'w32)))
+                           ;; Client runs on Windows, but the server
+                           ;; runs on a Posix host.
+                           (equal tty-name "CONOUT$")
                    (push "-window-system" args-left)))
 
                 ;; -position +LINE[:COLUMN]:  Set point to the given