“Could not open input file” error even when WP-CLI is in the path

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

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 to open the .bash_profile file, and add the following to the end of it to add XAMPP’s PHP directory to the PATH environment variable:

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

Finally, run source ~/.bash_profile to load the new contents of the .bash_profile file.

Finally, run wp to confirm that WP-CLI is now working.

Leave a Comment