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

IO.popen fails to find executable with chdir: kwarg #8216

Closed
segiddins opened this issue Apr 29, 2024 · 6 comments · Fixed by #8218
Closed

IO.popen fails to find executable with chdir: kwarg #8216

segiddins opened this issue Apr 29, 2024 · 6 comments · Fixed by #8218
Milestone

Comments

@segiddins
Copy link

Environment Information

Provide at least:

  • JRuby version (jruby -v) and command line (flags, JRUBY_OPTS, etc)
    9.4.6.0
  • Operating system and platform (e.g. uname -a)
    Darwin sr-71 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:55:06 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6020 arm64
irb(main):001> IO.popen(["echo", "hi"], &:read)
=> "hi\n"
irb(main):002> IO.popen(["echo", "hi"], chdir: "/", &:read)
org/jruby/RubyIO.java:4494:in `popen': No such file or directory - echo (Errno::ENOENT)
        from (irb):2:in `evaluate'
        from org/jruby/RubyKernel.java:1265:in `eval'
        from org/jruby/RubyKernel.java:1707:in `loop'
        from org/jruby/RubyKernel.java:1414:in `catch'
        from /Users/segiddins/.gem/jruby/3.1.4/gems/irb-1.8.1/exe/irb:9:in `<main>'
        from org/jruby/RubyKernel.java:1211:in `load'
        from /Users/segiddins/.gem/jruby/3.1.4/bin/irb:25:in `<main>'

This works on MRI.

@enebo enebo added this to the JRuby 9.4.8.0 milestone Apr 29, 2024
@headius
Copy link
Member

headius commented Apr 30, 2024

Good morning! This is a good find. Do you know what version of Ruby this keyword was added? Shouldn't be hard to add support.

@headius
Copy link
Member

headius commented Apr 30, 2024

looks like it has been around in some form since at least 2014. I guess we haven't audited our port of the argument processing code in some time!

@headius
Copy link
Member

headius commented Apr 30, 2024

Ok so we do have code for the option, but the way it is assembled is incorrect. Because posix_spawn does not support doing chdir, we have to restructure the command line to use sh and cd. This currently only happens when the current directory is not the same as the one JRuby was started in.

I'll see if I can rework that command line restructuring code to happen after the chdir option has been processed.

@headius
Copy link
Member

headius commented Apr 30, 2024

I've pushed a slightly hacky fix in #8218 that processes the chdir option earlier in order to let spawn initialization code see it and adjust the command line for sh and cd.

This code may be reaching a breaking point with all these hacks around posix_spawn limitations, so I think we need to explore making https://github.com/byteit101/subspawn used by default and only fall back on the ported logic when necessary.

@segiddins
Copy link
Author

I think you want posix_spawn_file_actions_addchdir, from looking at the docs?

@headius
Copy link
Member

headius commented Apr 30, 2024

@segiddins posix_spawn_file_actions_addchdir (and the variants _addchdir_np and _addfchdir) is unfortunately nonstandard. It has been implemented in recent version of glibc, unsure about musl, MacOS has it inherited from the BSDs, but all of them document it as a non-standard function. We could attempt to wire in the various versions of it, and then do version checking to only use it conditionally, but we'd still have to fall back on the sh+cd logic when it's not available.

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 a pull request may close this issue.

3 participants