Repository navigation
shell_exec: preflight direct spawn failures - #3015
Conversation
worktrunk-bot
left a comment
There was a problem hiding this comment.
Nice portability fix — moving the missing-binary / bad-current-dir detection ahead of spawn() is the right call for the platforms (qemu-user, OpenBSD, HPPA) where the child-side exec-failure errno channel is lossy, and the conservative mode & 0o111 check correctly only rejects when no exec bit is set rather than guessing at the effective-uid permission outcome. Tests pass locally, including the two qemu-affected ones.
One asymmetry worth tightening: stream() records the preflight failure to the [wt-trace] stream via trace_log.errored(&e), but run() and pipe_into() return through ? without emitting a trace record. Before this change a missing-binary / bad-dir failure in run() flowed through cmd.output() → trace_log.record_result() → errored, so it appeared in -vv output (and, for external() commands, the command log). With the preflight catching it first, that command now vanishes from the trace — and the bad-current_dir case is exactly the failure shape wt -vv diagnostics exist to surface (it's the same path repo_path_error_when_is_bare_fails exercises). Suggestions inline mirror the stream() handling.
|
Heads up: The uncovered lines are all the "exists but wrong type / not executable" branches. Both new tests use
Three small regression tests would close the gap and, more usefully, pin the behavior this PR is really about (turning these into spawn-time errors rather than exit-127):
Want me to push those tests to your branch? |
Rust's Command API normally reports execve and current_dir setup failures synchronously when spawn/output fails. Under qemu-user, glibc's posix_spawn path can use CLONE_VM|CLONE_VFORK and expect the child to write those errors back to the parent, but qemu-user does not preserve that shared-stack error channel. The parent can then observe a successful spawn and only see the child exit with status 127. This fixes the riscv64 qemu-user failures in shell_exec::tests::test_cmd_stream_spawn_failure_is_errored and git::repository::tests::repo_path_error_when_is_bare_fails, both of which expect missing executable or invalid current_dir failures to surface as spawn errors instead of ordinary child exit status 127. This is a portability issue in the child-side exec failure reporting path, not just a qemu-user quirk. Not all posix_spawn() implementations use errno to report execv() failures from the child process, such as qemu-user, OpenBSD and HPPA. Check the pieces that worktrunk already knows before spawning: explicit current_dir values must name directories, and direct commands with absolute program paths must already name executable files. All entry points keep their trace behavior for preflight failures: stream() keeps the existing 'Failed to execute command' wrapping, run() records the failed command and external log entry before returning the io::Error, and pipe_into() records the failing side before returning the io::Error. Add regression tests for missing paths, existing non-directory current_dir values, directory program paths, non-executable program paths, and update the existing stream() test to describe the externally visible behavior instead of depending on cmd.spawn() being the failing call.
Rust's Command API normally reports execve and current_dir setup failures synchronously when spawn/output fails. Under qemu-user, glibc's
posix_spawnpath can useCLONE_VM|CLONE_VFORKand expect the child to write those errors back to the parent, but qemu-user does not preserve that shared-stack error channel. The parent can then observe a successful spawn and only see the child exit with status 127.This fixes the riscv64 qemu-user failures in shell_exec::tests::test_cmd_stream_spawn_failure_is_errored and git::repository::tests::repo_path_error_when_is_bare_fails, both of which expect missing executable or invalid current_dir failures to surface as spawn errors instead of ordinary child exit status 127.
This is a portability issue in the child-side exec failure reporting path, not just a qemu-user quirk. Not all posix_spawn() implementations use errno to report execv() failures from the child process, such as qemu-user, OpenBSD and HPPA.
Check the pieces that worktrunk already knows before spawning: explicit current_dir values must name directories, and direct commands with absolute program paths must already name executable files. stream() keeps the existing 'Failed to execute command' wrapping and trace behavior, while run() and pipe_into() continue to return io::Error directly.
Add regression tests for direct run() failures and update the existing stream() test to describe the externally visible behavior instead of depending on cmd.spawn() being the failing call.
Ref:
https://gitlab.com/qemu-project/qemu/-/work_items/140
https://lists.nongnu.org/archive/html/qemu-devel/2025-03/msg04706.html