Skip to content

filter: fix directory filters for brace path alternatives - fixes #3250 - #9645

Open
giri256 wants to merge 1 commit into
rclone:masterfrom
giri256:fix-3250-brace-directory-filters
Open

giri256 wants to merge 1 commit into
rclone:masterfrom
giri256:fix-3250-brace-directory-filters

Conversation

@giri256

@giri256 giri256 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

What does this change do?

Brace filter alternatives containing path separators or recursive wildcards currently fall back to a catch-all directory rule, so a filter such as /a/{b,c/**} traverses unrelated directories.

This change expands only brace groups that can affect directory depth (/ or **) before deriving directory globs, deduplicates the resulting rules, and leaves ordinary filename alternatives unchanged. Nested braces, raw regex sections, and opaque escaped or character-class path patterns retain the existing conservative all-directory fallback.

Focused tests cover the generated directory rules and the end-to-end include/exclude behavior.

Linked issue

Fixes #3250

For new or changed backends

Not applicable.

Testing

  • gofmt -d fs/filter/glob.go fs/filter/glob_test.go fs/filter/filter_test.go produced no diff.
  • go test ./fs/filter -count=1 passed.
  • go build passed on Windows.
  • A real rclone.exe lsf -R --files-only --include '/a/{b,c/**}' reproduction listed only a/b and a/c/deep/keep.txt; the unrelated directory was pruned.
  • Broad RCLONE_CONFIG=/notfound go test ./... -count=1 verification was split after a connector transport interruption. All observed packages passed except the unrelated Windows baseline failure in backend/sftp, where TestSSHExternalWaitMultipleCalls invokes an external echo executable that is absent from C:\Program Files\Eclipse Adoptium\jdk-21.0.11.10-hotspot\bin;C:\Program Files\Eclipse Adoptium\jdk-25.0.2.10-hotspot\bin;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Windows\SYSTEM32;C:\Windows;C:\Windows\SYSTEM32\WBEM;C:\Windows\SYSTEM32\WINDOWSPOWERSHELL\V1.0\;C:\Windows\SYSTEM32\OPENSSH\;C:\Program Files\nodejs\;C:\Program Files\CMake\bin;C:\Program Files\Go\bin;C:\ProgramData\chocolatey\bin;C:\ProgramData\chocolatey\lib\mpvio.install\tools;C:\Program Files\Git\cmd;C:\Program Files\RedHat\Podman\;%LOCALAPPDATA%\Android\Sdk\platform-tools;%LOCALAPPDATA%\Android\Sdk\cmdline-tools\latest\bin;C:\Program Files\GitHub CLI\;C:\Users\raidsh\.local\bin;C:\Users\raidsh\AppData\Local\Microsoft\WindowsApps;C:\Users\raidsh\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\raidsh\.lmstudio\bin;C:\Users\raidsh\AppData\Local\Microsoft\WinGet\Packages\charmbracelet.crush_Microsoft.Winget.Source_8wekyb3d8bbwe\crush_0.1.11_Windows_x86_64;C:\Users\raidsh\AppData\Roaming\npm;C:\Users\raidsh\AppData\Local\Microsoft\WinGet\Links;C:\Users\raidsh\AppData\Local\Programs\VSCodium\bin;C:\Users\raidsh\AppData\Local\Microsoft\WinGet\Packages\yt-dlp.FFmpeg_Microsoft.Winget.Source_8wekyb3d8bbwe\ffmpeg-N-120424-g03b9180fe3-win64-gpl/bin;C:\Users\raidsh\go\bin;C:\Users\raidsh\AppData\Local\Android\Sdk\platform-tools;C:\Users\raidsh\AppData\Local\Microsoft\WinGet\Packages\Genymobile.scrcpy_Microsoft.Winget.Source_8wekyb3d8bbwe\scrcpy-win64-v4.0. The detached non-backend remainder completed, but its aggregate exit code could not be recovered after the transport session expired.
  • git diff --check passed.

Checklist

  • This change is trivial OR it has been discussed and agreed in the linked issue.
  • I have read the contribution guidelines.
  • (If I used AI tools to help write this code) I have read and understood the AI-assisted contributions guidance, and I have tested and take ownership of this change myself.
  • I have added tests for all changes in this PR if appropriate.
  • I have added documentation for the changes if appropriate (no user-facing documentation change is required).
  • All commit messages are in house style.
  • (Backend changes only) Not applicable.
  • This Pull Request is ready for review.

@ncw

ncw commented Oct 14, 2026

Copy link
Copy Markdown
Member

Thanks for this, and sorry for the slow review. I compared it with #9778, which tried to fix the same issue, and yours is the one we'll go with. It handles [...] classes and escapes inside {} groups correctly, and falling back to all directories when a class or escape could hide a / or ** is the right call.

One thing needs fixing before merge. expandPathBraceGlobs has no limit on how many globs it produces, and every path group doubles the count. Repeating {a,b/} 20 times in one glob gives 1,048,576 directory rules and takes about 3.4s, and a slightly longer --include will run out of memory. Could you cap the expansion at something small (say 64 globs) and above that fall back to /** with the same Infof message? A test for the fallback would be good too.

Smaller things:

  • braceContainsOpaquePathPattern, braceContainsPathPattern, splitBraceOptions, skipGlobClass and globToDirGlobsSingle need a one-line doc comment each.
  • In expandPathBraceGlobs the inner for loop can be expanded = append(expanded, suffixExpansions...).
  • The commit author name is "p1". Could you set your git user.name and amend the commit so the changelog credits you properly?

Thanks again!

@giri256
giri256 force-pushed the fix-3250-brace-directory-filters branch from 11b9981 to facee24 Compare October 18, 2026 01:17
@giri256

giri256 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! All addressed. I amended the single commit (now authored as Giridhar):

  • expandPathBraceGlobs is capped at maxPathBraceGlobs = 64. It bails out as soon as the running count goes over the cap, so it stops early instead of building the full set. Above the cap globToDirGlobs falls back to /** with the same Infof message.
  • TestGlobToDirGlobsExpansionLimit checks that 6 path groups still expand to exactly 64 globs, and that 7, 20 and 100 groups fall back to /**. The 100-group case finishes instantly.
  • Added doc comments to the five helpers. The expandPathBraceGlobs comment was sitting above the wrong function, so I moved it back.
  • The inner loop is now append(expanded, suffixExpansions...).

go test ./fs/filter/ and go vet pass.

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.

filter: fix directory filter globs for / and ** in {}

2 participants