From 4ff8ad5b1b4eadb0ef13c2f18a752557fe912082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Tue, 12 Nov 2024 15:39:27 +0100 Subject: [PATCH] Add compiler path to `$PATH` for subcommands --- spec/primitives/external_command_spec.cr | 27 ++++++++++++++---------- src/compiler/crystal/command.cr | 7 +++++- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/spec/primitives/external_command_spec.cr b/spec/primitives/external_command_spec.cr index 91687f7c2d21..5e9b40a65dfb 100644 --- a/spec/primitives/external_command_spec.cr +++ b/spec/primitives/external_command_spec.cr @@ -4,31 +4,36 @@ require "../spec_helper" describe Crystal::Command do it "exec external commands", tags: %w[slow] do - with_temp_executable "crystal-external" do |path| + with_temp_executable "crystal-external" do |command_path| + compiler_path = File.expand_path(ENV["CRYSTAL_SPEC_COMPILER_BIN"]? || "bin/crystal") + with_tempfile "crystal-external.cr" do |source_file| File.write source_file, <<-CRYSTAL - puts ENV["CRYSTAL"]? + puts Process.find_executable("crystal") puts PROGRAM_NAME puts ARGV CRYSTAL - Process.run(ENV["CRYSTAL_SPEC_COMPILER_BIN"]? || "bin/crystal", ["build", source_file, "-o", path]) + Process.run(compiler_path, ["build", source_file, "-o", command_path]) end - File.exists?(path).should be_true + File.exists?(command_path).should be_true - process = Process.new(ENV["CRYSTAL_SPEC_COMPILER_BIN"]? || "bin/crystal", + process = Process.new(compiler_path, ["external", "foo", "bar"], output: :pipe, - env: {"PATH" => {ENV["PATH"], File.dirname(path)}.join(Process::PATH_DELIMITER)} + env: {"PATH" => {ENV["PATH"], File.dirname(command_path)}.join(Process::PATH_DELIMITER)} ) - output = process.output.gets_to_end + lines = process.output.gets_to_end.lines + status = process.wait status.success?.should be_true - lines = output.lines - lines[0].should match /crystal/ - lines[1].should match /crystal-external/ - lines[2].should eq %(["foo", "bar"]) + + lines.should eq [ + compiler_path, + command_path, + %(["foo", "bar"]), + ] end end end diff --git a/src/compiler/crystal/command.cr b/src/compiler/crystal/command.cr index 571c965352e0..c23259f90e09 100644 --- a/src/compiler/crystal/command.cr +++ b/src/compiler/crystal/command.cr @@ -132,7 +132,12 @@ class Crystal::Command error "file '#{command}' does not exist" elsif external_command = Process.find_executable("crystal-#{command}") options.shift - Process.exec(external_command, options, env: {"CRYSTAL" => Process.executable_path}) + if executable_path = Process.executable_path + crystal_path = File.dirname(executable_path) + end + path = [crystal_path, ENV["PATH"]?].compact_map!.join(Process::PATH_DELIMITER) + + Process.exec(external_command, options, env: {"PATH" => path}) else error "unknown command: #{command}" end