Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support wp-cli #827

Closed
adamziel opened this issue Dec 1, 2023 · 5 comments
Closed

Support wp-cli #827

adamziel opened this issue Dec 1, 2023 · 5 comments
Labels
[Feature] PHP.wasm [Focus] Developer Tools [Type] Bug An existing feature does not function as intended

Comments

@adamziel
Copy link
Collaborator

adamziel commented Dec 1, 2023

Now that proc_open support is available, let's find a way to support wp-cli.

Related tasks

Done is:

@adamziel adamziel added [Type] Bug An existing feature does not function as intended [Feature] PHP.wasm [Focus] Developer Tools labels Dec 1, 2023
@adamziel adamziel changed the title wp-cli support Fix wp-cli error Dec 1, 2023
@adamziel adamziel changed the title Fix wp-cli error Support wp-cli Dec 1, 2023
@adamziel
Copy link
Collaborator Author

adamziel commented Dec 1, 2023

I just tried running wp-cli via Playground's php-wasm-cli, and ran into some errors related to creating temporary files:

; PHP=8.0 node --loader=./packages/nx-extensions/src/executors/built-script/loader.mjs ./dist/packages/php-wasm/cli/main.js ../../wordpress-develop/wp-cli.phar
(node:86157) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

Warning: proc_open(): Unable to create temporary file. in phar:///wordpress-develop/wp-cli.phar/vendor/wp-cli/wp-cli/php/utils.php on line 1580

Warning: proc_open(): Cannot represent a stream of type TEMP as a File Descriptor in phar:///wordpress-develop/wp-cli.phar/vendor/wp-cli/wp-cli/php/utils.php on line 1580

Fatal error: Uncaught TypeError: proc_close(): Argument #1 ($process) must be of type resource, bool given in phar:///wordpress-develop/wp-cli.phar/vendor/wp-cli/wp-cli/php/commands/src/Help_Command.php:143
Stack trace:
#0 phar:///wordpress-develop/wp-cli.phar/vendor/wp-cli/wp-cli/php/commands/src/Help_Command.php(143): proc_close(false)
#1 phar:///wordpress-develop/wp-cli.phar/vendor/wp-cli/wp-cli/php/commands/src/Help_Command.php(94): Help_Command::pass_through_pager('\e[1mNAME\e[0m\n\n ...')

I started issue #828 to track this problem.

@adamziel
Copy link
Collaborator Author

adamziel commented Jan 10, 2024

Some wp-cli commands work on the latest trunk version!

; PHP=8.2 TMPDIR="`pwd`"/tmp node --loader ../plugins/playground/packages/nx-extensions/src/executors/built-script/loader.mjs ../plugins/playground/dist/packages/php-wasm/cli/main.js ./wp-cli.phar user list                                                                       1 ↵
+----+------------+--------------+--------------+--------------+---------------+
| ID | user_login | display_name | user_email   | user_registe | roles         |
|    |            |              |              | red          |               |
+----+------------+--------------+--------------+--------------+---------------+
| 1  | admin      | admin        | admin@exampl | 2023-02-25 1 | administrator |
|    |            |              | e.org        | 9:51:14      |               |
+----+------------+--------------+--------------+--------------+---------------+
; PHP=8.2 TMPDIR=`pwd`/tmp node --loader ../plugins/playground/packages/nx-extensions/src/executors/built-script/loader.mjs ../plugins/playground/dist/packages/php-wasm/cli/main.js ./wp-cli.phar config list
+------------------+-----------------------------+----------+
| name             | value                       | type     |
+------------------+-----------------------------+----------+
| table_prefix     | wptests_                    | variable |
| DB_NAME          | database_name_here          | constant |
| DB_USER          | username_here               | constant |
| DB_PASSWORD      | password_here               | constant |
| DB_HOST          | localhost                   | constant |
| DB_CHARSET       | utf8                        | constant |
| DB_COLLATE       |                             | constant |
| AUTH_KEY         | put your unique phrase here | constant |
| SECURE_AUTH_KEY  | put your unique phrase here | constant |
| LOGGED_IN_KEY    | put your unique phrase here | constant |
| NONCE_KEY        | put your unique phrase here | constant |
| AUTH_SALT        | put your unique phrase here | constant |
| SECURE_AUTH_SALT | put your unique phrase here | constant |
| LOGGED_IN_SALT   | put your unique phrase here | constant |
| NONCE_SALT       | put your unique phrase here | constant |
| WP_DEBUG         |                             | constant |
+------------------+-----------------------------+----------+
; PHP=8.2 TMPDIR=`pwd`/tmp node --loader ../plugins/playground/packages/nx-extensions/src/executors/built-script/loader.mjs ../plugins/playground/dist/packages/php-wasm/cli/main.js ./wp-cli.phar eval "echo 1;"
1

The next blocker is #931 and #930. These explorations are highly relevant.

@adamziel
Copy link
Collaborator Author

FYI @swissspidy @danielbachhuber @schlessera

@danielbachhuber
Copy link
Member

So cool! Thanks for driving this forward, @adamziel 😊

adamziel added a commit that referenced this issue Jan 18, 2024
Shims read(2) functionallity by providing an alternative read()
function called wasm_read() that gracefully handles blocking
pipes. This is neecessary because Emscripten returns EWOULDBLOCK
when reading from a blocking pipe, whereas a clang-compiled 
program would wait until data becomes available.

See:

* #951
* emscripten-core/emscripten#13214

## Other changes

This PR also ships:

* A regexp fix for `@php-wasm/cli` to preserve a trailing whitespace
when rewrite PHP-related spawn shell commands.
* A fix to correctly propagate the child process exit code back to PHP.
The WordPress bootstrap script for PHPUnit needs that to work correctly.

## Motivation

### wp-cli

Without this PR, running `wp-cli.phar` doesn't output anything. The
output is piped through a pager like `less` using `proc_open` and then
displayed by reading from a blocking pipe.

With this PR, running `wp-cli.phar` returns its regular help message as
the pager pipe can be read:

```
NAME
  wp
DESCRIPTION
  Manage WordPress through the command-line.
SYNOPSIS
  wp <command>
SUBCOMMANDS
  cache                 Adds, removes, fetches, and flushes the WP Object Cache
                        object.
  cap                   Adds, removes, and lists capabilities of a user role.
  cli                   Reviews current WP-CLI info, checks for updates, or
                        views defined aliases.
...
```

### PHPUnit

With this PR, Playground can run PHPunit on `wordpress-develop`!

```
TMPDIR=/tmp PHP=8.2 node --loader ../plugins/playground/packages/nx-extensions/src/executors/built-script/loader.mjs ../plugins/playground/dist/packages/php-wasm/cli/main.js ./vendor/bin/phpunit --no-coverage -v
(node:87723) ExperimentalWarning: Custom ESM Loaders is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Installing...
aaabb
int(0)
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 9.6.15 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.10-dev
Configuration: /Users/cloudnik/www/Automattic/core/wordpress-develop/phpunit.xml.dist
Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

...........................................................    59 / 17622 (  0%)
...........................................................   118 / 17622 (  0%)
............................FFFFFF.........................   177 / 17622 (  1%)
...........................................................   236 / 17622 (  1%)
```

## Testing instructions

Confirm the `proc_open()` tests pass on CI. This PR adjusts a few
of them to make sure the output is read without the tricky sleep()
call.

Related:

* #827
* #930

CC @mho22
@adamziel
Copy link
Collaborator Author

Wp-cli is now supported!

let’s close this issue and track any bugs and enhancements separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] PHP.wasm [Focus] Developer Tools [Type] Bug An existing feature does not function as intended
Projects
No open projects
Development

No branches or pull requests

2 participants