4

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 Answers 2

3

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 where you want to install it - /usr/local/bin tends to be the convention for user-installed binaries on Linux, or inside of XAMPP's PHP server root if you're using XAMPP - and rename it to something easier to type, like wp:

mv wp-cli.phar /cygdrive/c/XAMPP/php/wp

Then, navigate to the new install directory and create a wrapper file called php:

cd /cygdrive/c/XAMPP/php/wp &&
touch php &&
chmod +x ./php

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/c/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" "$@"

Finally, run wp to confirm that WP-CLI - and any other PHP tools requiring a Cygwin wrapper - is now working on Cygwin.

3
  • 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, 2019 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. Jun 18, 2019 at 18:09
  • Glad, it's working for you.
    – aefxx
    Jun 19, 2019 at 8:44
2

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"
4
  • 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? Jun 17, 2019 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, 2019 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. Jun 17, 2019 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, 2019 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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