bash-prefix-permission-shim.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { chmod, mkdir, writeFile } from "node:fs/promises";
  2. import { delimiter, join } from "node:path";
  3. const BASH_PREFIX_PERMISSION_SPEC =
  4. "conversation-session-v4-bash-command-prefix-permission.test.ts";
  5. const CASE_RUNTIME_DIR = "conversation-session-v4-bash-command-prefix-permission";
  6. const PNPM_SHIM = `#!/bin/sh
  7. case " $* " in
  8. *" run lint "*)
  9. printf "%s\\n" "lint-ok"
  10. ;;
  11. *" run test "*)
  12. printf "%s\\n" "test-ok"
  13. ;;
  14. *)
  15. printf "%s\\n" "unsupported deterministic pnpm fixture: $*" >&2
  16. exit 64
  17. ;;
  18. esac
  19. `;
  20. export async function installBashPrefixPermissionPnpmShim(params: {
  21. environment: NodeJS.ProcessEnv;
  22. homeDir: string;
  23. specs: readonly string[];
  24. }): Promise<string | undefined> {
  25. if (!params.specs.some((spec) => spec.includes(BASH_PREFIX_PERMISSION_SPEC))) {
  26. return undefined;
  27. }
  28. const shimDirectory = join(params.homeDir, "ZCodeProject", ".zcode-e2e", CASE_RUNTIME_DIR, "bin");
  29. const shimPath = join(shimDirectory, "pnpm");
  30. await mkdir(shimDirectory, { recursive: true });
  31. await writeFile(shimPath, PNPM_SHIM, { encoding: "utf8", mode: 0o755 });
  32. if (process.platform !== "win32") {
  33. await chmod(shimPath, 0o755);
  34. }
  35. // Bug 根因:BPR 只验证 pnpm script 的权限 prefix,却调用 runner 上的真实 pnpm;
  36. // 全量 E2E 下外部 store/启动状态偶发卡住子进程。只给本 case 的 Electron/Host/Agent
  37. // 继承确定性 shim,命令文本和 Bash/prefix 执行链保持不变,其他 spec 的 PATH 不受影响。
  38. // Windows 通常保留 `Path` 的原始大小写;另写 `PATH` 会让 spawn 收到两个同名变量,
  39. // Node 在序列化环境时可能选中未注入 shim 的旧值,因此必须原位更新已有 key。
  40. const pathKey =
  41. Object.keys(params.environment).find((key) => key.toLowerCase() === "path") ?? "PATH";
  42. const currentPath = params.environment[pathKey] ?? "";
  43. const entries = currentPath.split(delimiter).filter(Boolean);
  44. params.environment[pathKey] = [
  45. shimDirectory,
  46. ...entries.filter((entry) => entry !== shimDirectory),
  47. ].join(delimiter);
  48. return shimPath;
  49. }