Project

General

Profile

Actions

Bug #18624

closed

`const_source_location` returns [false, 0] when autoload is defined for the constant

Added by pocke (Masataka Kuwabara) over 2 years ago. Updated over 2 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.2.0dev (2022-03-11T08:38:13Z master 2e4516be26) [x86_64-linux]
[ruby-core:107846]

Description

const_source_location returns [false, 0] unexpectedly with the following code.

# test.rb
path = File.join(__dir__, 'test2')
Object.autoload 'Test2', path
require path

p Object.const_source_location 'Test2'
# test2.rb
class Test2
end
$ ruby -v test.rb
ruby 3.2.0dev (2022-03-11T08:38:13Z master 2e4516be26) [x86_64-linux]
[false, 0]

I expect it returns ['/path/to/test2.rb', 2], but it doesn't.
Ruby 2.7 ~ 3.1 have the same behavior.

I actually encountered this problem in a Rails app with Zeitwerk. The following codes are a reproducing example with Zeitwerk.
loader.setup calls autoload, so it causes the same problem.

# test.rb
require "zeitwerk"
loader = Zeitwerk::Loader.for_gem
loader.setup

require File.join(__dir__, 'test2')

p Zeitwerk::VERSION
p Object.const_source_location 'Test2'
# test2.rb
class Test2
end
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0