{"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":"exception","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/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/fs/30_fs.js#L789-L825","documentation":"Truncating a file requires write access. Deno validates open-option coherence up front and throws when truncate is true while write is false or absent, because an emptying operation through a read-only handle is meaningless.","triggerScenarios":"Deno.open(path, { read: true, truncate: true }); copying a read-only options object and adding truncate: true; defaults where write is false and only truncate was set.","commonSituations":"Log-rotation or rewrite logic that wants 'empty the file then write' but forgets write: true; developers assuming truncate is standalone like Deno.truncate(path).","solutions":["Add write: true alongside truncate: true","If you only need to empty the file, call Deno.truncate(path, 0) separately with a read/write open or on its own","Check dependent flag pairs inside a shared options builder"],"exampleFix":"// before\nconst f = await Deno.open(path, { read: true, truncate: true });\n\n// after\nconst f = await Deno.open(path, { read: true, write: true, truncate: true });","handlingStrategy":"validation","validationCode":"function normalizeOpenOptions(o) {\n  if (o.truncate) o = { ...o, write: true };\n  return o;\n}\n\nconst f = await Deno.open(path, normalizeOpenOptions(opts));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pair truncate: true with write: true","Use Deno.truncate(path, 0) when you only need to empty a file","Centralize option validation in one builder used by all open calls"],"tags":["fs","open","options","truncate"],"backgroundTag":"invalid-option-combination","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}