Skip to content

refactor(platform): 平台规则改为数据,平台专属测试移到 lithe-platform-tests - #48

Merged
xiaoyumuxi merged 1 commit into
mainfrom
refactor/platform-generic-helpers
Nov 15, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
refactor/platform-generic-helpers

Conversation

@xiaoyumuxi

@xiaoyumuxi xiaoyumuxi commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

关联 Issue

无。来自平台专属代码通用化的讨论:能用数据表达的平台差异改成按 Platform 参数计算,只能在真实系统上验证的测试移到单独的测试包。

类型

refactor(含两处行为修复,见下)

改动点

新增 lithe-platform crate(平台规则即数据)

  • Platform { Unix, Windows } 与 Platform::HOST,以及 is_windows、executable_name、null_device。
  • paths:relative_path_contains(按平台决定是否忽略大小写和分隔符)、slash_separated、native_path_from_bytes。
  • names:is_reserved_device_name、is_portable_windows_component。Windows 保留设备名只保留这一份(CON/PRN/AUX/NUL/CONIN$/CONOUT$/COM1–9/COM¹²³/LPT…),原来 git/src/repository.rs 和 project-generators/src/archive.rs 各有一份(git 那份缺 CONIN$/CONOUT$),现在都改为调用它。
  • fs:OWNER_ONLY_FILE_MODE、OWNER_ONLY_DIRECTORY_MODE、owner_only_file、directory_mode、restrict_directory_to_owner、unix_mode、has_execute_bit、is_launchable_file、add_execute_bits 等。调用方不用再写 #[cfg(unix)]。
  • process:CREATE_NO_WINDOW、hide_console_window、verbatim_argument、exit_status_from_code。

这样纯规则函数能在任意一台机器上测两个平台的分支。各 crate 里原先 #[cfg(windows)] 的单元测试大多改成了 Platform::Windows / Platform::Unix 参数化测试,macOS 和 Linux 上也会跑。

行为修复与调整

  • LSP 进程在所有平台使用 ProcessTree(lsp-transport/src/process.rs):
    • 原来只有 Windows 用 Job Object。macOS/Linux 上终止或释放语言服务器时,只结束直接启动的进程;如果它是启动脚本(比如再拉起 JVM),子进程会一直存活。用旧代码跑新增的两个 LSP 进程树测试,在 macOS 上都会失败(10 秒期限到后子进程仍然存活);用新代码则通过。
    • dispose 先回收根进程,再 tree.terminate。原因是 Unix 进程组里未回收的僵尸根进程仍算存活,已有测试 a_unix_tree_ends_only_once_its_root_is_reaped 验证了这一点。
    • 副作用:Unix 语言服务器进入独立进程组,终端里的 Ctrl-C 不会再直接发给它,改由宿主负责结束。
    • 新增 BatchLaunch:Windows 上 .cmd/.bat 的命令行拼接集中到一处。
  • Git 路径校验:统一使用 lithe-platform 的保留名规则,因此新增拒绝 CONIN$、CONOUT$。
  • 补丁指纹(git/src/patch_exchange.rs):改为无条件计入文件模式。原来 Windows 上不计入,Windows 打开远程 Unix 工作区时会和 Unix 端算出的结果不一致。
  • SSH Connection::close(workspace/src/sessions.rs):原来先 tree.terminate(2s) 再 kill/wait 根进程;Unix 进程组把未回收的根进程算作存活,所以每次关闭都要等满 2 秒超时。现在先 tree.kill() 立即结束整棵树,再 kill/wait 回收根进程,最后 tree.terminate(2s) 只等可能残留的子进程。

新增 lithe-platform-tests 包(平台专属测试单独存放)

  • src/bin/lithe-platform-fixture.rs:测试用的 fixture 程序,取代原来依赖的 sh/cmd 命令和 sleep。共有 hold、parent、failure、stream、echo-arguments 五种模式。
    • 程序带 30 秒看门狗:测试失败时也会自行退出,不会遗留进程。
  • tests/platform/:所有测试编进同一个测试二进制。
    • process_tree.rs:从 git-host/tests/process.rs 迁入并重写。覆盖增量输出取消、非零退出保留输出、终止整棵进程树、Unix 根进程未回收时的超时,以及 Windows 上释放 Job 即结束进程树。
    • lsp_process.rs:语言服务器 terminate/dispose 时结束子进程。Windows 另有 batch 启动脚本的测试:带空格的路径和 & % ! ^ ( ) " 等元字符要原样传到程序;终止脚本时要连它启动的程序一起结束。
    • permissions.rs:Unix 上的 owner-only 文件和目录权限、可执行位;Windows 上没有 mode 时的行为。
    • git_executable.rs:Windows 上解析出的路径使用正斜杠(从 git-host/src/configuration.rs 迁入);Unix 上文件有可执行位后才会被采用。
  • 在单一平台的机器上运行这两个包的测试:cargo test -p lithe-platform -p lithe-platform-tests。

其他

  • shared/plugins/sdk/e2e/plugin_sdk/mod.rs:修复原有的 clippy needless_borrow 报错(listing(&package) 改为 listing(package)),这个报错和本次改动无关,但会挡住 clippy -D warnings。

仍保留 cfg 的地方(有意不改)

  • lithe-remote 服务端的平台适配器,以及它本身就分平台的测试。
  • git-host 的平台模块(Unix 进程组和 Windows Job Object 的实现本身)。
  • project-generators/archive.rs 的 reparse point 检查和 share_mode 断言。
  • 只在 Unix 上有意义的 symlink/chmod 测试,在 Linux 和 macOS 上运行。

验证

macOS(aarch64)上执行:

  • cargo test --workspace:2049 个通过,0 个失败。
  • cargo test -p lithe-platform-tests:集成测试 9 个、单元测试 2 个全部通过,用时 0.51 秒。之后检查过,没有残留的 fixture 进程。
  • cargo fmt --all --check。
  • cargo clippy --workspace --all-targets --all-features -D warnings。
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-msvc -D warnings:只用默认 features。--all-features 会打开 lithe-remote 的 server feature,它依赖的 trash crate 在 Windows 上本来就编不过(E0070);服务端只支持 Linux 和 macOS,这个问题在本次改动前就存在。
  • cargo clippy --workspace --all-targets --all-features --target x86_64-unknown-linux-musl -D warnings。
  • scripts/rust-core/verify-rust-core-comments.sh。
  • node scripts/logging/generate-log-events.mjs --check。
  • node shared/plugins/scripts/verify-docs.mjs。
  • write-stable-tests 的稳定性检查 verify-test-stability.mjs。

已知限制

  • Windows 测试没有实际运行:当前没有可用的 Windows 机器或虚拟机,Windows 分支只通过了 x86_64-pc-windows-msvc 的 clippy 交叉检查。合入前需要在 Windows 上跑一次 cargo test -p lithe-platform -p lithe-platform-tests,重点看 batch 参数测试和 Job Object 测试。
  • 原来的 background_git_processes_do_not_create_windows_console 检查的是常量标志位。它已经删掉,没有换成行为测试,因为没有 Windows 环境可以验证“确实没有弹出控制台窗口”。hide_console_window 目前也没有单元测试(CommandExt::creation_flags 设置后无法从 Command 读回),只能在 Windows 上确认。
  • Core 目前没有跑 cargo test 的 CI,以上结果都来自本地运行。

- 新增 lithe-platform crate:Platform { Unix, Windows } 与 Platform::HOST,
  路径包含判断、斜杠路径、Windows 保留设备名、owner-only 权限、可执行位、
  隐藏控制台窗口、batch 参数转义统一成按平台参数计算的函数
- LSP 进程在所有平台都放进 ProcessTree:macOS/Linux 终止或释放语言服务器时
  一并结束它启动的子进程(旧代码下子进程会存活);Unix 语言服务器进入独立
  进程组,终端 Ctrl-C 不再直接传给它
- Git 路径校验统一使用同一份保留名规则,新增拒绝 CONIN$/CONOUT$
- 补丁指纹无条件计入文件模式,Windows 打开远程 Unix 工作区时结果一致
- SSH Connection::close 调整关闭顺序,Unix 下不再固定等满 2 秒
- 新增 lithe-platform-tests 包:fixture 程序代替 shell 命令,迁入
  git-host/tests/process.rs,并新增 LSP 进程树、Windows batch 参数、权限、
  Git 可执行文件解析的真实系统测试
- 顺手修复 shared/plugins/sdk/e2e 中原有的 clippy needless-borrow 报错
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 15, 2026 05:02
@xiaoyumuxi
xiaoyumuxi merged commit dd09bb4 into main Nov 15, 2026
5 checks passed
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