{"record":{"id":"df9f77e4e82054c9","repo":"coleam00/Archon","slug":"failed-to-read-version-package-json-is-malformed","errorCode":null,"errorMessage":"Failed to read version: package.json is malformed","messagePattern":"Failed to read version: package\\.json is malformed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/version.ts","lineNumber":53,"sourceCode":"\n  let content: string;\n  try {\n    content = await readFile(pkgPath, 'utf-8');\n  } catch (error) {\n    const err = error as NodeJS.ErrnoException;\n    if (err.code === 'ENOENT') {\n      throw new Error('Failed to read version: package.json not found (bad installation?)');\n    } else if (err.code === 'EACCES') {\n      throw new Error('Failed to read version: permission denied reading package.json');\n    }\n    throw new Error(`Failed to read version: ${err.message}`);\n  }\n\n  let pkg: PackageJson;\n  try {\n    pkg = JSON.parse(content) as PackageJson;\n  } catch (_error) {\n    throw new Error('Failed to read version: package.json is malformed');\n  }\n\n  return { name: pkg.name, version: pkg.version };\n}\n\n/**\n * Get the git commit hash at runtime (dev mode).\n * Returns 'unknown' if git is unavailable or the command fails.\n */\nasync function getDevGitCommit(): Promise<string> {\n  try {\n    const { stdout } = await execFileAsync('git', ['rev-parse', '--short', 'HEAD'], {\n      timeout: 5000,\n    });\n    return stdout.trim();\n  } catch (err) {\n    // Non-blocking: git may not be installed or cwd may not be a git repo\n    log.debug({ err }, 'version.git_commit_lookup_failed');","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/coleam00/Archon/blob/0773b9745896ef0612e709c80845a0f7db315b19/packages/cli/src/commands/version.ts#L35-L71","documentation":"getDevVersion() reads the monorepo root package.json at runtime when the CLI runs in development (non-binary) mode. After a successful read, JSON.parse of the file contents is wrapped in try/catch; any parse failure becomes this error. It indicates the root package.json exists but is not valid JSON, so the version/name cannot be reported.","triggerScenarios":"Running `archon version` (via devInfo -> getDevVersion) in a source checkout whose root package.json was hand-edited, truncated by an interrupted merge/checkout, saved with a trailing comma or BOM, or corrupted so JSON.parse throws.","commonSituations":"Manual edit of package.json that left invalid JSON; a git merge conflict left conflict markers in the file; an interrupted npm/bun install wrote a partial file; a text editor or tool rewrote the file with a byte-order mark or comments.","solutions":["Validate the root package.json with `bun pm` or `node -e \"JSON.parse(require('fs').readFileSync('package.json','utf8'))\"` to see the exact parse error.","Fix the JSON by removing conflict markers, trailing commas, comments, or a BOM (UTF-8 without BOM).","Restore the file from git: `git checkout -- package.json` if it was committed and only locally corrupted.","Reinstall the checkout (fresh clone or `bun install`) if the corruption came from a tool."],"exampleFix":"// package.json (before — invalid)\n{ \"name\": \"archon\", \"version\": \"0.3.1\", }\n// after\n{ \"name\": \"archon\", \"version\": \"0.3.1\" }","handlingStrategy":"validation","validationCode":"import { readFileSync } from 'fs';\ntry {\n  const content = readFileSync('package.json', 'utf-8');\n  JSON.parse(content); // throws with position info before the CLI ever runs\n} catch (e) {\n  console.error('package.json is not valid JSON:', (e as Error).message);\n}","typeGuard":"function isValidPackageJson(s: string): boolean {\n  try {\n    const p: unknown = JSON.parse(s);\n    return typeof p === 'object' && p !== null && 'name' in p && 'version' in p;\n  } catch {\n    return false;\n  }\n}","tryCatchPattern":"try {\n  await versionCommand();\n} catch (e) {\n  if ((e as Error).message.startsWith('Failed to read version')) {\n    // fix or restore package.json, or pin a binary release which embeds the version\n  }\n}","preventionTips":["Validate package.json after any manual edit (`node -e \"JSON.parse(...)\"`).","Resolve merge conflicts in package.json immediately; never commit conflict markers.","Prefer `bun version` / npm version commands over hand-editing the version field."],"tags":["cli","package-json","json-parse","configuration"],"backgroundTag":"malformed-package-json","analyzedSha":"0773b9745896ef0612e709c80845a0f7db315b19","analyzedAt":"2026-09-01T02:28:07.064Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}