{"record":{"id":"1f59c94338f08024","repo":"withastro/astro","slug":"incorrect-value-for-key","errorCode":null,"errorMessage":"Incorrect value for ${key}","messagePattern":"Incorrect value for (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/astro/src/preferences/index.ts","lineNumber":78,"sourceCode":"\nexport function isValidKey(key: string): key is PreferenceKey {\n\treturn dget(DEFAULT_PREFERENCES, key) !== undefined;\n}\nexport function coerce(key: string, value: unknown) {\n\tconst type = typeof dget(DEFAULT_PREFERENCES, key);\n\t// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check\n\tswitch (type) {\n\t\tcase 'string':\n\t\t\treturn value;\n\t\tcase 'number':\n\t\t\treturn Number(value);\n\t\tcase 'boolean': {\n\t\t\tif (value === 'true' || value === 1) return true;\n\t\t\tif (value === 'false' || value === 0) return false;\n\t\t\tbreak;\n\t\t}\n\t\tdefault:\n\t\t\tthrow new Error(`Incorrect value for ${key}`);\n\t}\n\treturn value as any;\n}\n\nexport default function createPreferences(\n\tconfig: Record<string, any>,\n\tdotAstroDir: URL,\n): AstroPreferences {\n\tconst global = new PreferenceStore(getGlobalPreferenceDir());\n\tconst project = new PreferenceStore(fileURLToPath(dotAstroDir));\n\tconst stores: Record<PreferenceLocation, PreferenceStore> = { global, project };\n\n\treturn {\n\t\tasync get(key, { location } = {}) {\n\t\t\tif (!location) return project.get(key) ?? global.get(key) ?? dget(DEFAULT_PREFERENCES, key);\n\t\t\treturn stores[location].get(key);\n\t\t},\n\t\tasync set(key, value, { location = 'project', reloadServer = true } = {}) {","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/withastro/astro/blob/52e6c34790cc8ac4e69e6135ace06049867e5c4a/packages/astro/src/preferences/index.ts#L60-L96","documentation":"Astro persists preferences (telemetry ok, update checks, first-wizard flags) as JSON with a type marker (string/number/boolean) in per-project (.astro dir) and global preference stores. On read, the deserializer switches on that stored type; the default branch throws `Incorrect value for ${key}` when the stored type is something else - i.e. the preferences file is corrupted, hand-edited, or was written by an incompatible Astro version.","triggerScenarios":"A preferences JSON file where an entry's type field is missing, misspelled, or a non-supported value (e.g. 'object' from a future version); manually editing .astro/preferences.json or the global preferences file; partial writes from a killed process leaving truncated JSON metadata.","commonSituations":"Checking .astro/preferences.json into version control and merging conflicts badly; switching between very old and new Astro versions on the same machine; scripts or tools rewriting files inside the .astro directory.","solutions":["Reset the offending preference: `npx astro preferences unset <name>` (or disable/enable it again).","If unset itself fails, delete the store file - project: <root>/.astro/preferences.json, global: the Astro preferences dir in your home directory - Astro recreates it with defaults.","Stop committing or hand-editing the preferences file, and regenerate it after version switches."],"exampleFix":"# before - .astro/preferences.json contains a broken entry\n{ \"telemetry\": { \"value\": 1, \"type\": \"bool\" } } // wrong type marker\n\n# after - reset to a well-formed state\nnpx astro preferences unset telemetry\n# or simply delete the file:\nrm .astro/preferences.json","handlingStrategy":"fallback","validationCode":"// guard before reading a preference: verify the stored type marker\nfunction isKnownPrefType(t: unknown): t is 'string' | 'number' | 'boolean' {\n  return t === 'string' || t === 'number' || t === 'boolean';\n}","typeGuard":null,"tryCatchPattern":"try {\n  const value = astroPreferences.get('telemetry');\n} catch {\n  // store is corrupt: reset to defaults and continue\n  execSync('npx astro preferences unset telemetry', { stdio: 'ignore' });\n}","preventionTips":["Do not commit or hand-edit .astro/preferences.json.","After switching between distant Astro versions, delete stale .astro state if commands misbehave.","Treat the .astro directory as generated output, not source."],"tags":["preferences","cli","config","corruption"],"backgroundTag":"corrupt-stored-config","analyzedSha":"52e6c34790cc8ac4e69e6135ace06049867e5c4a","analyzedAt":"2026-08-18T18:48:03.901Z","contentChangedAt":"2026-08-18T18:48:03.901Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}