Skip to content

Commit 1fa41bf

Browse files
author
schneems
committed
Remove NoMethodError integration
In favor of highlight_error gem https://bugs.ruby-lang.org/issues/18159#note-7
1 parent e972628 commit 1fa41bf

File tree

4 files changed

+5
-146
lines changed

4 files changed

+5
-146
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## HEAD (unreleased)
22

3+
## 1.2.0
4+
5+
- Remove NoMethodError patching instead use https://github.com/ruby/error_highlight/ ()
6+
37
## 1.1.7
48

59
- Fix sinatra support for `require_relative` (https://github.com/zombocom/dead_end/pull/63)

README.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,9 @@ end
8181

8282
As well as unmatched `|` and unmatched `}`. These errors can be time consuming to debug because Ruby often only tells you the last line in the file. The command `ruby -wc path/to/file.rb` can narrow it down a little bit, but this library does a better job.
8383

84-
## What other errors does it handle?
85-
86-
In addition to syntax errors, the NoMethodError is annotated to show the line where the error occured, and the surrounding context:
87-
88-
```
89-
scratch.rb:7:in `call': undefined method `upcase' for nil:NilClass (NoMethodError)
90-
91-
92-
1 class Pet
93-
6 def call
94-
❯ 7 puts "Come here #{@neam.upcase}"
95-
8 end
96-
9 end
97-
```
98-
9984
## Sounds cool, but why isn't this baked into Ruby directly?
10085

101-
I would love to get something like this directly in Ruby, but I first need to prove it's useful. The `did_you_mean` functionality started as a gem that was eventually adopted by a bunch of people and then Ruby core liked it enough that they included it in the source. The goal of this gem is to:
102-
103-
1. Get real world useage and feedback. If we gave you an awful suggestion, let us know! We try to handle lots of cases well, but maybe we could be better.
104-
2. Prove out demand. If you like this idea, then vote for it by putting it in your Gemfile.
86+
We are now talking about it https://bugs.ruby-lang.org/issues/18159#change-93682.
10587

10688
## Artificial Inteligence?
10789

lib/dead_end/auto.rb

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -53,52 +53,3 @@ def require(path)
5353
end
5454
end
5555

56-
module DeadEnd
57-
IsProduction = -> {
58-
ENV["RAILS_ENV"] == "production" || ENV["RACK_ENV"] == "production"
59-
}
60-
end
61-
62-
# Unlike a syntax error, a NoMethodError can occur hundreds or thousands of times and
63-
# chew up CPU and other resources. Since this is primarilly a "development" optimization
64-
# we can attempt to disable this behavior in a production context.
65-
if !DeadEnd::IsProduction.call
66-
class NoMethodError
67-
alias :dead_end_original_to_s :to_s
68-
69-
def to_s
70-
return super if DeadEnd::IsProduction.call
71-
72-
file, line, _ = backtrace[0].split(":")
73-
return super if !File.exist?(file)
74-
75-
index = line.to_i - 1
76-
source = File.read(file)
77-
code_lines = DeadEnd::CodeLine.parse(source)
78-
79-
block = DeadEnd::CodeBlock.new(lines: code_lines[index])
80-
lines = DeadEnd::CaptureCodeContext.new(
81-
blocks: block,
82-
code_lines: code_lines
83-
).call
84-
85-
message = super.dup
86-
message << $/
87-
message << $/
88-
89-
message << DeadEnd::DisplayCodeWithLineNumbers.new(
90-
lines: lines,
91-
highlight_lines: block.lines,
92-
terminal: self.class.to_tty?
93-
).call
94-
95-
message << $/
96-
message
97-
rescue => e
98-
puts "DeadEnd Internal error: #{e.dead_end_original_to_s}"
99-
puts "DeadEnd Internal backtrace:"
100-
puts backtrace.map {|l| " " + l }.join($/)
101-
super
102-
end
103-
end
104-
end

spec/integration/ruby_command_line_spec.rb

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -29,84 +29,6 @@ module DeadEnd
2929
end
3030
end
3131

32-
it "does not get in an infinite loop when NoMethodError is raised internally" do
33-
Dir.mktmpdir do |dir|
34-
@tmpdir = Pathname(dir)
35-
@script = @tmpdir.join("script.rb")
36-
@script.write <<~'EOM'
37-
class DeadEnd::DisplayCodeWithLineNumbers
38-
def call
39-
raise NoMethodError.new("foo")
40-
end
41-
end
42-
43-
class Pet
44-
def initialize
45-
@name = "cinco"
46-
end
47-
48-
def call
49-
puts "Come here #{@neam.upcase}"
50-
end
51-
end
52-
53-
Pet.new.call
54-
EOM
55-
56-
out = `ruby -I#{lib_dir} -rdead_end/auto #{@script} 2>&1`
57-
58-
expect(out).to include("DeadEnd Internal error: foo")
59-
expect(out).to include("DeadEnd Internal backtrace")
60-
expect($?.success?).to be_falsey
61-
end
62-
end
63-
64-
it "annotates NoMethodError" do
65-
Dir.mktmpdir do |dir|
66-
@tmpdir = Pathname(dir)
67-
@script = @tmpdir.join("script.rb")
68-
@script.write <<~'EOM'
69-
class Pet
70-
def initialize
71-
@name = "cinco"
72-
end
73-
74-
def call
75-
puts "Come here #{@neam.upcase}"
76-
end
77-
end
78-
79-
Pet.new.call
80-
EOM
81-
82-
out = `ruby -I#{lib_dir} -rdead_end/auto #{@script} 2>&1`
83-
84-
error_line = <<~'EOM'
85-
❯ 7 puts "Come here #{@neam.upcase}"
86-
EOM
87-
88-
expect(out).to include("NoMethodError")
89-
expect(out).to include(error_line)
90-
expect(out).to include(<<~'EOM')
91-
1 class Pet
92-
6 def call
93-
❯ 7 puts "Come here #{@neam.upcase}"
94-
8 end
95-
9 end
96-
EOM
97-
expect($?.success?).to be_falsey
98-
99-
# Test production check
100-
out = `RAILS_ENV=production ruby -I#{lib_dir} -rdead_end/auto #{@script} 2>&1`
101-
expect(out).to include("NoMethodError")
102-
expect(out).to_not include(error_line)
103-
104-
out = `RACK_ENV=production ruby -I#{lib_dir} -rdead_end/auto #{@script} 2>&1`
105-
expect(out).to include("NoMethodError")
106-
expect(out).to_not include(error_line)
107-
end
108-
end
109-
11032
it "detects require error and adds a message with auto mode" do
11133
Dir.mktmpdir do |dir|
11234
@tmpdir = Pathname(dir)

0 commit comments

Comments
 (0)