Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fastlane] Print better errors when exception occurring in Fastfile #21977

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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