{"record":{"id":"27c53ec63e629990","repo":"microsoft/TypeScript","slug":"diagnostic-code-code-appears-more-than-once","errorCode":null,"errorMessage":"Diagnostic code ${code} appears more than once.","messagePattern":"Diagnostic code (.+?) appears more than once\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/processDiagnosticMessages.mjs","lineNumber":73,"sourceCode":"    }\r\n\r\n    const infoFileOutput = buildInfoFileOutput(diagnosticMessages, inputFilePath);\r\n    checkForUniqueCodes(diagnosticMessages);\r\n    await writeFile(\"diagnosticInformationMap.generated.ts\", infoFileOutput);\r\n\r\n    const messageOutput = buildDiagnosticMessageOutput(diagnosticMessages);\r\n    await writeFile(\"diagnosticMessages.generated.json\", messageOutput);\r\n}\r\n\r\n/**\r\n * @param {InputDiagnosticMessageTable} diagnosticTable\r\n */\r\nfunction checkForUniqueCodes(diagnosticTable) {\r\n    /** @type {Record<number, true | undefined>} */\r\n    const allCodes = [];\r\n    diagnosticTable.forEach(({ code }) => {\r\n        if (allCodes[code]) {\r\n            throw new Error(`Diagnostic code ${code} appears more than once.`);\r\n        }\r\n        allCodes[code] = true;\r\n    });\r\n}\r\n\r\n/**\r\n * @param {InputDiagnosticMessageTable} messageTable\r\n * @param {string} inputFilePathRel\r\n * @returns {string}\r\n */\r\nfunction buildInfoFileOutput(messageTable, inputFilePathRel) {\r\n    const result = [\r\n        \"// <auto-generated />\",\r\n        `// generated from '${inputFilePathRel}'`,\r\n        \"\",\r\n        'import { DiagnosticCategory, DiagnosticMessage } from \"./types.js\";',\r\n        \"\",\r\n        \"function diag(code: number, category: DiagnosticCategory, key: string, message: string, reportsUnnecessary?: {}, elidedInCompatabilityPyramid?: boolean, reportsDeprecated?: {}): DiagnosticMessage {\",\r","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/scripts/processDiagnosticMessages.mjs#L55-L91","documentation":"processDiagnosticMessages reads src/compiler/diagnosticMessages.json and runs checkForUniqueCodes, which flags any numeric `code` that appears in more than one entry. Duplicate codes would corrupt diagnostic lookup and reporting, so code-gen refuses to proceed.","triggerScenarios":"Adding a new diagnostic whose `code` is already used by another entry in diagnosticMessages.json.","commonSituations":"A contributor picks an already-allocated TS diagnostic code; a rebase brings in two diagnostics with the same code.","solutions":["Search diagnosticMessages.json for the duplicated code and renumber the new entry.","Pick the next free code in the appropriate range (consult neighboring entries).","Re-run `hereby generate-diagnostics` (or the processDiagnosticMessages task) to confirm."],"exampleFix":"// before\n{ \"code\": 1234, \"category\": \"Error\", ... }  // appears twice\n\n// after\n// change the new diagnostic to a free code, e.g.\n{ \"code\": 1235, \"category\": \"Error\", ... }","handlingStrategy":"validation","validationCode":"const codes = new Set();\nfor (const { code } of diagnosticMessages) {\n  if (codes.has(code)) throw new Error(`Duplicate diagnostic code ${code}`);\n  codes.add(code);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Allocate diagnostic codes from a single owned range and track the next-free code.","Run the diagnostics codegen task in CI to catch duplicates at PR time."],"tags":["diagnostics","build","validation","codegen"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}