Skip to content

Commit

Permalink
[fastlane] Print better errors when exception occurring in Fastfile (f…
Browse files Browse the repository at this point in the history
…astlane#21977)

* Wrap error code content in codefences

* Better context for errors in Fastfile
  • Loading branch information
AliSoftware authored Apr 17, 2024
1 parent 8699120 commit df12128
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
24 changes: 16 additions & 8 deletions fastlane/lib/fastlane/lane_manager_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,23 @@ def self.print_lane_context
end

def self.print_error_line(ex)
error_line = ex.backtrace.first
return if error_line.nil?

error_line = error_line.match("Fastfile:(\\d+):")
return unless error_line
lines = ex.backtrace_locations&.select { |loc| loc.path == 'Fastfile' }&.map(&:lineno)
return if lines.nil? || lines.empty?

fastfile_content = File.read(FastlaneCore::FastlaneFolder.fastfile_path, encoding: "utf-8")
if ex.backtrace_locations.first&.path == 'Fastfile'
# If exception happened directly in the Fastfile itself (e.g. ArgumentError)
UI.error("Error in your Fastfile at line #{lines.first}")
UI.content_error(fastfile_content, lines.first)
lines.shift
end

line = error_line[1]
UI.error("Error in your Fastfile at line #{line}")
UI.content_error(File.read(FastlaneCore::FastlaneFolder.fastfile_path, encoding: "utf-8"), line)
unless lines.empty?
# If exception happened directly in the Fastfile, also print the caller (still from the Fastfile).
# If exception happened in _fastlane_ internal code, print the line from the Fastfile that called it
UI.error("Called from Fastfile at line #{lines.first}")
UI.content_error(fastfile_content, lines.first)
end
end
end
end
4 changes: 2 additions & 2 deletions fastlane/lib/fastlane/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ def execute(lane, platform = nil, parameters = nil)
return_val = nil

path_to_use = FastlaneCore::FastlaneFolder.path || Dir.pwd
parameters ||= {}
parameters ||= {} # by default no parameters
begin
Dir.chdir(path_to_use) do # the file is located in the fastlane folder
execute_flow_block(before_all_blocks, current_platform, current_lane, parameters)
execute_flow_block(before_each_blocks, current_platform, current_lane, parameters)

return_val = lane_obj.call(parameters) # by default no parameters
return_val = lane_obj.call(parameters)

# after blocks are only called if no exception was raised before
# Call the platform specific after block and then the general one
Expand Down
2 changes: 2 additions & 0 deletions fastlane_core/lib/fastlane_core/ui/implementations/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ def content_error(content, error_line)
start_line = error_line - 2 < 1 ? 1 : error_line - 2
end_line = error_line + 2 < contents.length ? error_line + 2 : contents.length

error('```')
Range.new(start_line, end_line).each do |line|
str = line == error_line ? " => " : " "
str << line.to_s.rjust(Math.log10(end_line) + 1)
str << ":\t#{contents[line - 1]}"
error(str)
end
error('```')
end

#####################################################
Expand Down

0 comments on commit df12128

Please sign in to comment.