Skip to content

feat(remote): 通过 wsl.exe 打开本机 WSL 发行版作为远程工作区 (1lck/Lithe-IDEA#2231) - #157

Merged
xiaoyumuxi merged 1 commit into
mainfrom
feat/2231-remote-wsl
Nov 21, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
feat/2231-remote-wsl

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

关联 Issue

1lck/Lithe-IDEA#2231(远程开发完整设计):远程工作区要能打开本机的 WSL 发行版。本 PR 是 Core 部分,桌面端的连接窗口接入另开 PR。

类型

  • feat:remote.connect 支持 wsl: { distribution, user? },通过 wsl.exe 打开本机 WSL 发行版作为远程工作区;新增 remote.listWslDistributions 列出已安装的发行版

改动说明

传输链(crates/remote/src/client/transport.rs)

  • 新增 Hop::Wsl(WslHop),命令行是 %SystemRoot%\System32\wsl.exe --distribution <d> [--user <u>] --cd ~ --exec <命令…>:
    • wsl.exe 从 Windows 系统目录取,不走 PATH,PATH 上同名程序顶替不了;
    • --exec 不经过 shell,参数不需要再做 shell 转义;含 NUL 的参数拒绝(UnsafeArgument),因为 Windows 命令行放不下 NUL;
    • --cd ~ 让服务从 Linux 主目录启动,不落在 /mnt 下那个很慢的 Windows 挂载目录。
  • 发行版名只接受 wsl.exe --import 认的写法(ASCII 字母、数字、.、_、-,字母或数字开头,最长 64 字节);用户名沿用 SSH 的登录名规则,最长 32 字节(useradd 的上限)。
  • WSL 只能是第一跳:SSH 之后、WSL 之后再接 WSL 都拒绝(InvalidHop);WSL 之后接容器可以,即发行版里的容器。
  • restrict_environment:WSL 链保留全部环境变量(wsl.exe 只把 WSLENV 列出的变量传进发行版),并设置 WSL_UTF8=1,让 wsl.exe 的提示用 UTF-8 输出。
  • identity() 为 wsl:<发行版> 或 wsl:<用户>@<发行版>,最近项目和按主机保存的数据用它作键。
  • 新增 starts_with_ssh(),会话据此决定是否需要 AskPass。

连接参数与发行版列表(新文件 crates/remote/src/client/wsl.rs)

  • WslConnection { distribution, user? } 就是 remote.connect 的 wsl 字段,拒绝未知字段(例如 program,请求不能指定要运行的程序)。hop() 先去掉首尾空白,空 user 表示发行版默认用户;拒绝时给出字段和原因:distribution 的 required、tooLong、invalid,user 的 tooLong、invalid。
  • list_command() 是 wsl.exe --list --quiet。parse_distributions():
    • UTF-16LE(有无 BOM 都行)和 UTF-8 都能读,因为 WSL 0.64 之前不认 WSL_UTF8;
    • 去掉 Docker Desktop 自己的 docker-desktop、docker-desktop-data(和 VS Code 一样),忽略大小写去重(WSL 比较名称时不分大小写),跳过不是发行版名的行(如“没有安装发行版”的提示);
    • 最多读 64 KiB 输出、返回 64 个名称。
  • output_text() 用同样的规则把 wsl.exe 的输出解码成文本,供诊断信息使用。

信任记录(crates/remote/src/client/trust.rs)

  • 新增 TargetHop::Wsl(WslTarget { distribution, user? })。键只包含发行版和账户,不包含 wsl.exe 的路径。
  • 读取时拒绝不是第一跳的 WSL、不合法的发行版名或用户名;未知字段和其他类型一样按文档错误拒绝。

日志(rust/logging/events/remote.json)

  • 连接类型新增 wsl(ConnectionKind::Wsl);cleanup_failed、diagnostics_failed 的说明补上 wsl.exe。目录已重新生成(146 个事件)。
  • 和 SSH 一样:wsl.exe 的诊断只显示给用户,不写日志;日志里没有发行版名和用户名。

连接流程(crates/workspace/src/sessions.rs、probe.rs)

  • remote.connect 必须且只能给出 connection 或 wsl 中的一个,否则在启动任何进程前返回 invalid_request:两个都没有时 details 为 { field: "connection", reason: "required" },两个都有时为 { field: "wsl", reason: "invalid" }。原有的 connection 请求不受影响。
  • WSL 连接不创建 AskPass 会话,也就不会有 remote.authRespond;探测、校验后上传、各阶段和会话本身都和 SSH 相同,进程树同样登记在会话表里,断开时结束。
  • 错误信息按客户端区分:例如 “Could not start wsl.exe”、“The WSL distribution did not finish starting in time”。
  • 服务启动前 wsl.exe 就退出时,用新增的 classify_wsl 按 wsl.exe 固定的错误码分类:WSL_E_DISTRO_NOT_FOUND 归为 unreachable,其余归为 failed;诊断信息按 UTF-16 或 UTF-8 解码后脱敏。
  • 进程退出但什么都没输出时,诊断写“wsl.exe exited with status N”(SSH 同样补上“ssh exited with status N”),不再是空字符串。

列出发行版(新文件 crates/workspace/src/wsl.rs,命令 remote.listWslDistributions)

  • 不需要参数,返回 { available, distributions, diagnostics },不保存任何内容。
  • 非 Windows 系统直接返回 available: false,不运行任何程序;Windows 上找不到 wsl.exe 时也返回 available: false。
  • 运行 wsl.exe --list --quiet 时设置 WSL_UTF8=1,最多等 15 秒(开机后第一次调用要启动 WSL 服务),超时返回 timed_out,并响应请求取消。
  • wsl.exe 返回非零时 available: true、列表为空,diagnostics 是它输出内容的脱敏文本(例如还没装 WSL 的提示),只显示、不写日志。
  • crates/core 只加命令名映射和 dispatcher 分支。

契约(rust/contracts/rust-core-api.md)

  • 命令表:remote.connect 写明 SSH 或本机 WSL 发行版;remote.disconnect 写明结束 OpenSSH 或 wsl.exe 的进程树;新增 remote.listWslDistributions 一行。
  • 新增两段说明:发行版列表的结果字段;wsl 字段的写法、不走 AskPass、发行版不存在时为 unreachable、信任记录里的跳形状,以及 connection、wsl 都给或都不给时的错误和字段规则。

范围

  • 逻辑在 crates/remote;crates/workspace 只负责把它接到连接流程和命令上,crates/core 只做转接。ConnectionError::at 改成 pub(crate),供 wsl.rs 构造字段错误。

验证

  • cargo test -p lithe-remote:122 个通过。新增:
    • transport:WSL 跳的命令行(不经 shell、--cd ~、--user)、identity、拒绝不合法名称和 NUL、不是第一跳时拒绝、发行版里接容器、wsl.exe 取自系统目录、环境变量保留并设置 WSL_UTF8=1;
    • trust:WSL 目标的序列化与往返、不同发行版或账户互不覆盖、拒绝不是第一跳的 WSL 和未知字段;
    • wsl:UTF-16(有无 BOM)和 UTF-8 解析、去掉 Docker 引擎发行版、去重、跳过提示行、64 个与 64 KiB 上限、字段去空白和逐项拒绝原因、拒绝 program 字段。
  • cargo test -p lithe-workspace:53 个通过。用替身 wsl.exe 跑完整个连接流程:
    • 检查替身收到的参数(--distribution Ubuntu-24.04 --user dev --cd ~ --exec …)和环境(有 WSL_UTF8=1、保留调用方变量、没有 SSH_ASKPASS_REQUIRE);
    • 发行版不存在时(UTF-16 输出)为 unreachable 且诊断可读,静默 exit 3 时为 failed 并写明退出码;
    • connection、wsl 都给或都不给,以及发行版、用户名不合法时直接拒绝,替身一次都没运行;
    • 列表:UTF-16 输出、--list --quiet 参数和 WSL_UTF8=1、失败时带提示且无名称、找不到程序和非 Windows 系统都是 available: false。
  • cargo test -p lithe-core --lib:888 个通过。connection_window_helpers_answer_through_the_dispatcher 新增:非 Windows 上列表返回 available: false;同时给 connection 和 wsl 时返回 invalid_request,details 为 {"field":"wsl","reason":"invalid"};wsl 带未知字段时返回 invalid_request。
  • cargo test -p lithe-diagnostics:71 个通过(日志目录重新生成后)。
  • cargo clippy -p lithe-remote -p lithe-workspace -p lithe-core --all-targets --all-features -- -D warnings、cargo fmt --all --check:通过。
  • scripts/rust-core/verify-rust-core-comments.sh、node scripts/rust-core/verify-platform-tests.mjs、node scripts/logging/generate-log-events.mjs(生成后无差异):通过。

还没在真 Windows 上确认的行为

替身程序按文档和 VS Code 的做法写,下面几项要在 Windows 虚拟机上装 WSL 实测,桌面端 PR 会一起做:

  • wsl.exe 的错误提示(发行版不存在、WSL 未安装)写到标准输出还是标准错误。现在两边都读:连接失败读标准错误,列表失败两边合并。
  • --exec 下标准输入输出是否完全不改字节(远端服务协议是二进制帧)。
  • 结束 wsl.exe 进程树后,发行版里的服务进程是否随之退出。
  • --user 指定不存在的用户时 wsl.exe 输出什么、退出码是多少;现在会归为 failed,并显示它的原文。
  • Windows 目标(x86_64-pc-windows-msvc)的 clippy 本机跑不了(libsqlite3-sys 需要 Windows 的 C 编译器),交给 CI 的 Windows 任务。

1lck/Lithe-IDEA#2231)

remote.connect accepts wsl: { distribution, user? } instead of connection.
Core then runs %SystemRoot%\System32\wsl.exe --distribution <d> [--user <u>]
--cd ~ --exec … in place of OpenSSH; the probe, the checked upload, the
stages and the session are the same as over SSH.

- Transport: the WSL hop validates the user as a Linux login name (at most
  32 bytes), is allowed as the first hop only, keeps the Windows environment
  wsl.exe needs and sets WSL_UTF8=1. Identity is wsl:[user@]distribution.
- Trust: decisions name the distribution and the account as the first and
  only hop; WSL after SSH, WSL after WSL and unknown fields are refused.
- Connection: WSL starts no AskPass session. A wsl.exe that exits before the
  server starts is classified from its stable error codes
  (WSL_E_DISTRO_NOT_FOUND is unreachable), with its UTF-16 or UTF-8 message
  decoded for diagnostics; a silent exit names wsl.exe and the status.
  A request naming both connection and wsl, or neither, is invalid_request
  with the field, before anything starts.
- remote.listWslDistributions (no payload) runs wsl.exe --list --quiet with
  a 15 s bound and returns { available, distributions, diagnostics }: at most
  64 names in wsl.exe order, without Docker Desktop's engine distributions
  and case-insensitive repeats. Other systems answer unavailable without
  running anything.
- Logging: the connection kind wsl, and the cleanup and diagnostics stage
  descriptions cover wsl.exe; catalog regenerated.
- Contract: remote.connect's wsl field, its refusals, and the new command.

Tests use a stand-in wsl.exe to run the whole connection, check the
arguments and environment it receives, the UTF-16 failure path and the
listing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant