`await app.vault.adapter.rmdir(dirPath, false)` throws error

await app.vault.adapter.rmdir(dirPath, false) throws SystemError [ERR_FS_EISDIR]: Path is a directory: rm returned EISDIR (is a directory)

If you replace false with true it works fine.

The behavior is counter-intuitive.

It is expected that with false only empty directory can be removed and if it is not empty to throw the error something like directory is not empty

SYSTEM INFO:
Obsidian version: v1.3.5
Installer version: v1.3.5
Operating system: Windows 10 Pro 10.0.22621

I must add that the behavior is not only counter-intuitive, it actually violates the documentation

* @param recursive - If `true`, delete folders under this folder recursively, if `false` the folder needs to be empty.

I made the following workaround

const rmdirOriginal = app.vault.adapter.rmdir;
app.vault.adapter.rmdir = async function(normalizedPath, recursive) {
  if (!recursive) {
    const abstractFile = app.vault.getAbstractFileByPath(normalizedPath);
    if (abstractFile instanceof customJS.obsidian.TFolder) { // COMMENT1
      if (abstractFile.children.length > 0) {
        throw new Error(`Directory ${normalizedPath} is not empty`);
      }

      recursive = true;
    }
  }

  await rmdirOriginal.call(this, normalizedPath, recursive);
};

Note that in COMMENT1 I had to use customJS to get access to TFolder class. Unfortunately obsidian API doesn’t give an easy way to access obsidian module. Raised an issue about this before.

Alternatively, we can just check something like

    if (abstractFile?.children !== undefined) {

Thanks

Will be fixed in the next build.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.