Skip to content

Treat a trashed folder ID used as the root as missing - #451

Merged
shcheklein merged 9 commits into
mainfrom
trashed-root-raises
Dec 5, 2026
Merged

shcheklein merged 9 commits into
mainfrom
trashed-root-raises

Conversation

@shcheklein

@shcheklein shcheklein commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

A filesystem whose root is a folder ID kept listing that folder after it was moved to the trash. info() already said it was gone.

Example

fs = GDriveFileSystem("1AbC...")      # a folder ID; the folder is in the trash
fs.info("1AbC...")                    # FileNotFoundError
fs.ls("1AbC...")                      # ['1AbC.../a.txt'] before, FileNotFoundError now
list(fs.walk("1AbC..."))              # [('1AbC...', [], ['a.txt'])] before, [] now
fs.cat_file("1AbC.../a.txt")          # b"a" before, FileNotFoundError now

A sync job that lists the folder kept seeing its files, or nothing to do, instead of an error.

The rule

A trashed folder ID is a missing path, in every call. For a missing path:

  • ls() and info() raise FileNotFoundError, and exists() is false, for the folder and for every path under it;
  • find() and walk() return nothing; fsspec does that for every missing path, and other code relies on it.
  • a write into the folder raises FileNotFoundError. Drive itself accepts new files in a trashed folder, so without the check they would land in the trash;
  • rm_file() of the folder raises FileNotFoundError. Before, it trashed the folder again, or with trash_only=False removed it from the trash for good. On a filesystem that hasn't read the root yet, this costs one metadata request before the delete.

How

The filesystem reads a folder ID root's metadata before its first listing, and again after invalidate_cache(). Some calls checked its trashed flag; ls() of the root did so only when the root was a file, and paths under the root never did. The root's metadata now refuses a trashed root itself, so every call that reads it raises, and rm_file() of the root reads it too. That includes the listing parameters, which every lookup under the root uses, and a write directly under the root. Such a write used to reach the check only when the filesystem had to fetch new IDs from Drive, so after invalidate_cache() a filesystem that had written before uploaded into the trashed folder.

Checking the children instead does not work on real Drive. Right after a folder is trashed, its children still list for a while as not trashed. The first version of this PR did that, and the live tests caught it.

A filesystem that had already listed the folder before another client trashed it keeps using its cache. The guide's rule for other changes by other clients applies: call fs.invalidate_cache(), after which the folder is missing in every call.

Validation

  • Live, on real Drive, one test with a folder that holds one file. A filesystem lists the folder, another client trashes it, and then both that filesystem after invalidate_cache() and a new one must find the folder and its file missing: ls(), info() and cat_file() raise, exists() is false, find() of either and walk() return nothing, pipe_file() into the folder raises, and rm_file() of the folder raises. The first filesystem writes a file before the trash, so it holds IDs Drive generated for later files; on earlier heads its write after the trash went through (run 47573937891), and so did rm_file() of the trashed folder (run 47575684540).
  • Offline, the window Drive cannot be made to show on demand: the root is trashed while Drive still lists its file. info(), ls() and cat_file() of the file raise, and find() of it returns nothing, without sending the listing. They fail without the fix.
  • The offline suite passes with the current google-auth and with 2.15.0 (540 passed).

🤖 Generated with Claude Code

shcheklein and others added 2 commits December 2, 2026 19:12
A filesystem on a folder ID listed a trashed folder as empty, while
info() raised FileNotFoundError. ls() now raises too: an empty listing
of the root checks that the folder is not trashed. find() and walk()
return nothing, as for any missing path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On real Drive the children of a folder that was just trashed still
list for a while, so an empty listing did not show that the root was
trashed. ls() of a folder ID root now raises when the root's metadata
says it is trashed, as info() does. A filesystem that listed the
folder before learns of the change when its cache is invalidated, as
the guide says for other changes made by other clients.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI generated — Copilot]

5-pass review summary: fixes ls(folder_id) on a trashed folder ID root, but subpaths under a trashed folder ID still bypass the check via _list_params, and the trashed check is now duplicated across 5 call sites.

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved
shcheklein and others added 2 commits December 3, 2026 11:05
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

When the root is trashed, fs.find("FolderID/a.txt") on a new filesystem or after invalidate_cache() raises FileNotFoundError instead of returning [] (or {} with detail=True). Descendant resolution fails before find() reaches its missing-path handlers.

1 open issue
What changed in this PR

This PR makes trashed folder-ID roots behave as missing paths in the Google Drive filesystem, including lookups beneath them.

Changes:

  • Centralizes trash checks and applies them before listing.
  • Adds live coverage for fresh filesystems and cache invalidation.
  • Adds offline coverage for children that Drive still lists after trashing.
File Description
pydrive2/​test/​test_fs.py Adds live regression coverage for trashed roots and children.
pydrive2/​test/​test_fs_unit.py Tests descendant rejection before listing requests.
pydrive2/​fs/​spec.py Shares and extends root metadata trash checks.

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

For a named mount such as GDriveFileSystem("FolderID/sub", ...), find("FolderID/sub/a.txt") should return an empty result when FolderID is trashed, but it still raises because _resolver() resolves self.base before entering the exception handler. This affects both fresh instances and instances whose cache has been invalidated.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Handle trashed mount resolution errors in find

pydrive2/​fs/​spec.py:1162

For a named mount such as GDriveFileSystem("FolderID/sub", ...), find("FolderID/sub/a.txt") still raises when FolderID is trashed. _resolver() eagerly resolves self.base, reaching the new trash check before this try block, both on a fresh instance and after cache invalidation. Move resolver creation inside the handler too, so these searches return [] (or {} with detail=True).

1 resolved since last review

Review effort: Balanced

@shcheklein

Copy link
Copy Markdown
Member Author

AI generated · Claude · Re Copilot's "Handle trashed mount resolution errors in find" (review on eda256b): keeping it as it is.

With a mount such as GDriveFileSystem("FolderID/sub"), a trashed FolderID means the mount itself is missing. find already raises FileNotFoundError for a missing mount: on main, _resolver() raises it when sub is deleted, before any find handling. This PR makes the trashed case match that, and it now stops after the one metadata request without listing the trashed folder. I checked both cases offline against eda256b: each raises FileNotFoundError.

Moving _resolver() inside the handler would also make find return [] for a mount whose base path was deleted. That would change existing behaviour for missing mounts, which is outside this PR. find returns empty only when the mount resolves and the searched path under it is missing.

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI generated — Copilot]

The read, lookup, ls, info, exists, find, walk, mkdir, and rmdir paths all look solid (including keeping _resolver() outside find()'s FileNotFoundError handler so missing mounts keep their existing behavior). One subtle gap left on writes directly under a trashed folder ID root after invalidate_cache() — noted inline.

Comment thread pydrive2/fs/spec.py Resolved
Drive accepts new files in a trashed folder. A write directly under the
root reached the trash check only when it had to fetch new IDs, so after
invalidate_cache() a filesystem that had written before uploaded into
the trashed folder, while a new filesystem raised.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI generated — Copilot]

Adversarial + simplification round on 4a6f53f: direct-child writes after invalidate_cache() look cleanly fixed. Two inline notes:

  1. One remaining trashed-root bypass in rm_file(root).
  2. A simplification that folds the trashed check (and optional 404->PermissionError mapping) into _get_root_metadata() itself, removing _checked_root_metadata, _refuse_trashed, and the duplicated try/except in ls().

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

For a filesystem mounted at FolderID/sub, searches of that mount or its descendants raise FileNotFoundError instead of returning an empty result when FolderID is trashed. This occurs on a new filesystem or after invalidate_cache(), because the base is resolved before entering the exception handler.

1 open issue

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved
shcheklein and others added 3 commits December 5, 2026 14:15
Every caller refused a trashed root, so the root metadata refuses it
itself and takes the error to raise for a 404 root. The helper and the
six call-site checks go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rm_file of the root went straight to the delete, so on a trashed root it
trashed it again or removed it from the trash for good. It now reads the
root like the other calls, one request on a filesystem that has not read
the root yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shcheklein
shcheklein marked this pull request as ready for review December 5, 2026 23:10
@shcheklein shcheklein self-assigned this Dec 5, 2026
@shcheklein
shcheklein requested a balanced review from Copilot December 5, 2026 23:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No actionable issues were found in the reviewed changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@shcheklein
shcheklein merged commit dddfb64 into main Dec 5, 2026
25 checks passed
@shcheklein
shcheklein deleted the trashed-root-raises branch December 5, 2026 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants