Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compiler path to $PATH for subcommands #15186

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/crystal
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ case "$(uname -s)" in
;;
esac

# CRYSTAL_EXEC_PATH determines the location of the `crystal` program for external
# compiler commands.
CRYSTAL_EXEC_PATH="$SCRIPT_ROOT"

if [ -x "$CRYSTAL_DIR/${CRYSTAL_BIN}" ]; then
__warning_msg "Using compiled compiler at ${CRYSTAL_DIR#"$PWD/"}/${CRYSTAL_BIN}"
exec "$CRYSTAL_DIR/${CRYSTAL_BIN}" "$@"
Expand Down
32 changes: 20 additions & 12 deletions spec/primitives/external_command_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,39 @@ 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

exit 123
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"])
status.normal_exit?.should be_true
status.exit_code.should eq 123

lines.should eq [
compiler_path,
command_path,
%(["foo", "bar"]),
]
end
end
end
14 changes: 13 additions & 1 deletion src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,19 @@ 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})

crystal_exec_path = ENV["CRYSTAL_EXEC_PATH"]?
unless crystal_exec_path
if executable_path = Process.executable_path
crystal_exec_path = File.dirname(executable_path)
end
end
path = [crystal_exec_path, ENV["PATH"]?].compact!.join(Process::PATH_DELIMITER)

Process.exec(external_command, options, env: {
"PATH" => path,
"CRYSTAL_EXEC_PATH" => crystal_exec_path,
})
else
error "unknown command: #{command}"
end
Expand Down
Loading