Comments (6)
Can you explain the use case a bit better? I'd like to not bloat the API by adding stuff that can easily be done cross-platform through other means, which is what Dir.chdir provides.
from childprocess.
Certainly. I agree and can empathize with you on avoiding API bloat of a good library. Dir.chdir
is cross-platform, but forking a child process isn't, and doing both at once cross-platform gets very difficult quickly.
First, a general use case: You want to execute some program that creates/uses files based on the current working directory, which is not the same as the CWD you invoked your application from. This requires a Dir.chdir
at some point.
A more specific use case, in my case: I am writing a suite of acceptance tests for a command-line application (Vagrant), which by default uses the CWD for many things. Of course, I can use environmental variables to tell Vagrant to look in other places, but I'd also like to test that when it does use the CWD, it does it correctly. I'm currently using posix-spawn because it makes specifying chdir easy as well as working with child processes. Unfortunately, I'd love to be able to run these acceptance tests on Windows machines as well, so posix-spawn isn't a long term solution.
Now, let's look at how we would do this using childprocess
in its current form: Since childprocess
doesn't support chdir
, I would actually need to execute another application which does a chdir
, which then executes ANOTHER child process (via whatever means) to get that chdir. The end-developer complexity gets very high for such a simple thing. The easiest thing to do obviously would be for the first Ruby process to call fork
then Dir.chdir
but of course, fork
doesn't work on Windows.
Let's see how childprocess
would do it if it were built in. Note I am not familiar with Windows APIs so I'm not sure how it would work there. But on Unix: The childprocess module would simply do a fork
, Dir.chdir
, then an exec
. Easy. On Windows, where fork
doesn't exist, it would do whatever is necessary to set the Dir.chdir
prior to creating the subprocess. May not be easy for the childprocess
dev (sorry!) but it makes it easy for the end user.
So, in conclusion:
Yes, Dir.chdir
is cross platform. Doing this as a one-time go within your app is easy.
But, Dir.chdir
for only a child process is a nightmare, and I think something like childprocess
can make this easy.
Thanks for your time, as always, and I hope this makes sense. Please let me know if it doesn't.
Best,
Mitchell
from childprocess.
Thanks for the explanation. I still don't quite get this part:
Since childprocess doesn't support chdir, I would actually need to execute another application which does a chdir, which then executes ANOTHER child process (via whatever means) to get that chdir. The end-developer complexity gets very high for such a simple thing.
This appears to work well (on both *nix and Windows) and doesn't seem overly complex to me:
process = Dir.chdir(Dir.tmpdir) do
ChildProcess.build(
"ruby", "-rfileutils", "-e", "FileUtils.touch 'hello'"
).start
end
process.poll_for_exit(1)
p File.exists?(File.join(Dir.tmpdir, 'hello')) #=> true
That is, no launching of two separate processes just to do a chdir. Have you considered this option? Is the problem that you want to call #start from code that shouldn't know about what directory the child should execute in? That's the only reason I can think of where doing the above would be inconvenient.
This would be a pretty simple change, regardless of platform - it's basically just moving a piece of state (the directory name) out of the user's hands and into childprocess, then executing Dir.chdir right before we launch the process (instead of having the user do the same). Since it's so simple to do, I just want to make sure you haven't considered this option :)
PS. You might want to check out Aruba which is built on childprocess and makes CLI acceptance testing pretty simple (though perhaps too simple for Vagrant).
from childprocess.
Jarib,
Ah hah, I wasn't aware of block passing to Dir.chdir
.
This indeed solves all problems I'm aware of. Closing this issue!
As for Aruba, that is where I found out about childprocess
:) I still need to investigate aruba more, although I have a distinct opinion against anything related to cucumber (I hate cucumber), so that is tarnishing my view of it right now. ;)
Mitchell
from childprocess.
Glad to have helped :) And thanks for Vagrant!
from childprocess.
Thanks :)
from childprocess.
Related Issues (20)
- Redirect to IO.pipe on Windows causes inability to start a process HOT 6
- Security Question: Is there a reason childprocess is released as an unsigned gem? HOT 2
- FFI is a required pre-requisite HOT 4
- Pipe i/o example in documentation is incorrect HOT 2
- Erro ao rodar o comando rspec -fd HOT 1
- childprocess kills detached windows process on exit HOT 4
- FFI is a required pre-requisite for Windows or posix_spawn support in the ChildProcess gem. HOT 1
- A backend based on Process.spawn HOT 12
- Ruby 3.0 support HOT 10
- code hanging attempting to @thread.join HOT 1
- Should work for the M1 chip, but needs to be mapped or smth
- `host_cpu` changes between Ruby 2.7.2 and 2.7.3 HOT 2
- FFI installes but rake test get error HOT 4
- cannot load such file -- ffi (LoadError) HOT 2
- Use with StringIO HOT 1
- posix_spawn is not yet supported on aarch64-linux (aarch64-linux), HOT 2
- posix_spawn is not yet supported on sparc-solaris (sparc-solaris2.11) HOT 1
- I happen to be having this error after even having all both ruby, gem, and even chocolatey handy HOT 2
- Test suite is going to be broken with Ruby 3.3 HOT 1
- differences between macos and linux HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from childprocess.