Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoloading fails when using debug #43691

Closed
brasic opened this issue Nov 22, 2021 · 1 comment
Closed

Autoloading fails when using debug #43691

brasic opened this issue Nov 22, 2021 · 1 comment

Comments

@brasic
Copy link
Contributor

brasic commented Nov 22, 2021

Hello! I've been testing the wonderful debug gem with the GitHub monolith. I noticed an issue using it with autoloading that makes me concerned it's not fully compatible with zeitwerk: autoloading some constants fail inside a debugger session when they do not in a normal context. This looks similar to how byebug fails with zeitwerk, see fxn/zeitwerk#31 (comment) and deivid-rodriguez/byebug#564 although I imagine it might be a different underlying cause.

It's difficult to use the bug report tempate to explain this issue so I've opened https://github.com/brasic/rails-debug-bug-report to demonstrate the issue.

/cc @fxn @ko1 @jhawthorn

Steps to reproduce

  1. Start a new rails app on main (bundle exec railties/exe/rails new ../myapp --edge --dev)
  2. Add these three files in app/models: brasic/rails-debug-bug-report@7b64856
  3. open a debugger session with bin/rails runner debugger
  4. Reference Git::Ref::Collection in the debugger by typing it.
  5. (Optional) verify that the constant loads correctly in a normal console.

In case something happens to my test repo, here are the test files:

$ tree git/
git/
├── ref
│   ├── collection
│   │   └── pagination.rb
│   └── collection.rb
└── ref.rb

$ cat git/ref.rb
# frozen_string_literal: true

module Git
  class Ref
  end
end
$ cat git/ref/collection.rb
# frozen_string_literal: true

class Git::Ref::Collection
end
$ cat git/ref/collection/pagination.rb
# frozen_string_literal: true

module Git::Ref::Collection::Pagination
end

Expected behavior

All constants that can autoload outside a debugger session also load within one. In particular referencing Git::Ref::Collection in the above example when it is not loaded should autoload it.

Actual behavior

Referencing Git::Ref::Collection inside a debugger session triggers uninitialized constant Git::Ref::Collection, although the same behavior outside a debugger session works fine.

Additional details

This repro was extracted from real files in the github monolith. In case it helps, here are the zeitwerk debug logs emitted when debug and an IRB session attempt to load this constant:

debug (started from IRB):

(rdbg) Git::Ref::Collection
Zeitwerk@rails.main: module Git autovivified from directory app/models/git
Zeitwerk@rails.main: autoload set for Git::Ref, to be autovivified from app/models/git/ref
Zeitwerk@rails.main: earlier autoload for Git::Ref discarded, it is actually an explicit namespace defined in app/models/git/ref.rb
Zeitwerk@rails.main: autoload set for Git::Ref, to be loaded from app/models/git/ref.rb
Zeitwerk@rails.main: constant Git::Ref loaded from file app/models/git/ref.rb
eval error: uninitialized constant Git::Ref::Collection
  (rdbg)(irb):1: in `<main>'

IRB: (started using bin/rails console):

irb(main):001:0> Git::Ref::Collection
Zeitwerk@rails.main: module Git autovivified from directory app/models/git
Zeitwerk@rails.main: autoload set for Git::Ref, to be autovivified from app/models/git/ref
Zeitwerk@rails.main: earlier autoload for Git::Ref discarded, it is actually an explicit namespace defined in app/models/git/ref.rb
Zeitwerk@rails.main: autoload set for Git::Ref, to be loaded from app/models/git/ref.rb
Zeitwerk@rails.main: autoload set for Git::Ref::Collection, to be autovivified from app/models/git/ref/collection
Zeitwerk@rails.main: earlier autoload for Git::Ref::Collection discarded, it is actually an explicit namespace defined in app/models/git/ref/collection.rb
Zeitwerk@rails.main: autoload set for Git::Ref::Collection, to be loaded from app/models/git/ref/collection.rb
Zeitwerk@rails.main: constant Git::Ref loaded from file app/models/git/ref.rb
Zeitwerk@rails.main: autoload set for Git::Ref::Collection::Pagination, to be loaded from app/models/git/ref/collection/pagination.rb
Zeitwerk@rails.main: constant Git::Ref::Collection loaded from file app/models/git/ref/collection.rb
=> Git::Ref::Collection

System configuration

Rails version: tested on 6e16942, 6e83d06

Ruby version: tested on 3.0.2p107, 3.0.3p143 (2021-10-09 revision fe6d90fd66)

@fxn
Copy link
Member

fxn commented Nov 22, 2021

This seems unrelated to Rails. I reported it in ruby/debug.

@fxn fxn closed this as completed Nov 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants