Git Product home page Git Product logo

Comments (6)

jarib avatar jarib commented on June 19, 2024

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.

mitchellh avatar mitchellh commented on June 19, 2024

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.

jarib avatar jarib commented on June 19, 2024

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.

mitchellh avatar mitchellh commented on June 19, 2024

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.

jarib avatar jarib commented on June 19, 2024

Glad to have helped :) And thanks for Vagrant!

from childprocess.

mitchellh avatar mitchellh commented on June 19, 2024

Thanks :)

from childprocess.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.