studio icon indicating copy to clipboard operation
studio copied to clipboard

WP CLI: environment variables not set

Open fbertram opened this issue 1 year ago • 1 comments

Quick summary

I want to use WP CLI on Windows. When I open a shell by clicking Studio's 'Terminal' button, the environment variables needed to run WP CLI are not set correctly.

Steps to reproduce

  1. Start the Studio App on Windows
  2. Start an existing WordPress project
  3. Click on the 'Open in Terminal' button to open a shell
  4. enter a command, e.g., wp user list

What you expected to happen

WP CLI should launch and execute the desired command

What actually happened

because PATH is not set correctly, the wp command is not found. Also, in order to successfully execute C:\Users\[username]\AppData\Local\studio\app-1.0.5\resources\bin\wp.bat, the variable STUDIO_APP_PATH must be set.

Impact

Some (< 50%)

Available workarounds?

Yes, easy to implement

Platform

Windows

Logs or notes

A possible workaround is to:

  • set STUDIO_APP_PATH to C:\Users\[username]\AppData\Local\studio\app-1.0.5\Studio.exe
  • set PATH to include C:\Users\[username]\AppData\Local\studio\app-1.0.5\resources\bin

fbertram avatar Jul 13 '24 20:07 fbertram

I am on Mac, I will let you triage this one, @wojtekn

mrfoxtalbot avatar Jul 16 '24 10:07 mrfoxtalbot

I am a mac user as well. @wojtekn were you able to take a look at this?

peterschimke avatar Nov 19 '24 14:11 peterschimke

@mrfoxtalbot @peterbutler I validated and confirmed this issue:

  1. Install PHP for Windows CLI using instructions
  2. Install WP CLI using instructions
  3. Open Terminal from Studio
  4. Run wp command
  5. See the error

Image

When I open a new terminal tab and run the wp command again, it works correctly:

Image

wojtekn avatar Nov 20 '24 09:11 wojtekn

This may not be directly related to the problem reported here, but I had a problem when trying to get WP-CLI to work in Studio. Here's how I solved it, but I hope it helps someone.

PHP and WP-CLI already work on Windows OS:

wp --info
OS:     Windows NT 10.0 build 22631 (Windows 11) AMD64
Shell:  C:\Windows\system32\cmd.exe
PHP binary:     C:\php\php-8.3.14-Win32-vs16-x64\php.exe
PHP version:    8.3.14
php.ini used:   C:\php\php-8.3.14-Win32-vs16-x64\php.ini
MySQL binary:
MySQL version:
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       C:\Users\{username}\Studio\my-wordpress-website
WP-CLI packages dir:
WP-CLI cache dir:       C:\Users\{username}/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.11.0

When I opened the terminal from the Studio app and ran wp user list, the following error occurred:

wp user list
Error: PDO Driver for SQLite is missing. Your PHP installation appears not to have the right PDO drivers loaded. These are required for this version of WordPress and the type of database you have specified.

So I edited php.ini and enabled the following two lines:

extension=pdo_sqlite
extension=sqlite3

By doing this, WP-CLi worked in Studio:

wp user list
+----+------------+--------------+---------------------+---------------------+-----------------------------+
| ID | user_login | display_name | user_email          | user_registered     | roles                       |
+----+------------+--------------+---------------------+---------------------+-----------------------------+
| 1  | admin      | admin        | [email protected] | 2024-11-09 07:20:34 | administrator,bbp_keymaster |
+----+------------+--------------+---------------------+---------------------+-----------------------------+

t-hamano avatar Dec 09 '24 08:12 t-hamano

I've debugged this and concluded that it happens because Studio is started with the system-level PATH environment variable, which is then passed down to the terminal instance.

When I installed WP-CLI, I only modified the user-level environment variable (and I suspect you did the same thing, @wojtekn). When the command line is started from the Start menu, it pulls the user-level PATH variable, which is why running wp that way works, but not from the Studio-initiated command line.

It was surprisingly difficult to launch a child process from Node that uses the "default" user-level PATH variable. I'll do some more digging tomorrow.

Image

fredrikekelund avatar Jan 16 '25 15:01 fredrikekelund

Would it work correctly if the user set the path for all users in the system, like described in the guide?

setx PATH "%PATH%;c:\wp-cli" /m

I see the official guide doesn't include the /m option, though.

If the above works, I think we could close it, as the problem could be solved easily outside the Studio.

wojtekn avatar Jan 17 '25 07:01 wojtekn

Adding the WP-CLI path to the system-level PATH environment variable fixes the issue. So, yes, setx /M "%PATH%;c:\wp-cli" should work 👍

Still, I'm sure plenty of users will overlook this distinction when installing WP-CLI, and for us to document it as a Windows gotcha seems contrary to the idea of supporting Windows as a first-class citizen.

After banging my head against this issue for a while longer, I found a solution that should work in most cases. I'll post a PR shortly

fredrikekelund avatar Jan 17 '25 10:01 fredrikekelund

Thank you for looking into this. We should make sure it's somewhere in on our developer documentation.

mrfoxtalbot avatar Jan 17 '25 10:01 mrfoxtalbot

https://github.com/Automattic/studio/pull/814 has a fix that should cover all normal Windows installations (it depends on PowerShell), so hopefully we shouldn't have to document it

fredrikekelund avatar Jan 17 '25 10:01 fredrikekelund

For the record, when testing built versions of Studio, I could no longer reproduce the problem reported in this issue. I've confirmed that command prompts spawned by Studio loads the user-level PATH environment variable (which is what starting a command prompt from the Start menu does, too).

I've also confirmed that the logic in Studio for launching a Windows command prompt is equivalent to that in GitHub Desktop, which is additional confirmation that we don't need to tweak our existing behavior.

The upcoming 1.3.3 release will contain my fixes from https://github.com/Automattic/studio/pull/814, but they ended up being more cosmetic.

@fbertram, if you are still experiencing issues after installing the 1.3.3 release (due in the next week or so), please let us know. The more detailed the bug report, the easier for us it will be to reproduce 🙏

fredrikekelund avatar Jan 29 '25 13:01 fredrikekelund