|
| 1 | +// Temporary, key-free diagnosis for #366; never used by the production launcher. |
| 2 | +import { spawnSync } from "node:child_process" |
| 3 | +import { createHash } from "node:crypto" |
| 4 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" |
| 5 | +import { tmpdir } from "node:os" |
| 6 | +import path from "node:path" |
| 7 | +import { fileURLToPath } from "node:url" |
| 8 | + |
| 9 | +const here = path.dirname(fileURLToPath(import.meta.url)) |
| 10 | +const sources = { |
| 11 | + "exit 0": "exit 0", |
| 12 | + "Set-Acl": readFileSync(path.join(here, "../profile/src/cor-local-bin-defaults.test.ts"), "utf8").match( |
| 13 | + /const script = `([\s\S]*?)`/, |
| 14 | + )?.[1], |
| 15 | + "Get-Acl": readFileSync(path.join(here, "cor-local-env.js"), "utf8").match( |
| 16 | + /const WINDOWS_KEY_ACL = `([\s\S]*?)`/, |
| 17 | + )?.[1], |
| 18 | +} |
| 19 | + |
| 20 | +function presence() { |
| 21 | + const names = new Set(Object.keys(process.env).map((key) => key.toUpperCase())) |
| 22 | + return { |
| 23 | + ...Object.fromEntries( |
| 24 | + ["SYSTEMROOT", "WINDIR", "PSMODULEPATH", "USERPROFILE", "TEMP", "TMP", "PATH", "LOCALAPPDATA", "COMSPEC"].map( |
| 25 | + (key) => [key, names.has(key)], |
| 26 | + ), |
| 27 | + ), |
| 28 | + SystemRootLookup: Boolean(process.env.SystemRoot), |
| 29 | + SYSTEMROOTLookup: Boolean(process.env.SYSTEMROOT), |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function probe(stage) { |
| 34 | + const script = sources[stage] |
| 35 | + if (!script) throw new Error("Unknown ACL diagnostic stage") |
| 36 | + const dir = mkdtempSync(path.join(tmpdir(), "opencode-acl-transport-")) |
| 37 | + const file = path.join(dir, "fixture [key]'s.key") |
| 38 | + writeFileSync(file, "diagnostic-placeholder\n") |
| 39 | + const started = performance.now() |
| 40 | + try { |
| 41 | + const result = spawnSync( |
| 42 | + path.join(process.env.SystemRoot || "C:\\\\Windows", "System32", "WindowsPowerShell", "v1.0", "powershell.exe"), |
| 43 | + ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", script], |
| 44 | + { |
| 45 | + env: { |
| 46 | + ...process.env, |
| 47 | + OPENCODE_COR_LOCAL_TEST_PATH: file, |
| 48 | + OPENCODE_COR_LOCAL_TEST_PUBLIC: "0", |
| 49 | + OPENCODE_COR_LOCAL_ACL_PATH: file, |
| 50 | + }, |
| 51 | + encoding: "utf8", |
| 52 | + stdio: ["ignore", "pipe", "pipe"], |
| 53 | + timeout: 5_000, |
| 54 | + maxBuffer: 65_536, |
| 55 | + windowsHide: true, |
| 56 | + shell: false, |
| 57 | + }, |
| 58 | + ) |
| 59 | + return { |
| 60 | + stage, |
| 61 | + runtime: process.versions.bun ? `Bun ${process.versions.bun}` : `Node ${process.versions.node}`, |
| 62 | + envPresence: presence(), |
| 63 | + scriptSha256: createHash("sha256").update(script).digest("hex"), |
| 64 | + status: result.status, |
| 65 | + signal: result.signal, |
| 66 | + errorCode: result.error?.code, |
| 67 | + elapsed: Math.round(performance.now() - started), |
| 68 | + stdoutBytes: result.stdout?.length || 0, |
| 69 | + stderrBytes: result.stderr?.length || 0, |
| 70 | + } |
| 71 | + } finally { |
| 72 | + rmSync(dir, { recursive: true, force: true }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export function diagnose(stage, transport) { |
| 77 | + if (transport === "bun") return probe(stage) |
| 78 | + const result = spawnSync("node", [fileURLToPath(import.meta.url), stage], { |
| 79 | + env: process.env, |
| 80 | + encoding: "utf8", |
| 81 | + stdio: ["ignore", "pipe", "pipe"], |
| 82 | + timeout: 5_000, |
| 83 | + maxBuffer: 65_536, |
| 84 | + windowsHide: true, |
| 85 | + shell: false, |
| 86 | + }) |
| 87 | + if (result.status === 0 && !result.error) return JSON.parse(result.stdout) |
| 88 | + return { |
| 89 | + stage, |
| 90 | + runtime: "Node child did not report", |
| 91 | + envPresence: presence(), |
| 92 | + status: result.status, |
| 93 | + signal: result.signal, |
| 94 | + errorCode: result.error?.code, |
| 95 | + stdoutBytes: result.stdout?.length || 0, |
| 96 | + stderrBytes: result.stderr?.length || 0, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { |
| 101 | + if (process.platform !== "win32" || process.versions.node.split(".")[0] !== "24") |
| 102 | + throw new Error("ACL diagnostic child requires Windows Node 24") |
| 103 | + console.log(JSON.stringify(probe(process.argv[2]))) |
| 104 | +} |
0 commit comments