Skip to content

Commit

Permalink
Merge pull request #148 from zombocom/schneems/lazy-load-constant
Browse files Browse the repository at this point in the history
Fix lazy load for default gem
  • Loading branch information
schneems committed Jun 4, 2022
2 parents ec8dd51 + 5321695 commit 622dfdd
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,5 +1,6 @@
## HEAD (unreleased)

- [Breaking] Lazy loading moved from `autoload` to manually checking for constants and requiring `dead_end/api`. To manually use any DeadEnd internals you MUST require `dead_end/api`, otherwise it will be lazy loaded on syntax error (https://github.com/zombocom/dead_end/pull/148)
- Default to highlighted output on Ruby 3.2 (https://github.com/zombocom/dead_end/pull/150)
- Debug functionality enabled by `DEBUG=1` env var is now under `DEAD_END_DEBUG=1`. Note this is not a stable interface or feature. Output content is subject to change without major version change (https://github.com/zombocom/dead_end/pull/149)
- Enable/Disable dead_end by using the `dead_end` kwarg in `detailed_message` (https://github.com/zombocom/dead_end/pull/147)
Expand Down
2 changes: 1 addition & 1 deletion dead_end.gemspec
Expand Up @@ -8,7 +8,7 @@ end

Gem::Specification.new do |spec|
spec.name = "dead_end"
spec.version = UnloadedDeadEnd::VERSION
spec.version = DeadEnd::VERSION
spec.authors = ["schneems"]
spec.email = ["richard.schneeman+foo@gmail.com"]

Expand Down
2 changes: 0 additions & 2 deletions lib/dead_end/api.rb
Expand Up @@ -9,8 +9,6 @@
require "timeout"

module DeadEnd
VERSION = UnloadedDeadEnd::VERSION

# Used to indicate a default value that cannot
# be confused with another input.
DEFAULT_VALUE = Object.new.freeze
Expand Down
15 changes: 10 additions & 5 deletions lib/dead_end/core_ext.rb
@@ -1,11 +1,8 @@
# frozen_string_literal: true

# Allow lazy loading, only load code if/when there's a syntax error
autoload :DeadEnd, "dead_end/api"

# Ruby 3.2+ has a cleaner way to hook into Ruby that doesn't use `require`
if SyntaxError.method_defined?(:detailed_message)
module DeadEndUnloaded
module DeadEnd
class MiniStringIO
def initialize(isatty: $stderr.isatty)
@string = +""
Expand All @@ -25,14 +22,16 @@ def puts(value = $/, **)
def detailed_message(highlight: true, dead_end: true, **kwargs)
return super unless dead_end

require "dead_end/api" unless defined?(DeadEnd::DEFAULT_VALUE)

message = super
file = if highlight
DeadEnd::PathnameFromMessage.new(super(highlight: false, **kwargs)).call.name
else
DeadEnd::PathnameFromMessage.new(message).call.name
end

io = DeadEndUnloaded::MiniStringIO.new
io = DeadEnd::MiniStringIO.new

if file
DeadEnd.call(
Expand Down Expand Up @@ -72,12 +71,16 @@ module Kernel
def load(file, wrap = false)
dead_end_original_load(file)
rescue SyntaxError => e
require "dead_end/api" unless defined?(DeadEnd::DEFAULT_VALUE)

DeadEnd.handle_error(e)
end

def require(file)
dead_end_original_require(file)
rescue SyntaxError => e
require "dead_end/api" unless defined?(DeadEnd::DEFAULT_VALUE)

DeadEnd.handle_error(e)
end

Expand All @@ -90,6 +93,8 @@ def require_relative(file)
dead_end_original_require File.expand_path("../#{file}", relative_from_path)
end
rescue SyntaxError => e
require "dead_end/api" unless defined?(DeadEnd::DEFAULT_VALUE)

DeadEnd.handle_error(e)
end
end
Expand Down
6 changes: 1 addition & 5 deletions lib/dead_end/version.rb
@@ -1,9 +1,5 @@
# frozen_string_literal: true

# Calling `DeadEnd::VERSION` forces an eager load due to
# an `autoload` on the `DeadEnd` constant.
#
# This is used for gemspec access in tests
module UnloadedDeadEnd
module DeadEnd
VERSION = "4.0.0"
end
24 changes: 3 additions & 21 deletions spec/integration/ruby_command_line_spec.rb
Expand Up @@ -110,28 +110,10 @@ module DeadEnd
class Dog
end
# When a constant is defined through an autoload
# then Object.autoload? will return the name of the
# require only until it has been loaded.
#
# We can use this to detect if DeadEnd internals
# have been fully loaded yet or not.
#
# Example:
#
# Object.autoload?("Cat") # => nil
# autoload :Cat, "animals/cat
# Object.autoload?("Cat") # => "animals/cat
# Object.autoload?("Cat") # => "animals/cat
#
# # Once required, `autoload?` returns falsey
# puts Cat.meow # invoke autoload
# Object.autoload?("Cat") # => nil
#
if Object.autoload?("DeadEnd")
puts "DeadEnd is NOT loaded"
else
if defined?(DeadEnd::DEFAULT_VALUE)
puts "DeadEnd is loaded"
else
puts "DeadEnd is NOT loaded"
end
EOM

Expand Down

0 comments on commit 622dfdd

Please sign in to comment.