From: "fxn (Xavier Noria) via ruby-core" Date: 2024-01-18T18:37:51+00:00 Subject: [ruby-core:116309] [Ruby master Bug#20188] `Module#const_source_location` returns wrong information when real constant was defined but autoload is still ongoing Issue #20188 has been updated by fxn (Xavier Noria). @Eregon I feel we need to recenter the discussion to consider `const_source_location` under the current logic that undoes some things if the autoload raises. To me, there is no doubt `const_source_location` in the code being autoloaded has to return the real location just defined. Because that is coherent with the rest of the local state: * The lookup finds the constant. * `defined?(Bar)` returns "constant". * IMPORTANT: `autoload?(:Bar)` returns `nil`. * `constants` includes the constant. * Etc. So, the runtime is locally consistent, except for `const_source_location`. That one is off compared to the rest. If an exception is raised, the state of the constant is kind of restored. By the same reasoning, you'd restore `const_source_location` if you will, like `autoload?` goes back to returning the string with the file name again (but you got `nil` before). ---------------------------------------- Bug #20188: `Module#const_source_location` returns wrong information when real constant was defined but autoload is still ongoing https://bugs.ruby-lang.org/issues/20188#change-106331 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal * ruby -v: ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23] * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- Ref: https://github.com/fxn/zeitwerk/issues/281 `const_source_location` keeps returning the location of the `autoload` call, even after the real constant was defined. It only starts returning the real constant location once the autoload was fully completed. ```ruby # /tmp/autoload.rb File.write("/tmp/const.rb", <<~RUBY) module Const LOCATION = Object.const_source_location(:Const) end RUBY autoload :Const, "/tmp/const" p Const::LOCATION ``` Expected Output: ```ruby ["/tmp/const.rb", 1] ``` Actual Output: ```ruby ["/tmp/autoload.rb", 8] ``` Potential patch: https://github.com/ruby/ruby/pull/9549 -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/