Skip to content

Support out: and err: redirects without native popen - #10084

Draft
sferik wants to merge 1 commit into
jruby:jruby-10.0from
sferik:windows-system-popen-redirects
Draft

sferik wants to merge 1 commit into
jruby:jruby-10.0from
sferik:windows-system-popen-redirects

Conversation

@sferik

@sferik sferik commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #10081

Without native popen, which is always the case on Windows, system drops its whole options hash with a warning and IO.popen ignores err:. So something like system(cmd, out: File::NULL, err: File::NULL) still prints everything.

This passes the redirects that ProcessBuilder can express through to it: a file path (or anything with to_path, including File::NULL) for out: and err: on system, the same for err: on IO.popen, and err: [:child, :out] for both. That follows CRuby's check_exec_redirect, which opens a path for fd 1 or 2 as write, create and truncate, and handles [:child, fd] separately: https://github.com/ruby/ruby/blob/67814cf9b0f3a4bad6f7369b0c8e6fc69220de64/process.c#L1907-L1912 and https://github.com/ruby/ruby/blob/67814cf9b0f3a4bad6f7369b0c8e6fc69220de64/process.c#L1929-L1953

jnr-posix's ProcessMaker doesn't implement redirects (its redirect* methods just call unimplementedError), so when there is a redirect I build the process with ProcessBuilder directly and copy the environment from ProcessMaker. Without a redirect nothing changes. A redirect also skips in-process launching, since an in-process script shares our streams. Every other option, and redirects to IO objects, fds or symbols, still warn like before.

I added specs for system with out:, err: File::NULL and err: [:child, :out], and for IO.popen with err:. They pass on CRuby 3.4.12, and on JRuby on macOS in interpreter and JIT mode and with -Xnative.popen=false, which takes the same path as Windows. With -Xnative.popen=false the new specs fail before this change. Two existing IO.popen specs that use err: [:child, :out] also start passing in that mode. They're tagged windows:, so I left the tags alone for now.

This overlaps with #6241, which goes further (chdir:, in:) but has been open since 2020. I kept this one to what the issue needs.

Things I noticed but didn't change: Process.spawn and exec have the same gap on the non-native path, system there also ignores chdir: and exception:, and an options hash at the end of an IO.popen command array is still dropped with a "popen3 does not support spawn options in JRuby 1.7" warning.

Without native popen, which is always the case on Windows, system
dropped its options with a warning and IO.popen ignored err:. Both now
pass a file path (including File::NULL) for out: or err:, and
[:child, :out] for err:, to ProcessBuilder. jnr-posix's ProcessMaker
doesn't implement redirects, so ProcessBuilder is used directly when
there is one. Other options still warn as before.

Fixes jruby#10081
@sferik
sferik force-pushed the windows-system-popen-redirects branch from d40d463 to f2a7cdd Compare November 16, 2026 18:23
@sferik
sferik marked this pull request as ready for review November 16, 2026 18:53

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This change is probably fine and ShellLauncher is already a mess, but perhaps we should actually make changes against jnr-posix's ProcessMaker to support redirects? It would make sense for at least some of this logic to live there and not become part of JRuby's public API.

@sferik

sferik commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Good idea. ProcessMaker already has redirectOutput, redirectError and redirectErrorStream, but Java5ProcessMaker leaves them unimplemented, so I'll implement them there by passing them through to the ProcessBuilder it wraps. Then this PR can use ProcessMaker for redirects too, instead of building a ProcessBuilder itself. The part that reads the Ruby options (out:, err:, [:child, :out]) still has to live in JRuby. I'll mark this as a draft until there's a jnr-posix release with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants