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

Autoload with Zeitwerk #19

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions debug.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ Gem::Specification.new do |spec|

spec.add_dependency "irb", ">= 1.5.0" # for binding.irb(show_code: false)
spec.add_dependency "reline", ">= 0.3.1"
spec.add_development_dependency "zeitwerk", "~> 2.6.9"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imma be real, I have no idea what the difference is between this, and gemspec.add_development_dependency 🫥

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it just depends on how the variable from Gem::Specification.new do |spec| is named. In most cases it seems to be named spec?

end
7 changes: 5 additions & 2 deletions lib/debug/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ def update conf
end

if_updated old_conf, conf, :postmortem do |_, new_p|
if defined?(SESSION)
# puts("const_defined? #{ DEBUGGER__.const_defined?(:SESSION)}, defined? #{defined?(SESSION)}")
# if defined?(SESSION)
if DEBUGGER__.const_defined?(:SESSION)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand why, but defined?(SESSION) and DEBUGGER__.const_defined?(:SESSION) behave differently.

Without changing this, the if was being entered, SESSION was attempted to be loaded, and then not found because it's defined dynamically.

SESSION.postmortem = new_p
end
end
Expand All @@ -152,7 +154,8 @@ def update conf
end

if_updated old_conf, conf, :no_sigint_hook do |old, new|
if defined?(SESSION)
# if defined?(SESSION)
if DEBUGGER__.const_defined?(:SESSION)
SESSION.set_no_sigint_hook old, new
end
end
Expand Down
50 changes: 50 additions & 0 deletions lib/debug/open.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,56 @@
# If RUBY_DEBUG_PORT envval is provided (digits), open TCP/IP port.
# Otherwise, UNIX domain socket is used.
#
require "awesome_print"
require "pry"
require "zeitwerk"

module DEBUGGER__
# Define these dummy constants so we silence errors like:
# expected file lib/debug/local.rb to define constant DEBUGGER__::Local, but didn't (Zeitwerk::NameError)
module Local; end
module Server; end
module ServerCdp; end
module ServerDap; end
module Version; end

# List special cases where constants are defined in files where they wouldn't otherwise be found
autoload(:CONFIG, "#{__dir__}/config.rb")
autoload(:SESSION, "#{__dir__}/config.rb")
# autoload(:NaiveString, "#{__dir__}/server_dap.rb")
autoload(:SkipPathHelper, "#{__dir__}/thread_client.rb")
autoload(:UI_Base, "#{__dir__}/session.rb")
end

loader = Zeitwerk::Loader.new
loader.tag = File.basename(__FILE__, ".rb")
loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
# loader.push_dir(__dir__)

loader.ignore(
"lib/debug/start.rb",
"lib/debug/open.rb",
"lib/debug/open_nonstop.rb",
"lib/debug/prelude.rb",
"lib/debug/dap_custom/traceInspector.rb"
)

# Configure a special-case for this mangled module name
loader.push_dir(__dir__, namespace: DEBUGGER__)
# loader.inflector.inflect(
# "debug" => "DEBUGGER__"
# )

loader.enable_reloading
loader.setup
loader.log!
puts "Zeitwerk configured!"

# ap({ roots: loader.send(:roots), ignored_paths: loader.send(:ignored_paths), all_paths: Zeitwerk::Loader.all_dirs })
# loader.eager_load
# puts("loaded constants under DEBUGGER__:")
# ap(DEBUGGER__.constants)
# exit(0)

require_relative 'session'
return unless defined?(DEBUGGER__)
Expand Down
6 changes: 3 additions & 3 deletions lib/debug/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2212,7 +2212,7 @@ def self.open_tcp host: nil, port:, nonstop: false, **kw
CONFIG.set_config(**kw)
require_relative 'server'

if defined? SESSION
if DEBUGGER__.const_defined?(:SESSION)
SESSION.reset_ui UI_TcpServer.new(host: host, port: port)
else
initialize_session{ UI_TcpServer.new(host: host, port: port) }
Expand All @@ -2225,7 +2225,7 @@ def self.open_unix sock_path: nil, sock_dir: nil, nonstop: false, **kw
CONFIG.set_config(**kw)
require_relative 'server'

if defined? SESSION
if DEBUGGER__::const_defined?(:SESSION)
SESSION.reset_ui UI_UnixDomainServer.new(sock_dir: sock_dir, sock_path: sock_path)
else
initialize_session{ UI_UnixDomainServer.new(sock_dir: sock_dir, sock_path: sock_path) }
Expand Down Expand Up @@ -2376,7 +2376,7 @@ def self.log level, msg
@logfile = STDERR unless defined? @logfile
return if @logfile.closed?

if defined? SESSION
if DEBUGGER__::const_defined?(:SESSION)
pi = SESSION.process_info
process_info = pi ? "[#{pi}]" : nil
end
Expand Down
Loading