Feature #19849
openRequiring file with autoload results in confusing error if file doesn't exist
Description
Given the following file that calls the Example
constant:
# example.rb
require_relative 'autoload_example.rb'
Example.new
and an autoload to define the Example
constant, with an unknown path:
# autoload_example.rb
autoload "Example", "path_unknown"
Running ruby example.rb
results in the following error:
<internal:~/.rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:85:in `require': cannot load such file -- path_unknown (LoadError)
from <internal:~/.rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
from example.rb:4:in `<main>'
The error is somewhat confusing as it doesn't show the location of the autoload
which caused the error.
This can be especially confusing if the autoload is called somewhere deep in a gem.
Updated by p8 (Petrik de Heus) about 1 year ago
- Tracker changed from Bug to Feature
- Backport deleted (
3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN)
Updated by Eregon (Benoit Daloze) about 1 year ago
It cannot be part of the backtrace, because that line is executed way before and not when autoloading the constant.
It could maybe be part of the error message, but that might be weird and also quite difficult, because autoloading calls require
and that might be overridden, so it's hard to pass that state from autoloading to require and know to which require call it should apply.
Updated by mame (Yusuke Endoh) about 1 year ago
Briefly discussed at the dev meeting.
We understand the problem, but doesn't find a good solution. Since the call line of autoload does not exist in the call stack when the exception occurs, it cannot be displayed as a backtrace.
It may be possible to include it in an error message or Exception#cause
. If there are specific suggestions on how to actually implement it and how to display it, it could be considered.