Skip to content

feat: add initial WSL environment detection - #4

Open
onedotmint wants to merge 2 commits into
Wing900:masterfrom
onedotmint:feat/wsl-detection-clean
Open

feat: add initial WSL environment detection#4
onedotmint wants to merge 2 commits into
Wing900:masterfrom
onedotmint:feat/wsl-detection-clean

Conversation

@onedotmint

@onedotmint onedotmint commented Aug 8, 2026

Copy link
Copy Markdown

背景

我目前是在 Windows 上使用 Pi Switch,但 Pi 本身主要运行在 WSL 中。现有 Pi Switch 还不能直接识别或管理 WSL 内的 Pi 环境

这个 PR 先补充一层基础的、只读的 WSL 环境检测能力,为后续支持 Windows / WSL Pi 环境切换和配置管理做准备。

目前这个 PR 仍然不涉及配置修改或 UI,只包含两个基础步骤:

  1. 检测 WSL 是否可用,并枚举当前已安装的 WSL distributions
  2. 对指定 distro 检测 Pi CLI、HOME 和 Pi 配置路径状态

本次改动

WSL distribution detection

  • 新增 WSL 可用性检测
  • 通过 wsl.exe --list --quiet 枚举已安装的 distributions
  • 新增 App.GetWSLDetection
  • 增加针对 wsl.exe UTF-16LE 输出的解析
  • parser 支持 BOM、CRLF、空输出、结尾 NUL 和合法 surrogate pair
  • Windows GUI 下调用 wsl.exe 时使用 CREATE_NO_WINDOW,避免弹出命令行窗口
  • 非 Windows 平台使用 build-tagged stub

当前可以区分:

  • WSL 不可用
  • WSL 可用但没有已安装的 distribution
  • WSL 可用且存在一个或多个 distributions

WSL Pi detection

新增 App.GetWSLPiDetection(distro),用于只读检测指定 WSL distro 中的:

  • Linux $HOME
  • pi 是否能在固定的非交互环境 PATH 中找到
  • pi 的实际路径
  • ~/.pi 是否存在
  • ~/.pi/agent/settings.json 是否存在
  • ~/.pi/agent/models.json 是否存在

Windows 下通过结构化参数执行:

wsl.exe --distribution <distro> --exec sh -c <fixed probe>

distro 作为独立 argument 传入,不会拼接到 shell script 中。

probe 本身是固定的只读脚本,不会读取或修改 Pi 配置,也不会 source .bashrc.profile.zshrc 或启动 login / interactive shell。

因此这里的 PiAvailable 只表示 pi 能否在这个固定的非交互 WSL 环境中通过 PATH 找到,并不表示 Pi 一定没有安装。例如只通过 NVM shell profile 暴露的 Pi,目前可能返回 PiAvailable: false

不存在的 distro 会返回明确错误;Pi、.pi 或配置文件不存在则作为正常检测结果返回。

实现说明

WSL distribution 枚举直接调用:

wsl.exe --list --quiet

Pi detection 则调用:

wsl.exe --distribution <distro> --exec sh -c <fixed probe>

两处都没有经过 cmd.exe、PowerShell 或动态 shell 字符串拼接。

distribution list 在实际 Windows 环境中返回 UTF-16LE,因此单独进行了对应解析;WSL 内 probe 的输出则使用严格 UTF-8 和固定 NUL 分隔协议。

测试

已通过:

  • go test -count=1 ./...
  • go vet ./...
  • go build ./...
  • Windows amd64 internal/wsl compile check
  • macOS arm64 internal/wsl compile check
  • npm --prefix frontend run build
  • wails build -platform windows/amd64
  • gofmt -d
  • git diff --check

另外进行了真实 Windows 11 + WSL2 smoke test:

  • WSL distribution 列表与 wsl.exe --list --quiet 一致
  • 指定 Ubuntu distro 可以正确返回 HOME、Pi path 和 Pi config 状态
  • 返回结果与 WSL 内独立 probe 一致
  • 不存在的 distro 会正确返回 error
  • 调用 WSL probe 时没有观察到 console flash

当前范围

这个 PR 仍然只做只读 detection,没有涉及:

  • Windows / WSL 环境持久化选择
  • Pi 配置写入
  • settings.json / models.json 修改
  • configsync 改造
  • Provider / launcher 修改
  • WSL settings UI

后续规划

如果这个方向符合项目预期,后续可以继续逐步补充:

  1. Windows / WSL Pi 环境选择
  2. WSL 内 Pi 配置读写
  3. configsync / launcher 等与 WSL 环境的集成
  4. WSL 相关设置 UI

我会继续尽量保持每一步范围较小、可测试,也方便单独 review。如果您对后续实现方式或功能边界有其他偏好,我可以再按项目方向调整。

@Wing900

Wing900 commented Aug 8, 2026

Copy link
Copy Markdown
Owner

在保证代码可维护的情况下,我认为这个功能可以拓展。您可以随时提交您的PR🤗

下一个版本我会审核您的PR,并合并

@onedotmint onedotmint changed the title feat: add initial WSL distribution detection feat: add initial WSL environment detection Aug 9, 2026
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.

2 participants