Project

General

Profile

Actions

Feature #17548

closed

Need simple way to include symlink directories in Dir.glob

Feature #17548: Need simple way to include symlink directories in Dir.glob

Added by keithrbennett (Keith Bennett) over 5 years ago. Updated 10 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:102118]

Description

I would like to suggest that Ruby provide a simple way to glob that includes the content of symlinked directories.

I have my Ruby projects in a ~/work directory that is symlinked to a directory an another partition.

For example, ~ is on /dev/sda1, whereas ~/work is a link to a directory on /dev/sda2. I need this because I want to operate on the same code bases regardless of which partition I boot from (they are various Linux distros).

The array returned by Dir.glob(Dir.home, '**', '*rb') does not include any files in ~/work. The size of that array is only 84. In contrast, when I use find ~ -name "*rb" | wc -l to get the number of files, I get 87,229 files.

I was hoping that one of the flags that can be passed to glob would help, but the only relevant one I found was to not follow links, which is the opposite of what I wanted.

There are arcane workarounds using fancy glob patterns, but I believe it's important that the language provide a simple way to accomplish this. (For example, the Unix find command has an -L option for this.) I understand that it may not be possible to retrofit it into the existing functions (glob and []), but even providing a different method (e.g. one named glob_include_links) would be ok. Or perhaps a glob2 method could be added that would include simple ways to specify that files in hidden directories should be included, in addition to the option to follow symlinks.

I admit that I have no idea how much effort this would be to implement, especially regarding Windows compatibility, but this would be nice to have.

Updated by keithrbennett (Keith Bennett) over 5 years ago Actions #1 [ruby-core:102124]

A clarifying comment that corrects something in my original post but does not change my recommendation:

I wish there a way for me to correct my original post, but I don't see a way to do that. I wanted to delete this text:

"The size of that array is only 84. In contrast, when I use find ~ -name "*rb" | wc -l to get the number of files, I get 87,229 files."

I mistakenly believed that find's default behavior was to include symlinked directories, but that difference was instead due to the fact that it includes hidden directories by default. Specifically, the ~/.rvm directory.

The correct way to compare was to use find's -L option, which would have includes the symlinked directory and reports a greater number of files than the Dir.glob number.

Updated by keithrbennett (Keith Bennett) over 5 years ago Actions #2 [ruby-core:102125]

By the way, this is what I was referring to when I mentioned "arcane workarounds":

https://stackoverflow.com/questions/357754/can-i-traverse-symlinked-directories-in-ruby-with-a-glob

(Dir.glob("**{,/*/**}/*.rb"))

Updated by Dan0042 (Daniel DeLorme) over 5 years ago Actions #3 [ruby-core:102240]

For your specific case, a workaround might be to use a bind mount instead of symlink.

But in general I agree having a follow_symlink option would be nice to have, either in Dir.glob or in Find.find

Updated by Dan0042 (Daniel DeLorme) over 4 years ago Actions #5 [ruby-core:107999]

Bump.
Today I just came across a case where I wanted to use this feature.
@nobu (Nobuyoshi Nakada) according to dev meeting notes you said "will commit", which implies to me this was already coded? Not sure.

Updated by nobu (Nobuyoshi Nakada) 10 days ago 1Actions #6

  • Status changed from Open to Closed

Applied in changeset git|ea2836f7ec905e4963b49ffdc550cdf3e82ad8c9.


[Feature #17548] Allow Dir.glob to follow symlinks with ***

Allow recursive matching through directory symlinks while stopping
at links to ancestor directories to avoid cycles.

Actions

Also available in: PDF Atom