From 8daf815044093b21c77ababa78510432a2da05ea Mon Sep 17 00:00:00 2001 From: Benoit de Chezelles Date: Wed, 31 May 2017 21:56:02 +0200 Subject: [PATCH] Print build errors to stderr Remove unnecessary I/O flush, it's already done in the `main`. Note: The notation `command 2>&1 >/dev/null` will close stdout, then redirect stderr to (a new) stdout. --- spec/compiler/crystal/tools/init_spec.cr | 10 +++--- src/compiler/crystal/command.cr | 12 +++---- src/compiler/crystal/command/format.cr | 32 ++++++++----------- src/compiler/crystal/compiler.cr | 12 +++---- src/compiler/crystal/tools/init.cr | 10 +++--- src/compiler/crystal/tools/print_hierarchy.cr | 1 - 6 files changed, 36 insertions(+), 41 deletions(-) diff --git a/spec/compiler/crystal/tools/init_spec.cr b/spec/compiler/crystal/tools/init_spec.cr index 6a986255dbd9..9fa5f5d78876 100644 --- a/spec/compiler/crystal/tools/init_spec.cr +++ b/spec/compiler/crystal/tools/init_spec.cr @@ -163,7 +163,7 @@ end it "prints error if a directory already present" do Dir.mkdir_p("#{__DIR__}/tmp") - `bin/crystal init lib "#{__DIR__}/tmp" 2>/dev/null`.should contain("file or directory #{__DIR__}/tmp already exists") + `bin/crystal init lib "#{__DIR__}/tmp" 2>&1 >/dev/null`.should contain("file or directory #{__DIR__}/tmp already exists") `rm -rf #{__DIR__}/tmp` end @@ -171,7 +171,7 @@ end it "prints error if a file already present" do File.open("#{__DIR__}/tmp", "w") - `bin/crystal init lib "#{__DIR__}/tmp" 2>/dev/null`.should contain("file or directory #{__DIR__}/tmp already exists") + `bin/crystal init lib "#{__DIR__}/tmp" 2>&1 >/dev/null`.should contain("file or directory #{__DIR__}/tmp already exists") File.delete("#{__DIR__}/tmp") end @@ -179,11 +179,11 @@ end it "honors the custom set directory name" do Dir.mkdir_p("tmp") - `bin/crystal init lib tmp 2>/dev/null`.should contain("file or directory tmp already exists") + `bin/crystal init lib tmp 2>&1 >/dev/null`.should contain("file or directory tmp already exists") - `bin/crystal init lib tmp "#{__DIR__}/fresh-new-tmp" 2>/dev/null`.should_not contain("file or directory tmp already exists") + `bin/crystal init lib tmp "#{__DIR__}/fresh-new-tmp" 2>&1 >/dev/null`.should_not contain("file or directory tmp already exists") - `bin/crystal init lib tmp "#{__DIR__}/fresh-new-tmp" 2>/dev/null`.should contain("file or directory #{__DIR__}/fresh-new-tmp already exists") + `bin/crystal init lib tmp "#{__DIR__}/fresh-new-tmp" 2>&1 >/dev/null`.should contain("file or directory #{__DIR__}/fresh-new-tmp already exists") `rm -rf tmp #{__DIR__}/fresh-new-tmp` end diff --git a/src/compiler/crystal/command.cr b/src/compiler/crystal/command.cr index 3edf9eaf245a..80a6a55b308b 100644 --- a/src/compiler/crystal/command.cr +++ b/src/compiler/crystal/command.cr @@ -116,19 +116,19 @@ class Crystal::Command rescue ex : Crystal::Exception ex.color = @color if @config.try(&.output_format) == "json" - puts ex.to_json + STDERR.puts ex.to_json else - puts ex + STDERR.puts ex end exit 1 rescue ex : OptionParser::Exception error ex.message rescue ex - puts ex + STDERR.puts ex ex.backtrace.each do |frame| - puts frame + STDERR.puts frame end - puts + STDERR.puts error "you've found a bug in the Crystal compiler. Please open an issue, including source code that will allow us to reproduce the bug: https://github.com/crystal-lang/crystal/issues" end @@ -422,7 +422,7 @@ class Crystal::Command end if filenames.size == 0 || (cursor_command && cursor_location.nil?) - puts option_parser + STDERR.puts option_parser exit 1 end diff --git a/src/compiler/crystal/command/format.cr b/src/compiler/crystal/command/format.cr index 7c942e16bd4a..611118b65b30 100644 --- a/src/compiler/crystal/command/format.cr +++ b/src/compiler/crystal/command/format.cr @@ -84,23 +84,21 @@ class Crystal::Command exit(result == source ? 0 : 1) if check_files print result - STDOUT.flush rescue ex : InvalidByteSequenceError - print "Error: ".colorize.toggle(@color).red.bold - print "source is not a valid Crystal source file: ".colorize.toggle(@color).bold - puts ex.message + STDERR.print "Error: ".colorize.toggle(@color).red.bold + STDERR.print "source is not a valid Crystal source file: ".colorize.toggle(@color).bold + STDERR.puts ex.message exit 1 rescue ex : Crystal::SyntaxException if @format == "json" - puts ex.to_json + STDERR.puts ex.to_json else - puts ex + STDERR.puts ex end exit 1 rescue ex couldnt_format "STDIN" STDERR.puts - STDERR.flush exit 1 end end @@ -114,21 +112,20 @@ class Crystal::Command File.write(filename, result) rescue ex : InvalidByteSequenceError - print "Error: ".colorize.toggle(@color).red.bold - print "file '#{Crystal.relative_filename(filename)}' is not a valid Crystal source file: ".colorize.toggle(@color).bold - puts ex.message + STDERR.print "Error: ".colorize.toggle(@color).red.bold + STDERR.print "file '#{Crystal.relative_filename(filename)}' is not a valid Crystal source file: ".colorize.toggle(@color).bold + STDERR.puts ex.message exit 1 rescue ex : Crystal::SyntaxException if @format == "json" - puts ex.to_json + STDERR.puts ex.to_json else - puts ex + STDERR.puts ex end exit 1 rescue ex couldnt_format "'#{filename}'" STDERR.puts - STDERR.flush exit 1 end end @@ -168,15 +165,15 @@ class Crystal::Command if check_files check_files << FormatResult.new(filename, FormatResult::Code::INVALID_BYTE_SEQUENCE) else - print "Error: ".colorize.toggle(@color).red.bold - print "file '#{Crystal.relative_filename(filename)}' is not a valid Crystal source file: ".colorize.toggle(@color).bold - puts ex.message + STDERR.print "Error: ".colorize.toggle(@color).red.bold + STDERR.print "file '#{Crystal.relative_filename(filename)}' is not a valid Crystal source file: ".colorize.toggle(@color).bold + STDERR.puts ex.message end rescue ex : Crystal::SyntaxException if check_files check_files << FormatResult.new(filename, FormatResult::Code::SYNTAX) else - STDOUT << "Syntax Error:".colorize(:yellow).toggle(@color) << " " << ex.message << " at " << filename << ":" << ex.line_number << ":" << ex.column_number << "\n" + STDERR << "Syntax Error:".colorize(:yellow).toggle(@color) << " " << ex.message << " at " << filename << ":" << ex.line_number << ":" << ex.column_number << "\n" end rescue ex if check_files @@ -184,7 +181,6 @@ class Crystal::Command else couldnt_format "'#{filename}'" STDERR.puts - STDERR.flush end end end diff --git a/src/compiler/crystal/compiler.cr b/src/compiler/crystal/compiler.cr index 035532b2d9cb..80368e50654d 100644 --- a/src/compiler/crystal/compiler.cr +++ b/src/compiler/crystal/compiler.cr @@ -208,9 +208,9 @@ module Crystal parser.wants_doc = wants_doc? parser.parse rescue ex : InvalidByteSequenceError - stdout.print colorize("Error: ").red.bold - stdout.print colorize("file '#{Crystal.relative_filename(source.filename)}' is not a valid Crystal source file: ").bold - stdout.puts ex.message + stderr.print colorize("Error: ").red.bold + stderr.print colorize("file '#{Crystal.relative_filename(source.filename)}' is not a valid Crystal source file: ").bold + stderr.puts ex.message exit 1 end @@ -431,9 +431,9 @@ module Crystal TargetMachine.create(triple, @mcpu || "", @mattr || "", @release) end rescue ex : ArgumentError - stdout.print colorize("Error: ").red.bold - stdout.print "llc: " - stdout.puts ex.message + stderr.print colorize("Error: ").red.bold + stderr.print "llc: " + stderr.puts ex.message exit 1 end diff --git a/src/compiler/crystal/tools/init.cr b/src/compiler/crystal/tools/init.cr index 8f1b51d06610..0ab7f0504e9a 100644 --- a/src/compiler/crystal/tools/init.cr +++ b/src/compiler/crystal/tools/init.cr @@ -67,7 +67,7 @@ module Crystal def self.fetch_directory(args, project_name) directory = args.empty? ? project_name : args.shift if Dir.exists?(directory) || File.exists?(directory) - puts "file or directory #{directory} already exists" + STDERR.puts "file or directory #{directory} already exists" exit 1 end directory @@ -76,8 +76,8 @@ module Crystal def self.fetch_skeleton_type(opts, args) skeleton_type = fetch_required_parameter(opts, args, "TYPE") unless {"lib", "app"}.includes?(skeleton_type) - puts "invalid TYPE value: #{skeleton_type}" - puts opts + STDERR.puts "invalid TYPE value: #{skeleton_type}" + STDERR.puts opts exit 1 end skeleton_type @@ -85,8 +85,8 @@ module Crystal def self.fetch_required_parameter(opts, args, name) if args.empty? - puts "#{name} is missing" - puts opts + STDERR.puts "#{name} is missing" + STDERR.puts opts exit 1 end args.shift diff --git a/src/compiler/crystal/tools/print_hierarchy.cr b/src/compiler/crystal/tools/print_hierarchy.cr index 3ea03e50b1fe..15d94f183d11 100644 --- a/src/compiler/crystal/tools/print_hierarchy.cr +++ b/src/compiler/crystal/tools/print_hierarchy.cr @@ -278,7 +278,6 @@ module Crystal print_type(@program.object, json) end end - STDOUT.flush end def print_subtypes(types, json)