Skip to content

Commit ab8ea48

Browse files
Raise errors on correct line with annotations enabled
Co-authored-by: Aaron Patterson <tenderlove@github.com>
1 parent dee5d7d commit ab8ea48

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

actionview/lib/action_view/template.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,10 @@ def #{method_name}(local_assigns, output_buffer)
334334
raise WrongEncodingError.new(source, Encoding.default_internal)
335335
end
336336

337+
start_line = @handler.respond_to?(:start_line) ? @handler.start_line(self) : 0
338+
337339
begin
338-
mod.module_eval(source, identifier, 0)
340+
mod.module_eval(source, identifier, start_line)
339341
rescue SyntaxError
340342
# Account for when code in the template is not syntactically valid; e.g. if we're using
341343
# ERB and the user writes <%= foo( %>, attempting to call a helper `foo` and interpolate

actionview/lib/action_view/template/handlers/erb.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ def handles_encoding?
4040
true
4141
end
4242

43+
# Line number to pass to #module_eval
44+
#
45+
# If we're annotating the template, we need to offset the starting
46+
# line number passed to #module_eval so that errors in the template
47+
# will be raised on the correct line.
48+
def start_line(template)
49+
annotate?(template) ? -1 : 0
50+
end
51+
4352
def call(template, source)
4453
# First, convert to BINARY, so in case the encoding is
4554
# wrong, we can still find an encoding tag
@@ -60,8 +69,7 @@ def call(template, source)
6069
trim: (self.class.erb_trim_mode == "-")
6170
}
6271

63-
# Annotate output with template file names, if we're rendering HTML
64-
if ActionView::Base.annotate_template_file_names && template.format == :html
72+
if annotate?(template)
6573
options[:preamble] = "@output_buffer.safe_append='<!-- BEGIN #{template.short_identifier} -->\n';"
6674
options[:postamble] = "@output_buffer.safe_append='<!-- END #{template.short_identifier} -->\n';@output_buffer.to_s"
6775
end
@@ -70,6 +78,10 @@ def call(template, source)
7078
end
7179

7280
private
81+
def annotate?(template)
82+
ActionView::Base.annotate_template_file_names && template.format == :html
83+
end
84+
7385
def valid_encoding(string, encoding)
7486
# If a magic encoding comment was found, tag the
7587
# String with this encoding. This is for a case

actionview/test/actionpack/controller/render_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,4 +1482,18 @@ def test_template_annotations_do_not_render_for_non_html_format
14821482
ensure
14831483
ActionView::Base.annotate_template_file_names = false
14841484
end
1485+
1486+
def test_line_offset_with_annotations_enabled
1487+
ActionView::Base.annotate_template_file_names = true
1488+
1489+
exc = assert_raises ActionView::Template::Error do
1490+
get :render_line_offset
1491+
end
1492+
line = exc.backtrace.first
1493+
assert(line =~ %r{:(\d+):})
1494+
assert_equal "1", $1,
1495+
"The line offset is wrong, perhaps the wrong exception has been raised, exception was: #{exc.inspect}"
1496+
ensure
1497+
ActionView::Base.annotate_template_file_names = false
1498+
end
14851499
end

0 commit comments

Comments
 (0)