You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Treat a trashed folder ID used as the root as missing - #451
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 trashfs.info("1AbC...") # FileNotFoundErrorfs.ls("1AbC...") # ['1AbC.../a.txt'] before, FileNotFoundError nowlist(fs.walk("1AbC...")) # [('1AbC...', [], ['a.txt'])] before, [] nowfs.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).
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>
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.
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.
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
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).
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.
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.
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>
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:
One remaining trashed-root bypass in rm_file(root).
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().
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.
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>
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
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()andinfo()raiseFileNotFoundError, andexists()is false, for the folder and for every path under it;find()andwalk()return nothing; fsspec does that for every missing path, and other code relies on it.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 raisesFileNotFoundError. Before, it trashed the folder again, or withtrash_only=Falseremoved 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 itstrashedflag;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, andrm_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 afterinvalidate_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
invalidate_cache()and a new one must find the folder and its file missing:ls(),info()andcat_file()raise,exists()is false,find()of either andwalk()return nothing,pipe_file()into the folder raises, andrm_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 didrm_file()of the trashed folder (run 47575684540).info(),ls()andcat_file()of the file raise, andfind()of it returns nothing, without sending the listing. They fail without the fix.🤖 Generated with Claude Code