|
| 1 | +// Temporary, key-free CI diagnosis for Cor-Incorporated/opencode#366. No production settings are changed. |
| 2 | +import { spawnSync } from "node:child_process" |
| 3 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" |
| 4 | +import { tmpdir } from "node:os" |
| 5 | +import path from "node:path" |
| 6 | +import { fileURLToPath } from "node:url" |
| 7 | +import { privateWindowsKeyAcl } from "./cor-local-env.js" |
| 8 | + |
| 9 | +if (process.platform !== "win32") { |
| 10 | + console.log("windows-acl-diagnostic: Windows runner required") |
| 11 | + process.exit(0) |
| 12 | +} |
| 13 | + |
| 14 | +const here = path.dirname(fileURLToPath(import.meta.url)) |
| 15 | +const fixture = readFileSync(path.join(here, "../profile/src/cor-local-bin-defaults.test.ts"), "utf8").match( |
| 16 | + /const script = `([\s\S]*?)`/, |
| 17 | +)?.[1] |
| 18 | +const probe = readFileSync(path.join(here, "cor-local-env.js"), "utf8").match( |
| 19 | + /const WINDOWS_KEY_ACL = `([\s\S]*?)`/, |
| 20 | +)?.[1] |
| 21 | +if (!fixture || !probe) throw new Error("ACL source script not found") |
| 22 | +const powershell = path.join(process.env.SystemRoot || "C:\\Windows", "System32/WindowsPowerShell/v1.0/powershell.exe") |
| 23 | + |
| 24 | +function diagnose(label, ignoreStdin) { |
| 25 | + const directory = mkdtempSync(path.join(tmpdir(), "opencode-acl-diagnostic-")) |
| 26 | + const file = path.join(directory, "fixture [key]'s.key") |
| 27 | + writeFileSync(file, "diagnostic-placeholder\n") |
| 28 | + const env = { |
| 29 | + ...process.env, |
| 30 | + OPENCODE_COR_LOCAL_TEST_PATH: file, |
| 31 | + OPENCODE_COR_LOCAL_TEST_PUBLIC: "0", |
| 32 | + OPENCODE_COR_LOCAL_ACL_PATH: file, |
| 33 | + } |
| 34 | + const run = (stage, script) => { |
| 35 | + const started = performance.now() |
| 36 | + const result = spawnSync(powershell, ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", script], { |
| 37 | + env, |
| 38 | + encoding: "utf8", |
| 39 | + timeout: 5_000, |
| 40 | + maxBuffer: 65_536, |
| 41 | + windowsHide: true, |
| 42 | + shell: false, |
| 43 | + ...(ignoreStdin ? { stdio: ["ignore", "pipe", "pipe"] } : {}), |
| 44 | + }) |
| 45 | + console.log( |
| 46 | + JSON.stringify({ |
| 47 | + label, |
| 48 | + runtime: process.versions.bun ? `Bun ${process.versions.bun}` : `Node ${process.versions.node}`, |
| 49 | + stage, |
| 50 | + status: result.status, |
| 51 | + signal: result.signal, |
| 52 | + error: result.error?.code, |
| 53 | + elapsed: Math.round(performance.now() - started), |
| 54 | + stdout: result.stdout?.slice(0, 512), |
| 55 | + stderr: result.stderr?.slice(0, 512), |
| 56 | + ...(stage === "Get-Acl" ? { privateAcl: privateWindowsKeyAcl(result) } : {}), |
| 57 | + }), |
| 58 | + ) |
| 59 | + return result.status === 0 && !result.error |
| 60 | + } |
| 61 | + try { |
| 62 | + run("exit 0", "exit 0") |
| 63 | + if (run("Set-Acl", fixture)) run("Get-Acl", probe) |
| 64 | + } finally { |
| 65 | + rmSync(directory, { recursive: true, force: true }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +if (process.argv[2] === "node") { |
| 70 | + diagnose("Node stdin ignore", true) |
| 71 | + process.exit(0) |
| 72 | +} |
| 73 | +diagnose("Bun stdin default", false) |
| 74 | +diagnose("Bun stdin ignore", true) |
| 75 | +const node = spawnSync("node", [fileURLToPath(import.meta.url), "node"], { |
| 76 | + encoding: "utf8", |
| 77 | + stdio: ["ignore", "pipe", "pipe"], |
| 78 | + timeout: 20_000, |
| 79 | +}) |
| 80 | +process.stdout.write(node.stdout || "") |
| 81 | +process.stderr.write(node.stderr || "") |
| 82 | +if (node.error || node.status !== 0) throw new Error(`Node ACL diagnostic failed: ${node.error?.code || node.status}`) |
0 commit comments