{"record":{"id":"d5272f38d2002573","repo":"oven-sh/bun","slug":"failed-to-write-file-file","errorCode":null,"errorMessage":"Failed to write file ${file}","messagePattern":"Failed to write file (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/codegen/helpers.ts","lineNumber":60,"sourceCode":"  if (Array.isArray(contents)) contents = contents.join(\"\");\n  contents = contents.replaceAll(\"\\r\\n\", \"\\n\").trim() + \"\\n\";\n\n  try {\n    const oldContents = fs.readFileSync(file, \"utf8\");\n    if (oldContents === contents) {\n      return;\n    }\n  } catch (e) {}\n\n  try {\n    fs.writeFileSync(file, contents);\n  } catch (error) {\n    fs.mkdirSync(path.dirname(file), { recursive: true });\n    fs.writeFileSync(file, contents);\n  }\n\n  if (fs.readFileSync(file, \"utf8\") !== contents) {\n    throw new Error(`Failed to write file ${file}`);\n  }\n}\n\nexport function writeIfNotChangedBinary(file: string, contents: Buffer) {\n  try {\n    if (fs.readFileSync(file).equals(contents)) return;\n  } catch {}\n  try {\n    fs.writeFileSync(file, contents);\n  } catch {\n    fs.mkdirSync(path.dirname(file), { recursive: true });\n    fs.writeFileSync(file, contents);\n  }\n}\n\nexport function readdirRecursiveWithExclusionsAndExtensionsSync(\n  dir: string,\n  exclusions: string[],","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/src/codegen/helpers.ts#L42-L78","documentation":"writeIfNotChanged writes generated content, then reads the file back and requires byte-for-byte equality. The helper already retries once after mkdir-ing the parent directory, so this error means the write silently failed or the file on disk does not match what was written.","triggerScenarios":"Out-of-space disk filling during write, a read-only or permission-denied directory where writeFileSync appears to succeed, the target path existing as a directory, or an external process (antivirus, sync client) rewriting the file between write and read-back.","commonSituations":"Full disk / full Docker volume during `bun run build`; building into a checkout owned by another user; case-insensitive filesystem collisions; cloud-sync folders normalizing file content.","solutions":["Check disk space and write permissions on the output directory (df -h, ls -ld), then rerun the build.","Make sure the output path is a file location, not an existing directory, and that no symlink loop or case-insensitive collision shadows it.","Exclude build output directories from antivirus/sync tools, then rebuild."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"import fs from \"node:fs\";\nfunction canWrite(file: string) {\n  try {\n    fs.accessSync(file.endsWith(\"/\") ? file : fs.dirnameSync?.(file) ?? \".\", fs.constants.W_OK);\n    fs.statSync(file);\n    return !fs.statSync(file).isDirectory();\n  } catch {\n    return false;\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  writeIfNotChanged(outFile, contents);\n} catch (e) {\n  if (process.platform === \"win32\" && (e as NodeJS.ErrnoException).code === \"EPERM\") {\n    fs.rmSync(outFile, { force: true });\n    writeIfNotChanged(outFile, contents); // single retry after clearing the blocker\n  } else {\n    throw e;\n  }\n}","preventionTips":["Run builds with adequate free disk space (check df before long builds).","Keep generated-output directories out of cloud-sync and antivirus scan scopes.","Never point two generators at the same output file concurrently."],"tags":["codegen","filesystem","io","build-time"],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}