2

I'm developing Wordpress locally with XAMPP, and currently trying to install the WP-CLI tools as described here using Cygwin.

I renamed wp-cli.phar to wp, made it executable and moved it to the XAMPP/php folder.

However, running wp gives me the error:

Could not open input file: /cygdrive/b/Users/User/Desktop/XAMPP/php/wp

On the other hand, doing ./wp from the directory that the file is located in runs the program without issue.

This is despite the fact that both /cygdrive/b/Users/User/Desktop/XAMPP/php and /cygdrive/b/Users/User/Desktop/XAMPP/php/wp are in my PATH, via Cygwin's ~/.bash_profile file.

This is confirmed by doing which php:

/cygdrive/b/Users/User/Desktop/XAMPP/php/php

...and which wp:

/cygdrive/b/Users/User/Desktop/XAMPP/php/wp

What could be causing this problem?

2

I finally managed to scrape together enough information from disparate sources to figure out how to fix this, because - as I'm increasingly finding with anything related to Wordpress development - the documentation is so woefully inadequate.

The fact that it took so much research and hacking together on my part just to get an official command-line utility working on a platform that it claims to support is ridiculous, and my estimation of the Wordpress project and the Automattic team has dropped massively over the last few weeks for such reasons.

What follows in this answer adapts information from aefxx's answer here and leymannx's answer to this question.

As far I can tell, the issue here is that php.exe - the PHP binary included with XAMPP, WAMP and similar local servers - is a Windows binary that only understands paths in Windows format. The solution, as coded by aefxx in his answer, is to use a wrapper script that checks for Unix-style paths passed to the PHP binary and converts them into Windows paths that it can understand.

Note that because this solution is implemented as a wrapper to the PHP binary itself, it should work to solve this issue for any PHP program running under Cygwin, not just WP-CLI.

How to get WP-CLI working with Cygwin

Remember to replace any paths below with your own.

Once you've downloaded the wp-cli.phar file and made it executable as detailed in the documentation, move it to your server's PHP directory while renaming it to wp:

mv /cygdrive/b/Users/User/Desktop/XAMPP/php/wp

Inside the PHP directory, run the following:

touch php
chmod +x ./php

This creates a file called php inside of the PHP directory and makes it executable. This file will function as the wrapper script. Open the file in a text editor and paste the following into it, replacing the path to the PHP executable with your own:

#!/bin/bash

php="/cygdrive/b/Users/User/Desktop/XAMPP/php/php.exe"

for ((n=1; n <= $#; n++)); do
    if [ -e "${!n}" ]; then
        # Converts Unix style paths to Windows equivalents
        path="$(cygpath --mixed ${!n} | xargs)"

        case 1 in
            $(( n == 1 )) )
                set -- "$path" "${@:$(($n+1))}";;
            $(( n < $# )) )
                set -- "${@:1:$((n-1))}" "$path" ${@:$((n+1)):$#};;
            *)
                set -- "${@:1:$(($#-1))}" "$path";;
        esac
    fi
done

"$php" "$@"

Run cygstart ~/.bash_profile and add the following to the end of it to add your PHP directory to the PATH environment variable:

export PATH="/cygdrive/b/Users/User/Desktop/XAMPP/php:$PATH"

Finally, run:

source ~/.bash_profile
source ~/.bashrc

Running wp should now start WP-CLI successfully.

  • Seems legit. Only thing I'd do is to get rid of the wp alias. Since the wp-cli.phar does include a PHP shebang, you should be able to call it directly - i.e. as long as the wrapper is in your PATH. – aefxx Jun 18 at 7:53
  • @aefxx You're right. I could have sworn when testing yesterday that the alias was crucial in getting the whole thing to work, along with the wrapper, but sure enough removing it today doesn't seem to make a difference. Edited accordingly, thank you. – Hashim Jun 18 at 18:09
  • Glad, it's working for you. – aefxx Jun 19 at 8:44
1

There are at least two interesting questions on SO:

The easiest seems to be putting an alias into your Cygwin's ~/.bashrc or ~/.bash_profile:

alias wp="/cygdrive/b/Users/User/XAMPP/php/php /cygdrive/b/Users/User/XAMPP/php/wp"
  • That's a hacky solution, but I'll take it since it seems like the best one. I understand that the alias is calling the absolute links to both the php and wp binaries, but do you have any idea why that's necessary to do? – Hashim Jun 17 at 15:13
  • 1
    @Hashim – No clue to be honest. Some answers in the linked questions suggest it's Windows paths, some suggest its confusing PHP binaries... The alias then seems to be the least intrusive and the quickest fix at all; win win. – leymannx Jun 17 at 15:22
  • Scratch that, looks like I spoke too soon. Not even the alias allows wp to work. My actual path has a space in it, so I'm trying to nest both paths in single quotes, which gives me: alias wp="'/cygdrive/b/Users/User/Charity Website/XAMPP/php/php' '/cygdrive/b/Users/User/Charity Website/XAMPP/php/wp'". Adding this to ~/.bashrc and sourcing it doesn't make any difference, the same error persists. – Hashim Jun 17 at 16:13
  • 1
    @Hashim – Damn! Unfortunately I have no Windows at hand to test it myself any time soon. I edited my answer to unlock your vote, maybe you can unaccept my answer now? – leymannx Jun 17 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.