Git Product home page Git Product logo

Comments (12)

jarib avatar jarib commented on July 19, 2024

The error message isn't helpful because Windows is giving us conflicting information.

From the docs for CreateProcess:

If the function fails, the return value is zero. To get extended error information, call GetLastError.

In your case, CreateProcess returns zero, but GetLastError thinks "the operation completed successfully". I have not been able to track down what could cause this to happen.

from childprocess.

Ajedi32 avatar Ajedi32 commented on July 19, 2024

Yeah, I figured it was something weird like that.

In my case though I'm curious about why the command is failing at all. As I said, Open3 has no problem spawning the exact same process, so why is ChildProcess failing? Odd...

from childprocess.

AcidWeb avatar AcidWeb commented on July 19, 2024

Same issue. Windows 8.1 and Ruby 2.0.0-p247. Any workaround is available?

from childprocess.

rarenerd avatar rarenerd commented on July 19, 2024

What seems to work for me is adding "cmd.exe /c" in front of it. I'm guessing this is because you are actually calling a .bat file and according to the docs:

To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the following arguments: /c plus the name of the batch file.

Now lpApplicationName is set to NULL here: https://github.com/jarib/childprocess/blob/master/lib/childprocess/windows/process_builder.rb#L75

However it seems that just adding it to lpCommandLine does the same trick.

from childprocess.

jarib avatar jarib commented on July 19, 2024

@rarenerd Indeed - CreateProcess won't execute batch files directly. Both of these seem to solve the problem:

ChildProcess.build("ruby", "-S", "bundle")
ChildProcess.build("cmd.exe", "/c", "bundle")

from childprocess.

Ajedi32 avatar Ajedi32 commented on July 19, 2024

So is there any way that ChildProcess could be made to detect batch files and automatically run them using an interpreter? Would that be desirable behavior? If not, ChildProcess's inability to run batch files directly on Windows is probably something that should be documented. Either way, I'm not sure this issue should be closed just yet.

from childprocess.

jarib avatar jarib commented on July 19, 2024

I'm very reluctant to customize behaviour depending on what type of file is being executed. This is also consistent with what we already do on Unix - if you want to have your command interpreted, you need to explicitly run the shell yourself

ChildProcess.build("bash", "-c", "echo *")
# vs.
ChildProcess.build("echo", "*")

This is deliberately in contrast to Kernel#exec's default behaviour.

I'd be happy to accept a pull requests that documents the Windows workaround though.

from childprocess.

Ajedi32 avatar Ajedi32 commented on July 19, 2024

I'm confused. If this is consistent with how Unix does things, then why is the exact same command that works on Unix failing on Windows? Is it just because Windows ignores the hash bang at the start of the file whereas Unix uses that information?

If that's the case, couldn't ChildProcess somehow emulate that behavior to bring Windows more in line with the way Unix does things? I'd really prefer to avoid having to write a special case Windows if I can help it.

from childprocess.

jarib avatar jarib commented on July 19, 2024

I didn't claim it is consistent with "how Unix does things", but that ChildProcess is consistent in the decision not to execute commands through a command-line interpreter on either platform.

Even if you consider that moot, I can't imagine what a satisfactory heuristic to detect when to execute in cmd.exe would look like – which I think indicates it's better to leave the decision to the user.

If you know you always want cmd.exe on Windows, it's fairly simple to do:

def create_process(*cmd)
  cmd.unshift("cmd.exe", "/c") if ChildProcess.windows?
  ChildProcess.build(*cmd)
end

from childprocess.

Ajedi32 avatar Ajedi32 commented on July 19, 2024

Hmm, yeah I think I see what you're saying. Obviously you don't want to interpret any shell commands passed to ChildProcess.build, you just want to execute the batch file using the desired arguments.

Is there a reason though that we can't just use the hash-bang in the first line of a file to determine whether or not to invoke an interpreter? That's what Unix does, right? Although, usually the paths to the interpreters used in hash-bang lines are very Unix-specific, so maybe that wouldn't work...

from childprocess.

Ajedi32 avatar Ajedi32 commented on July 19, 2024

Hold on a second! How does Open3 resolve this problem? As shown in the example in my original post, Open3.popen3 works fine, and I'm pretty sure Open3 doesn't invoke cmd.exe for every command I run with it. Open3 also has the same behavior as ChildProcess in that it passes the arguments listed in the method call directly to the spawned process without interpreting them as shell commands. So what are they doing that we've missed?

from childprocess.

jarib avatar jarib commented on July 19, 2024

Open3 uses Process.spawn under the hood. You'll have to dig through the C source to figure out how it decides to invoke the shell.

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.