Project

General

Profile

Actions

Feature #17548

open

Need simple way to include symlink directories in Dir.glob

Added by keithrbennett (Keith Bennett) 4 months ago. Updated about 2 months ago.

Status:
Open
Priority:
Normal
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) 4 months ago

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) 4 months ago

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) 3 months ago

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

Actions

Also available in: Atom PDF