From 52820c82102d8736290107094b2a2f986e5a9513 Mon Sep 17 00:00:00 2001 From: Olivier Halligon Date: Wed, 17 Apr 2024 15:27:08 +0200 Subject: [PATCH 1/2] Wrap error code content in codefences --- fastlane_core/lib/fastlane_core/ui/implementations/shell.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fastlane_core/lib/fastlane_core/ui/implementations/shell.rb b/fastlane_core/lib/fastlane_core/ui/implementations/shell.rb index 87f448a06ea..19fa1e48671 100644 --- a/fastlane_core/lib/fastlane_core/ui/implementations/shell.rb +++ b/fastlane_core/lib/fastlane_core/ui/implementations/shell.rb @@ -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 ##################################################### From 537410b12c85f875789fc47e3366236c3e555199 Mon Sep 17 00:00:00 2001 From: Olivier Halligon Date: Wed, 17 Apr 2024 13:34:56 +0200 Subject: [PATCH 2/2] Better context for errors in Fastfile --- fastlane/lib/fastlane/lane_manager_base.rb | 24 ++++++++++++++-------- fastlane/lib/fastlane/runner.rb | 4 ++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/fastlane/lib/fastlane/lane_manager_base.rb b/fastlane/lib/fastlane/lane_manager_base.rb index 5fe6f9b7b89..fa1859f27e1 100644 --- a/fastlane/lib/fastlane/lane_manager_base.rb +++ b/fastlane/lib/fastlane/lane_manager_base.rb @@ -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 diff --git a/fastlane/lib/fastlane/runner.rb b/fastlane/lib/fastlane/runner.rb index 88d755281b8..d711bbd9cef 100644 --- a/fastlane/lib/fastlane/runner.rb +++ b/fastlane/lib/fastlane/runner.rb @@ -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