Skip to content

Dir.glob and Dir[] on existing file URL raises error #8789

@keithrbennett

Description

@keithrbennett

Environment Information

➜ ~ ruby -v
jruby 9.4.12.0 (3.1.4) 2025-02-11 f4ab750 Java HotSpot(TM) 64-Bit Server VM 23.0.1+11-39 on 23.0.1+11-39 +jit [arm64-darwin]
➜ ~ uname -a
Darwin Keiths-MacBook-Air.local 24.4.0 Darwin Kernel Version 24.4.0: Wed Mar 19 21:18:03 PDT 2025; root:xnu-11417.101.15~1/RELEASE_ARM64_T8112 arm64

This is not a web app, I am developing the rika gem.

Expected Behavior

In Ruby 3.4.2 irb:

3.4.2 :001 > Dir.glob 'file:///Users/kbennett/code/rika/x.pdf'
 => []

This returns an empty array in Ruby 3.4.2, and it should do so in JRuby as well.

Actual Behavior

In JRuby irb:

jruby-9.4.12.0 :002 > Dir.glob 'file:///Users/kbennett/code/rika/x.pdf'
java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55): Range [-7, 31) out of bounds for length 31 (Java::JavaLang::StringIndexOutOfBoundsException)
	from java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
	from java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
	from java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
         ...

Here are some more examples, indicating that the error only occurs when it is a file URL and the file exists; and that the error is not related to string length:

jruby-9.4.12.0 :025 > 'file:///Users/kbennett/code/rika/x.pdf'.size
 => 38
jruby-9.4.12.0 :026 > Dir.glob 'a' * 38
 => []
jruby-9.4.12.0 :027 > Dir.glob '/' * 38
 => ["//////////////////////////////////////"]
jruby-9.4.12.0 :028 > Dir.glob ':' * 38
 => []
jruby-9.4.12.0 :029 > Dir.glob ':/' * 19
 => []
jruby-9.4.12.0 :030 > Dir.glob 'file:///'
 => ["file:///"]
jruby-9.4.12.0 :031 > Dir.glob 'file:///Users'
 => ["file:///Users"]
jruby-9.4.12.0 :032 > Dir.glob 'file:///Users/kbennett'
 => ["file:///Users/kbennett"]
jruby-9.4.12.0 :033 > Dir.glob 'file:///Users/kbennett/code'
 => ["file:///Users/kbennett/code"]
jruby-9.4.12.0 :034 > Dir.glob 'file:///Users/kbennett/code/x.pdf' # file does not exist here
 => []
jruby-9.4.12.0 :035 > Dir.glob 'file:///Users/kbennett/code/rika/x.pdf' # file *does* exist here
java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55): Range [-7, 31) out of bounds for length 31 (Java::JavaLang::StringIndexOutOfBoundsException)
	from java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
	from java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
   ...

jruby-9.4.12.0 :038 > Dir.glob('file:///Users/a-very-very-very-very-very-very-very-very-very-very-long-dir-name/x.pdf')
 => []
jruby-9.4.12.0 :040 > Dir.glob('http://www.example.com')
 => []

The error occurs with Dir[] as well:

jruby-9.4.12.0 :041 > Dir['file:///Users/kbennett/code/rika/x.pdf'] # file *does* exist here
java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55): Range [-7, 31) out of bounds for length 31 (Java::JavaLang::StringIndexOutOfBoundsException)
	from java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
    ...

By the way, I am aware that these Dir methods are not intended to be used with URL's. However, I have lists that contain both URL's and filespecs and was hoping to pass all of them to the Dir method and ignore the empty array values returned for the URL's.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions