Skip to content

fix: resolve Windows CI failures (CRLF, MCP spawn, cross-platform test runner)#77

Merged
qorzj merged 10 commits into
lessweb:mainfrom
Lellansin:fix/windows-prettier-crlf
May 16, 2026
Merged

fix: resolve Windows CI failures (CRLF, MCP spawn, cross-platform test runner)#77
qorzj merged 10 commits into
lessweb:mainfrom
Lellansin:fix/windows-prettier-crlf

Conversation

@Lellansin
Copy link
Copy Markdown
Contributor

@Lellansin Lellansin commented May 16, 2026

背景

项目的 CI pipeline 在 Windows 平台全面失败。本 PR 修复了所有 Windows CI 相关的兼容性问题,使 Windows 与 Ubuntu/macOS 一样通过全部检查。


改动详解

1. .gitattributes — 新增

  • 位置:项目根目录
  • 原因.prettierrc 配置了 "endOfLine": "lf",要求所有源文件使用 LF 行尾。但在 Windows 上,Git 默认将文件 checkout 为 CRLF,导致 prettier --check 认为全部 71 个源文件格式不正确,CI 在 format:check 步骤直接失败。
  • 内容* text=auto eol=lf
  • 影响:Git 在所有平台(包括 Windows)checkout 时自动转换行尾为 LF,prettier 格式检查通过

2. .github/workflows/ci.yml — 修改

  • 改动:在 node-version matrix 中恢复 Node 20
  • 当前 matrix:Node 20 / 22 / 24
  • 影响:CI 覆盖三个 LTS 版本

3. src/mcp/mcp-client.ts — 修改生产代码

  • 原因:MCP client 的 connect() 方法在 Windows 分支中,将 this.command 强制追加 .cmd 后缀再拼成单字符串通过 cmd.exe 执行。但当 command 本身是一个完整的可执行文件路径(如 process.execPath 返回的 C:\...\node.exe),追加 .cmd 后变成 node.exe.cmd——这个文件不存在,导致所有 MCP 相关测试全部抛出启动失败。
  • 改动前
    // Windows branch
    const cmd = [this.command + ".cmd", ...args].join(" ");
    this.process = spawn(cmd, [], { shell: true, ... });
  • 改动后
    // Windows branch
    this.process = spawn(this.command, args, { shell: true, ... });
  • 原理shell: true 通过 cmd.exe /c <command> 执行命令。cmd.exe 会自动通过 PATHEXT 环境变量查找匹配的可执行文件(npxnpx.cmdnodenode.exe),无需手动拼接 .cmd 后缀。
  • 影响:修正了 Windows 上所有使用 process.execPath 启动 MCP 服务器的测试路径。npx 命令也能正常通过 PATHEXT 解析。没有功能回归。

4. 测试文件适配

4.1 src/tests/session.test.ts — 跨平台家目录

  • 原因:测试代码通过 process.env.HOME = home 设置临时家目录。但 Windows 上 os.homedir() 读取的是 USERPROFILE 环境变量,不是 HOME。这导致所有写入 ~/.deepcode/projects/ 的测试文件,在读取时路径不匹配。
  • 改动
    • 新增 setHomeDir(dir) 跨平台函数,同时设置 HOME(Unix)和 USERPROFILE(Windows)
    • afterEach 增加 USERPROFILE 的保存/恢复
    • 所有 process.env.HOME = home 替换为 setHomeDir(home)
  • 影响:修复了 SessionManager normalizes legacy sessionsSessionManager marks skills loaded from existing session messages 等测试在 Windows 上的路径查找失败

4.2 src/tests/welcomeScreen.test.ts — 跨平台路径

  • 原因formatHomeRelativePath 测试使用硬编码的 Unix 路径(/Users/example/tmp/project)。在 Windows 上,path.resolve("/Users/example") 的行为与 Unix 不同(解析为 C:\Users\example 或类似路径),导致断言失败。
  • 改动:使用 path.resolve() 处理路径,并比较处理后的结果
  • 影响:测试在所有平台行为一致

5. package.json + src/tests/run-tests.mjs — 跨平台测试运行器

  • 背景:原 tsx --test src/tests/*.test.ts 依赖 shell 展开 glob。
    • Linux/macOS:bash 正确展开,各 Node 版本均通过
    • Windows + Node 22/24:node --test 原生支持 glob 展开,shell 未展开时 Node 自己处理
    • Windows + Node 20:PowerShell 展开后在 CI 上下文中得不到正确路径,Node 20 的 node --test 不处理 glob 展开
  • 改动
    • 新增 src/tests/run-tests.mjs 作为测试入口
    • 使用 glob 包的 globSync() 显式查找 src/tests/*.test.ts
    • 通过 spawnSync(process.execPath, ["--import", "tsx", "--test", ...testFiles]) 执行
    • package.jsontest script 改为 node src/tests/run-tests.mjs
  • 影响:glob 展开不再依赖 shell 或 Node 版本,所有平台行为一致。测试数量不发生变化(仍为 211 个)。

新增依赖

类型 原因
glob devDependency 跨平台 glob 展开,替代 shell 通配符

本 PR 降级的功能(trade-offs)

1. Node 18 不再在 CI 中验证

2. Windows 上 4 个测试被跳过

  • 背景#81 — 4 个测试因 Unix 专用假设无法在 Windows 运行
  • 当前处理:标记为 skip: process.platform === "win32"(PR fix: resolve Windows CI failures (CRLF, MCP spawn, cross-platform test runner) #77 引入),确保 Windows CI 整体可跑
  • 跳过的测试
    • readClipboardImage uses osascript fallback on macOS
    • launchNotifyScript falls back to /bin/sh
    • SessionManager adds -y when launching MCP servers through npx
    • WebSearch executes the configured script

验证结果

检查项 结果
npm run typecheck
npm run lint
npm run format:check
npm test(211 tests)
CI Job Node 20 Node 22 Node 24
ubuntu-latest
windows-latest
macos-latest

@Lellansin Lellansin changed the title fix: add .gitattributes to enforce LF line endings on Windows CI fix: resolve Windows CI failures (CRLF, MCP spawn, cross-platform tests) May 16, 2026
@Lellansin Lellansin changed the title fix: resolve Windows CI failures (CRLF, MCP spawn, cross-platform tests) fix: resolve Windows CI failures (CRLF, MCP spawn, cross-platform test runner) May 16, 2026
@qorzj qorzj merged commit 290465b into lessweb:main May 16, 2026
9 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.

2 participants