{"record":{"id":"af16c67a099313bb","repo":"chenglou/pretext","slug":"invalid-accuracy-check-port-requestedportraw","errorCode":null,"errorMessage":"Invalid ACCURACY_CHECK_PORT: ${requestedPortRaw}","messagePattern":"Invalid ACCURACY_CHECK_PORT: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/accuracy-check.ts","lineNumber":185,"sourceCode":"    if (mismatch.diagnosticLines && mismatch.diagnosticLines.length > 0) {\n      for (const line of mismatch.diagnosticLines) {\n        console.log(`   ${line}`)\n      }\n    }\n  }\n}\n\nlet serverProcess: ChildProcess | null = null\nlet proxyServer: HttpServer | null = null\nconst lock = await acquireBrowserAutomationLock(browser)\nconst output = parseStringFlag('output')\nconst usePostedReport = includeFullRows\nconst requestedPortRaw = process.env['ACCURACY_CHECK_PORT']\nconst requestedPort = (() => {\n  if (requestedPortRaw === undefined) return null\n  const parsed = Number.parseInt(requestedPortRaw, 10)\n  if (!Number.isFinite(parsed)) {\n    throw new Error(`Invalid ACCURACY_CHECK_PORT: ${requestedPortRaw}`)\n  }\n  return parsed\n})()\n\ntry {\n  let baseUrl: string\n\n  if (browser === 'firefox') {\n    const bunPort = await getAvailablePort()\n    const bunBaseUrl = `http://localhost:${bunPort}/accuracy`\n    serverProcess = spawn('/bin/zsh', ['-lc', `bun --port=${bunPort} --no-hmr pages/*.html`], {\n      cwd: process.cwd(),\n      stdio: 'ignore',\n    })\n    await waitForServer(bunBaseUrl)\n\n    const proxy = await startProxyServer(`http://[::1]:${bunPort}`)\n    proxyServer = proxy.server","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/chenglou/pretext/blob/ac49b09b7d83ede19581fa94a8b892b07d309baf/scripts/accuracy-check.ts#L167-L203","documentation":"Thrown by an IIFE at accuracy-check.ts:181-188 that parses the ACCURACY_CHECK_PORT environment variable. If the variable is set (not undefined) but Number.parseInt yields NaN (not finite), the value is unusable as a port and it throws immediately. An undefined variable is allowed (returns null, meaning 'pick any free port'); only a present-but-non-numeric value is rejected.","triggerScenarios":"Exporting ACCURACY_CHECK_PORT='' (empty string — parseInt('') is NaN), ACCURACY_CHECK_PORT=auto, ACCURACY_CHECK_PORT=any, or ACCURACY_CHECK_PORT=:8080. Note parseInt is lenient with trailing junk, so '8080abc' parses to 8080 and does NOT trip this guard; only values with no leading digits do.","commonSituations":"A CI matrix sets ACCURACY_CHECK_PORT from a templated variable that resolved to empty; a shell alias exports it unconditionally with a placeholder; copy-pasting a command that left the value blank; a .env file with `ACCURACY_CHECK_PORT=` (trailing nothing).","solutions":["Set ACCURACY_CHECK_PORT to a concrete integer (e.g. 8080) or unset it entirely to let the script pick a free port.","If the value comes from a template, default it: ACCURACY_CHECK_PORT=\"${ACCURACY_CHECK_PORT:-0}\" and treat 0 as 'auto'.","Audit .env files and CI secrets for an empty ACCURACY_CHECK_PORT assignment.","Validate before exporting: only export when the value matches /^[0-9]+$/."],"exampleFix":"# before\nexport ACCURACY_CHECK_PORT=\n\n# after (unset to use auto, or set a real port)\nunset ACCURACY_CHECK_PORT\n# or\nexport ACCURACY_CHECK_PORT=8080","handlingStrategy":"validation","validationCode":"// Coerce and validate the env var before the script reads it.\nconst raw = process.env.ACCURACY_CHECK_PORT\nconst port = raw === undefined || raw === '' ? null : Number.parseInt(raw, 10)\nif (port !== null && !Number.isFinite(port)) {\n  throw new Error(`Invalid ACCURACY_CHECK_PORT: ${raw}`)\n}","typeGuard":"function isValidPortEnv(raw: string | undefined): boolean {\n  return raw === undefined || raw === '' || /^\\d+$/.test(raw)\n}","tryCatchPattern":"// The IIFE throws at module top-level; wrap the whole script entry or validate first.\nconst requestedPortRaw = process.env['ACCURACY_CHECK_PORT']\nif (requestedPortRaw !== undefined && requestedPortRaw !== '' && !/^\\d+$/.test(requestedPortRaw)) {\n  console.error(`Invalid ACCURACY_CHECK_PORT: ${requestedPortRaw}`)\n  process.exit(2)\n}","preventionTips":["Default the env var with parameter expansion: ACCURACY_CHECK_PORT=\"${ACCURACY_CHECK_PORT:-}\" and treat empty as auto.","Audit .env and CI config for empty ACCURACY_CHECK_PORT assignments.","Only export the variable when it matches /^[0-9]+$/."],"tags":["accuracy-check","config","environment-variable","validation","port"],"backgroundTag":null,"analyzedSha":"ac49b09b7d83ede19581fa94a8b892b07d309baf","analyzedAt":"2026-08-12T17:03:16.263Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}