Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dir_tree() not listing files inside a directory inside path when glob or regexp is used #398

Open
dhersz opened this issue Oct 10, 2022 · 1 comment
Labels
bug an unexpected problem or unintended behavior

Comments

@dhersz
Copy link

dhersz commented Oct 10, 2022

Hi. I'm having a issue where I'm trying to list some zip files inside a directory in my project using dir_tree() and they're not showing up. I thought I was messing up with glob or regexp, but it doesn't seem to be the case, since dir_ls() works fine. Example below:

library(fs)

dir_path <- tempfile("tmpdir")
dir.create(dir_path)

nested_dir <- file.path(dir_path, "nested_dir")
dir.create(nested_dir)

dir_tree(dir_path)
#> /tmp/RtmpCR1OTJ/tmpdir8bdf67c149e7
#> └── nested_dir

file.create(file.path(nested_dir, "a.zip"))
#> [1] TRUE
file.create(file.path(nested_dir, "b.zip"))
#> [1] TRUE

dir_tree(dir_path, recurse = TRUE, glob = "*.zip")
#> /tmp/RtmpCR1OTJ/tmpdir8bdf67c149e7

dir_ls(dir_path, recurse = TRUE, glob = "*.zip")
#> /tmp/RtmpCR1OTJ/tmpdir8bdf67c149e7/nested_dir/a.zip
#> /tmp/RtmpCR1OTJ/tmpdir8bdf67c149e7/nested_dir/b.zip

dir_tree(dir_path)
#> /tmp/RtmpCR1OTJ/tmpdir8bdf67c149e7
#> └── nested_dir
#>     ├── a.zip
#>     └── b.zip

You can see that both a.zip and b.zip are listed using dir_ls(), but this is not the case when using dir_tree(). I'd hope that the output of the first dir_tree() attempt would look like the second one, but that's not the case. I tried many different patterns ("nested_dir/a.zip", "*/*.zip", ...), but none seemed to work.

Not sure if this is intended behaviour or not, but I'd imagine that both dir_tree() and dir_ls() would output the same file list. Cheers!

@olivroy
Copy link
Contributor

olivroy commented May 16, 2023

I have the same issue!

@gaborcsardi gaborcsardi added the bug an unexpected problem or unintended behavior label Nov 1, 2023
JoshOBrien added a commit to JoshOBrien/fs that referenced this issue Feb 5, 2024
This PR addresses the issue described in r-lib#398. `dir_tree()` didn't work when a pattern was supplied to `regexp=` or `glob=` arguments that get passed on to `dir_ls()`. That's because the list of file paths returned by those calls to `dir_ls()` did not include paths of the parent (and grandparent etc.) directories that `dir_tree()` uses to construct the tree.

The code I've added uses the file paths returned by `dir_ls()` to recreate the directory paths that'll be needed to build the tree.

To see that this now works, try for example:

```r
dir_tree(system.file(package = "fs"), regexp = "[.]rds$")
dir_tree(system.file(package = "fs"), type = "directory")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants