{"record":{"id":"cd588482f3379fa7","repo":"denoland/deno","slug":"truncate-option-requires-write-to-be-true","errorCode":null,"errorMessage":"'truncate' option requires 'write' to be true","messagePattern":"'truncate' option requires 'write' to be true","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/fs/30_fs.js","lineNumber":807,"sourceCode":"  [SymbolDispose]() {\n    core.tryClose(this.#rid);\n  }\n}\n\nfunction checkOpenOptions(options) {\n  if (\n    ArrayPrototypeFilter(\n      ObjectValues(options),\n      (val) => val === true,\n    ).length === 0\n  ) {\n    throw new Error(\n      \"'options' requires at least one option to be true\",\n    );\n  }\n\n  if (options.truncate && !options.write) {\n    throw new Error(\n      \"'truncate' option requires 'write' to be true\",\n    );\n  }\n\n  const createOrCreateNewWithoutWriteOrAppend =\n    (options.create || options.createNew) &&\n    !(options.write || options.append);\n\n  if (createOrCreateNewWithoutWriteOrAppend) {\n    throw new Error(\n      \"'create' or 'createNew' options require 'write' or 'append' to be true\",\n    );\n  }\n}\n\nfunction readFileSync(path) {\n  return op_fs_read_file_sync(pathFromURL(path));\n}","sourceCodeStart":789,"sourceCodeEnd":825,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/fs/30_fs.js#L789-L825","documentation":"Part of checkOpenOptions for Deno.open/openSync: truncation modifies file contents, which requires the file to be opened for writing. Specifying truncate: true without write: true (including the combination with append) throws a plain Error before the file is touched.","triggerScenarios":"Deno.open(path, { truncate: true }) or Deno.openSync(path, { truncate: true, append: true }) — truncate true while write is not true.","commonSituations":"Porting Node's 'w' flag piecewise and forgetting write; attempting to emulate touch/clear-file semantics; combining truncate with append expecting O_TRUNC|O_APPEND behavior.","solutions":["Add write: true: Deno.open(path, { write: true, truncate: true })","For a full Node-'w' equivalent use { write: true, create: true, truncate: true }","Do not combine truncate with append; choose one write mode"],"exampleFix":"// before\nawait Deno.open(\"/tmp/log\", { truncate: true }); // Error\n\n// after\nawait Deno.open(\"/tmp/log\", { write: true, truncate: true });","handlingStrategy":"validation","validationCode":"function normalizeOpenOptions(o) {\n  if (o.truncate) o = { ...o, write: true };\n  return o;\n}\nawait Deno.open(path, normalizeOpenOptions(options));","typeGuard":"function areTruncateOptionsValid(o: { truncate?: boolean; write?: boolean }): boolean {\n  return !o.truncate || o.write === true;\n}","tryCatchPattern":"try {\n  file = await Deno.open(path, options);\n} catch (err) {\n  if (err instanceof Error && err.message === \"'truncate' option requires 'write' to be true\") {\n    file = await Deno.open(path, { ...options, truncate: false, write: true, truncate: true });\n  } else throw err;\n}","preventionTips":["Always pair truncate: true with write: true","Model Node 'w' as { write: true, create: true, truncate: true } in one place","Do not combine truncate with append; pick one mode"],"tags":["fs","open","truncate","options"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}