{"record":{"id":"5c79e45599c256e7","repo":"denoland/deno","slug":"deno-fsfile-cannot-be-constructed-use-deno-ope","errorCode":null,"errorMessage":"'Deno.FsFile' cannot be constructed, use 'Deno.open()' or 'Deno.openSync()' instead","messagePattern":"'Deno\\.FsFile' cannot be constructed, use 'Deno\\.open\\(\\)' or 'Deno\\.openSync\\(\\)' instead","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/fs/30_fs.js","lineNumber":652,"sourceCode":"    create: true,\n  });\n}\n\nclass FsFile {\n  #rid = 0;\n\n  #readable;\n  #writable;\n\n  constructor(rid, symbol) {\n    ObjectDefineProperty(this, internalRidSymbol, {\n      __proto__: null,\n      enumerable: false,\n      value: rid,\n    });\n    this.#rid = rid;\n    if (!symbol || symbol !== fsFileConstructorKey) {\n      throw new TypeError(\n        \"'Deno.FsFile' cannot be constructed, use 'Deno.open()' or 'Deno.openSync()' instead\",\n      );\n    }\n  }\n\n  write(p) {\n    return write(this.#rid, p);\n  }\n\n  writeSync(p) {\n    return writeSync(this.#rid, p);\n  }\n\n  truncate(len) {\n    return op_fs_file_truncate_async(this.#rid, coerceLen(len));\n  }\n\n  truncateSync(len) {","sourceCodeStart":634,"sourceCodeEnd":670,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/fs/30_fs.js#L634-L670","documentation":"Deno.FsFile wraps an OS file resource whose lifecycle the runtime owns. Direct construction with new Deno.FsFile(rid) is not allowed: the constructor requires an internal key (fsFileConstructorKey) that only Deno.open()/Deno.openSync() (and internal creators like the stdio singletons) pass. This guard keeps rid management under runtime control.","triggerScenarios":"new Deno.FsFile(3); new Deno.FsFile(Deno.stdin.rid); attempting to fake or subclass FsFile by calling the constructor; code written for older Deno versions where constructing with an rid was possible.","commonSituations":"Migrating legacy scripts that wrapped raw rids; test helpers trying to construct fake file objects; interop code that receives an rid from another API and wants an FsFile view.","solutions":["Use Deno.open(path, opts) / Deno.openSync(path, opts) to obtain an FsFile","For standard streams use the singletons Deno.stdin, Deno.stdout, Deno.stderr","Wrap or compose an FsFile returned by open() rather than constructing one; if another API returns an rid, use that API's own handle type"],"exampleFix":"// before\nconst f = new Deno.FsFile(3);\n\n// after\nconst f = await Deno.open(\"./data.txt\", { read: true });","handlingStrategy":"fallback","validationCode":"if (f instanceof Deno.FsFile) {\n  // already a real FsFile from open()/openSync()\n} else {\n  f = await Deno.open(path, { read: true });\n}","typeGuard":"const isFsFile = (v) => v instanceof Deno.FsFile;","tryCatchPattern":null,"preventionTips":["Obtain files only through Deno.open/Deno.openSync (or Deno.create)","Use Deno.stdin/stdout/stderr for standard streams instead of wrapping rids","Wrap or compose FsFile instances; never construct or subclass the constructor"],"tags":["fs","fsfile","constructor","rid"],"backgroundTag":"private-constructor","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"}