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 in the browser #957

Merged
merged 19 commits into from
Jan 22, 2024
Merged

Support wp-cli in the browser #957

merged 19 commits into from
Jan 22, 2024

Conversation

adamziel
Copy link
Collaborator

@adamziel adamziel commented Jan 18, 2024

What is this PR doing?

Adds wp-cli support in the browser!

CleanShot 2024-01-22 at 15 13 38@2x

This PR brings parts of #161 by @swissspidy into trunk to enable using wp-cli in the browser.

Codebase changes

  • setSapiName method for PHP. wp-cli expects to see the exact string "cli" as the SAPI name.
  • All PHP versions are now built with phar support.
  • js_open_process is asynchronous via Asyncify. This is to enable calling setSpawnHandler(handler) from the parent site to set a handler in an asynchronous web worker. Unfortunately, using the Comlink messaging library is challenging with complex objects like spawn handlers so this PR ships a workaround where spawn handlers are passed as strings and eval()-ed in the worker. This shouldn't have any security implications as Playground enables evaluating arbitrary code by definition.
  • proc_open waits for the process to spawn, and proc_close waits for the process to die. This is necessary to support the wp-cli behavior of piping its output through a pager program (like cat or less -S).

Follow-up work

  • Remove eval() from setSpawnHandler(handler). Setting spawn handler works in Node.js where PHP runs synchronously in the same process, but these spawn handlers are challenging to transfer over postMessage as they're essentially composite EventEmitters that need to accept input inside the Worker process, handle it in the client process, and transmit it back to the worker process. Intuitively, it would be pretty easy if we used MessagePort directly. It seems like Comlink as an abstraction layer makes things much easier to a point, since we can magically call functions on objects living in another thread, but there's a boundary beyond which Comlink makes these API interactions more difficult.

cc @danielbachhuber @schlessera

@swissspidy
Copy link
Member

There's a bunch of warnings, but it works! I suspect the warnings are related to PHP 8.2. There's a chance they wouldn't be there on PHP 8.0 or 7.4.

It's odd that you are seeing messages about missing ReturnTypeWillChange attributes, because those methods in core definitely do have these attributes. So this should not be happening at all.

@schlessera
Copy link
Member

However, the help message doesn't show up as wp-cli pipes it through a pager – less -S in this case. less is not available in the browser so we must handle that on our side.

WP-CLI's behavior for this can be controlled via the standard POSIX PAGER environment variable. This means you can add whatever binary you want to use for output and have WP-CLI pipe everything through it via PAGER=<binary>. A simplified version could be PAGER=cat wp --help, for example.

@adamziel
Copy link
Collaborator Author

It's odd that you are seeing messages about missing ReturnTypeWillChange attributes, because those methods in core definitely do have these attributes. So this should not be happening at all.

Aha! I bet the minification (that runs on PHP 7.4) thinks those attributes are comments and it removes them.

@adamziel
Copy link
Collaborator Author

adamziel commented Jan 19, 2024

WP-CLI's behavior for this can be controlled via the standard POSIX PAGER environment variable. This means you can add whatever binary you want to use for output and have WP-CLI pipe everything through it via PAGER=. A simplified version could be PAGER=cat wp --help, for example.

In the browser we have neither so setting the PAGER is just choosing a different name for a custom callback handler we still have to provide. Once WASI is more baked out we should be able to use the actual pager binary, though!

@adamziel
Copy link
Collaborator Author

It took me the entire day but I figured it out. proc_open needs to wait for the process to spawn, and proc_close needs to wait for the process to terminate. I just pushed a code change that does that (and fixes proc_get_status() cc @mho22). Next week I'll fix the tests and hopefully land this PR!

CleanShot 2024-01-19 at 17 24 20@2x

CleanShot 2024-01-19 at 17 23 52@2x

@adamziel adamziel marked this pull request as ready for review January 22, 2024 08:53
@adamziel adamziel requested a review from a team as a code owner January 22, 2024 08:53
adamziel added a commit that referenced this pull request Jan 22, 2024
## What is this PR doing?

A part of #957

Ensures that PHP attributes like `#[ Attr ]` are preserved when
minifying WordPress by using PHP 8.0 for minification. PHP 7.4 stripped
them out as comments.

## Testing instructions

Confirm the CI tests pass
@adamziel adamziel changed the title Explore: Support wp-cli in the browser Support wp-cli in the browser Jan 22, 2024
@adamziel adamziel merged commit ae8ec6a into trunk Jan 22, 2024
5 checks passed
@adamziel adamziel deleted the experiment-xterm branch January 22, 2024 14:32
@adamziel adamziel mentioned this pull request Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants