Skip to content

Commit

Permalink
Print build errors to stderr
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bew committed Jun 6, 2017
1 parent abd2dfd commit 06a304d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 41 deletions.
10 changes: 5 additions & 5 deletions spec/compiler/crystal/tools/init_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,27 @@ 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

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

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
Expand Down
12 changes: 6 additions & 6 deletions src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
32 changes: 14 additions & 18 deletions src/compiler/crystal/command/format.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -168,23 +165,22 @@ 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
check_files << FormatResult.new(filename, FormatResult::Code::BUG)
else
couldnt_format "'#{filename}'"
STDERR.puts
STDERR.flush
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions src/compiler/crystal/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions src/compiler/crystal/tools/init.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -76,17 +76,17 @@ 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
end

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
Expand Down
1 change: 0 additions & 1 deletion src/compiler/crystal/tools/print_hierarchy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ module Crystal
print_type(@program.object, json)
end
end
STDOUT.flush
end

def print_subtypes(types, json)
Expand Down

0 comments on commit 06a304d

Please sign in to comment.