Danielduel
Danielduel•9mo ago

Read-only FS detection

Hi, I have few deps that by default create cache/log dirs... I can disable it, but the detection itself is unclear. Is there a better way than
const canWriteToFs = (() => {
try {
Deno.mkdirSync("./log", { recursive: true });
return true;
}
catch {
return false;
}
})();
const canWriteToFs = (() => {
try {
Deno.mkdirSync("./log", { recursive: true });
return true;
}
catch {
return false;
}
})();
2 Replies
Deno AI Helper
Deno AI Helper•9mo ago
The documentation suggests that your approach is generally correct. However, it's important to note that you should be careful with the error handling. In your current setup, you're catching all errors and assuming they mean the file system is read-only. However, other errors could occur, such as Deno.errors.PermissionDenied if you don't have the --allow-write flag, or Deno.errors.AlreadyExists if the directory already exists. Here's a more precise way to check if the file system is read-only:
const canWriteToFs = (() => {
try {
Deno.mkdirSync("./log", { recursive: true });
return true;
} catch (err) {
if (err instanceof Deno.errors.PermissionDenied) {
return false;
}
throw err; // Rethrow other errors
}
})();
const canWriteToFs = (() => {
try {
Deno.mkdirSync("./log", { recursive: true });
return true;
} catch (err) {
if (err instanceof Deno.errors.PermissionDenied) {
return false;
}
throw err; // Rethrow other errors
}
})();
This code will return false if the PermissionDenied error is thrown, indicating that the file system is read-only. If any other error is thrown, it will be rethrown, allowing you to handle it elsewhere in your code. Remember to run your script with the --unstable and --allow-write flags, as the Deno.mkdirSync function requires write permissions. 🦕
Danielduel
Danielduel•9mo ago
Actually that's smart