Why Bad resource ID occurs?
I ran the following and got
error: BadResource: Bad resource ID
on seek.
2 Replies
The error
BadResource: Bad resource ID
typically occurs when you're trying to perform an operation on a resource (like a file or a network connection) that has already been closed.
In your case, the toText(file.readable)
line is likely causing the issue. The toText
function reads from a readable stream until it's closed. In the process, it also closes the Deno.File
resource once it's done reading.
So, when you're trying to seek back to the start of the file with file.seek(0, Deno.SeekMode.Start)
, you're attempting to operate on a file that has already been closed, hence the BadResource: Bad resource ID
error.
To avoid this, you might want to avoid closing the file until you're done with all operations on it. If you need to read the file's content as text and also perform other operations like seeking, you might want to read the file into memory first, perform your operations, and then close the file.
Please note that this advice is not explicitly stated in the documentation, but is based on general file handling practices. 🦕@jeiea Can you file an issue for that on GItHub? That error is very likely pointing to a bug in deno