Skip to content

Use Process.exec in shards run #686

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
27 changes: 18 additions & 9 deletions src/commands/run.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,26 @@ module Shards
name = targets.first
end

if target = spec.targets.find { |t| t.name == name }
Commands::Build.run(path, [target.name], options)
target = spec.targets.find { |t| t.name == name }

Log.info { "Executing: #{target.name} #{run_options.join(' ')}" }
raise Error.new("Error target #{name} was not found in #{SPEC_FILENAME}") unless target

Commands::Build.run(path, [target.name], options)

Log.info { "Executing: #{target.name} #{run_options.join(' ')}" }

{% if flag?(:win32) %}
# FIXME: Process.exec doesn't work as expected on Windows, we need to run
# as a child process and report the exit code afterwards. https://github.com/crystal-lang/crystal/issues/14422#issuecomment-3204803933
status = Process.run(File.join(Shards.bin_path, target.name), args: run_options, input: Process::Redirect::Inherit, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
unless status.success?
exit status.exit_code
end
else
raise Error.new("Error target #{name} was not found in #{SPEC_FILENAME}")
end
exit status.exit_code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this continue to raise for STATUS_BREAKPOINT because it is an abnormal exit?

Copy link
Member Author

@straight-shoota straight-shoota Aug 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes 🙈 I've been responsible for the last Process::Status API revamp, but still manage to mistake it.

Maybe the confusing part is that #exit_code? returns nil for abnormal exits, which makes sense on Unix (abnormal states are not exit codes), but less so on Windows where they indeed are exit codes. Maybe that's not right...? 🤔

Or we should just use #system_exit_status here... On Windows it fits exactly what we need.

{% else %}
# FIXME: The explicit close is necessary to flush the last log message
# before `exec`. https://github.com/crystal-lang/crystal/issues/14422#issuecomment-3204803933
::Log.builder.close

Process.exec(File.join(Shards.bin_path, target.name), args: run_options)
{% end %}
end
end
end
Expand Down