From adbe3b98384eaaf17010f59a2492a6304bd7c69a Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Sun, 14 Jan 2024 14:37:59 -0800 Subject: [PATCH 1/2] fix: no-codegen output file name warning This avoids errors based on output file name when the `--no-codegen` command is passed to `crystal build` since it shouldn't be necessary to specify an output file if no file will be created anyway. Example: $ crystal build --no-codegen example/example.cr Error: can't use `example` as output filename because it's a directory --- src/compiler/crystal/command.cr | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compiler/crystal/command.cr b/src/compiler/crystal/command.cr index 97bf46feb663..b35dd0c03c0d 100644 --- a/src/compiler/crystal/command.cr +++ b/src/compiler/crystal/command.cr @@ -493,6 +493,7 @@ class Crystal::Command unless no_codegen opts.on("--no-codegen", "Don't do code generation") do compiler.no_codegen = true + no_codegen = true end opts.on("-o ", "Output filename") do |an_output_filename| opt_output_filename = an_output_filename From ca382f89ee65fefbdb44162677dbb7149624a73d Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Mon, 15 Jan 2024 09:29:12 -0800 Subject: [PATCH 2/2] Address feedback The idea here is to make the `#create_compiler` method easier to reason about by splitting up the responsibilities of the `no_codegen` input parameter which is used when building the options parser and the `compiler.no_codegen?` boolean value which is used when deciding how to compile the program. --- src/compiler/crystal/command.cr | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/crystal/command.cr b/src/compiler/crystal/command.cr index b35dd0c03c0d..2753cc4db435 100644 --- a/src/compiler/crystal/command.cr +++ b/src/compiler/crystal/command.cr @@ -368,6 +368,7 @@ class Crystal::Command allowed_formats = ["text", "json"]) compiler = new_compiler compiler.progress_tracker = @progress_tracker + compiler.no_codegen = no_codegen link_flags = [] of String filenames = [] of String has_stdin_filename = false @@ -493,7 +494,6 @@ class Crystal::Command unless no_codegen opts.on("--no-codegen", "Don't do code generation") do compiler.no_codegen = true - no_codegen = true end opts.on("-o ", "Output filename") do |an_output_filename| opt_output_filename = an_output_filename @@ -597,7 +597,7 @@ class Crystal::Command output_filename = "#{::Path[first_filename].stem}#{output_extension}" # Check if we'll overwrite the main source file - if !no_codegen && !run && first_filename == File.expand_path(output_filename) + if !compiler.no_codegen? && !run && first_filename == File.expand_path(output_filename) error "compilation will overwrite source file '#{Crystal.relative_filename(first_filename)}', either change its extension to '.cr' or specify an output file with '-o'" end end @@ -609,7 +609,7 @@ class Crystal::Command error "maximum number of threads cannot be lower than 1" if compiler.n_threads < 1 - if !no_codegen && !run && Dir.exists?(output_filename) + if !compiler.no_codegen? && !run && Dir.exists?(output_filename) error "can't use `#{output_filename}` as output filename because it's a directory" end @@ -617,7 +617,7 @@ class Crystal::Command emit_base_filename = ::Path[sources.first.filename].stem end - combine_rpath = run && !no_codegen + combine_rpath = run && !compiler.no_codegen? @config = CompilerConfig.new compiler, sources, output_filename, emit_base_filename, arguments, specified_output, hierarchy_exp, cursor_location, output_format.not_nil!, combine_rpath, includes, excludes, verbose, check, tallies