Skip to content

Commit

Permalink
Add compiler path to $PATH for subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Nov 12, 2024
1 parent caf57c2 commit 4ff8ad5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
27 changes: 16 additions & 11 deletions spec/primitives/external_command_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4ff8ad5

Please sign in to comment.