feat(remote): forward ports of a WSL project (#5140)

The Ports tool window existed only for SSH hosts and dev containers; a WSL project refused to forward. A WSL distribution shares this machine's loopback (as `forward_web_url` already assumes), so a port it listens on is reachable here on the same port without a tunnel. The route decision is now a pure `forward_route(target, host)` (SSH channel tunnel, container exec tunnel, or WSL's shared loopback; another host stays reachable from a dev container only), and `Forward::Wsl` reports the port unchanged and owns no resource. The Ports tool window registers for WSL sessions, remembered ports are scoped per distribution and folder (`wsl:<distro>:<root>`), the refusal and its string are gone, and the remote docs describe it.

Co-authored-by: akbar1214 <43771058+akbar1214@users.noreply.github.com>
This commit is contained in:
Akbar Husain
2026-11-22 14:23:05 +08:00
committed by GitHub
parent c57af26f9d
commit 62028362d6
11 changed files with 148 additions and 43 deletions
+3 -2
View File
@@ -16,13 +16,14 @@ import { closeConnectDialog, openConnectDialog } from "./model";
import { restorePorts, usePorts } from "./ports";
import { installReconnect, reconnectNow } from "./reconnect";
/** Forwarded ports are available while this window is connected to an SSH host or a dev container. */
/** Forwarded ports are available while this window is connected to an SSH host, a WSL distribution or a dev container. */
function trackPortsWindow(): () => void {
let off: (() => void) | null = null;
let session = useRemote.getState().session;
const sync = () => {
const next = useRemote.getState().session;
const connected = next?.target.kind === "ssh" || next?.target.kind === "devContainer";
const connected =
next?.target.kind === "ssh" || next?.target.kind === "wsl" || next?.target.kind === "devContainer";
if (connected && (next !== session || !off)) {
usePorts.setState({ ports: [] });
void restorePorts();
+77 -14
View File
@@ -138,14 +138,53 @@ it("forwards and remembers ports of a dev container, per folder and configuratio
useRemote.setState({ session: null });
});
it("doesn't forward ports of a WSL project yet", async () => {
it("forwards and remembers ports of a WSL distribution, per distro and folder", async () => {
const { useRemote } = await import("@/platform/remote");
ipc.remoteForwardPort.mockClear();
useRemote.setState({
session: { target: { kind: "wsl", distro: "u" }, root: "/home/u", host: "u", os: "linux", commands: [] },
const { forwardPort, savedPorts } = await import("./ports");
const wsl = (distro: string, root: string) => ({
target: { kind: "wsl" as const, distro },
root,
host: distro,
os: "linux",
commands: [],
});
expect(await forwardPort(8080)).toBeNull();
expect(ipc.remoteForwardPort).not.toHaveBeenCalled();
ipc.remoteForwardPort.mockClear();
useRemote.setState({ session: wsl("Ubuntu", "/home/u/app") });
expect(await forwardPort(8080)).toEqual({ remotePort: 8080, localPort: 8080, host: null });
expect(ipc.remoteForwardPort).toHaveBeenCalledWith(8080);
expect(savedPorts()).toEqual([8080]);
useRemote.setState({ session: wsl("Ubuntu", "/home/u/other") });
expect(savedPorts()).toEqual([]);
useRemote.setState({ session: wsl("Debian", "/home/u/app") });
expect(savedPorts()).toEqual([]);
// An SSH configuration of the same name has its own list.
useRemote.setState({
session: { target: { kind: "ssh", config: "Ubuntu" }, root: "/home/u/app", host: "h", os: "linux", commands: [] },
});
expect(savedPorts()).toEqual([]);
useRemote.setState({ session: null });
});
it("forwards the ports saved for a WSL distribution again after reconnecting", async () => {
const { useRemote } = await import("@/platform/remote");
const { forwardPort, restorePorts, savedPorts } = await import("./ports");
useRemote.setState({
session: {
target: { kind: "wsl", distro: "Ubuntu" },
root: "/home/u/app",
host: "Ubuntu",
os: "linux",
commands: [],
},
});
await forwardPort(3000);
expect(savedPorts()).toEqual([3000]);
// Reconnected: the new connection forwards nothing yet.
forwarded.length = 0;
ipc.remoteForwardPort.mockClear();
await restorePorts();
expect(ipc.remoteForwardPort).toHaveBeenCalledWith(3000);
expect(usePorts.getState().ports).toEqual([{ remotePort: 3000, localPort: 53000, host: null }]);
useRemote.setState({ session: null });
});
@@ -199,16 +238,40 @@ it("does not display a previous session's delayed port response", async () => {
useRemote.setState({ session: null });
});
it("reloads ports when switching directly between connected sessions", async () => {
it.each([
[
"SSH",
(folder: string) => ({
target: { kind: "ssh" as const, config: folder },
root: "/srv/app",
host: "h",
os: "linux",
commands: [],
}),
],
[
"WSL",
(folder: string) => ({
target: { kind: "wsl" as const, distro: folder },
root: "/home/u/app",
host: folder,
os: "linux",
commands: [],
}),
],
[
"dev container",
(folder: string) => ({
target: { kind: "devContainer" as const, folder, config: "" },
root: "/workspaces/app",
host: "app",
os: "linux",
commands: [],
}),
],
])("reloads ports on connect and when switching between connected %s sessions", async (_kind, session) => {
const { useRemote } = await import("@/platform/remote");
const { activate } = await import("./index");
const session = (folder: string) => ({
target: { kind: "devContainer" as const, folder, config: "" },
root: "/workspaces/app",
host: "app",
os: "linux",
commands: [],
});
useRemote.setState({ session: session("/first") });
forwarded.push({ remotePort: 8000, localPort: 8000, host: null });
const dispose = activate();
+5 -10
View File
@@ -1,6 +1,7 @@
/**
* Remote ports forwarded to this machine, remembered per host (or dev container) and folder and
* forwarded again on reconnect. A dev container also forwards its `forwardPorts` on its own.
* Remote ports forwarded to this machine, remembered per SSH host, WSL distribution or dev
* container and folder, and forwarded again on reconnect. A dev container also forwards its
* `forwardPorts` on its own.
*/
import { create } from "zustand";
import type { ForwardedPort } from "@/bindings";
@@ -16,6 +17,8 @@ function savedKey(): string | null {
switch (session?.target.kind) {
case "ssh":
return `${SAVED_KEY}${session.target.config}:${session.root}`;
case "wsl":
return `${SAVED_KEY}wsl:${session.target.distro}:${session.root}`;
case "devContainer":
return `${SAVED_KEY}devcontainer:${JSON.stringify([session.target.folder, session.target.config])}`;
default:
@@ -67,14 +70,6 @@ export function parsePort(text: string): number | null {
}
export async function forwardPort(port: number): Promise<ForwardedPort | null> {
if (useRemote.getState().session?.target.kind === "wsl") {
notifications().notify({
group: "remote",
level: "error",
title: tx("Forwarding ports is not supported in a WSL project yet"),
});
return null;
}
try {
const forwarded = await call(commands.remoteForwardPort(port));
await refreshPorts(true);
-1
View File
@@ -696,7 +696,6 @@
"WSL distribution:": "WSL distribution:",
"WSL distribution": "WSL distribution",
"Project runs on {label}": "Project runs on {label}",
"Forwarding ports is not supported in a WSL project yet": "Forwarding ports is not supported in a WSL project yet",
"PHPStan check failed": "PHPStan check failed",
"PHPStan ended with {status} and no report": "PHPStan ended with {status} and no report",
"Cannot run {program}: {error}": "Cannot run {program}: {error}",
-1
View File
@@ -4482,7 +4482,6 @@
"WSL distribution:": "WSL 배포판:",
"WSL distribution": "WSL 배포판",
"Project runs on {label}": "프로젝트가 {label}에서 실행됩니다",
"Forwarding ports is not supported in a WSL project yet": "WSL 프로젝트에서는 아직 포트 포워딩을 지원하지 않습니다",
"No SSH configurations": "SSH 구성 없음",
"Project directory:": "프로젝트 디렉터리:",
"Remote Development…": "원격 개발…",
-1
View File
@@ -617,7 +617,6 @@
"WSL distribution:": "WSL dağıtımı:",
"WSL distribution": "WSL dağıtımı",
"Project runs on {label}": "Proje {label} üzerinde çalışıyor",
"Forwarding ports is not supported in a WSL project yet": "WSL projelerinde bağlantı noktası yönlendirme henüz desteklenmiyor",
"PHPStan check failed": "PHPStan denetimi başarısız",
"PHPStan ended with {status} and no report": "PHPStan {status} durumuyla, rapor oluşturmadan sonlandı",
"Cannot run {program}: {error}": "{program} çalıştırılamıyor: {error}",
-1
View File
@@ -5380,7 +5380,6 @@
"WSL distribution:": "WSL 發行版:",
"WSL distribution": "WSL 發行版",
"Project runs on {label}": "專案執行於 {label}",
"Forwarding ports is not supported in a WSL project yet": "WSL 專案暫不支援埠轉發",
"No SSH configurations": "沒有 SSH 組態",
"Project directory:": "專案目錄:",
"Remote Development…": "遠端開發…",
-1
View File
@@ -5457,7 +5457,6 @@
"WSL distribution:": "WSL 发行版:",
"WSL distribution": "WSL 发行版",
"Project runs on {label}": "项目运行于 {label}",
"Forwarding ports is not supported in a WSL project yet": "WSL 项目暂不支持端口转发",
"No SSH configurations": "没有 SSH 配置",
"Project directory:": "项目目录:",
"Remote Development…": "远程开发…",