CLI: Set PLAYGROUND_AUTO_LOGIN_AS_USER at boot time for all workers when --login is used#3485
CLI: Set PLAYGROUND_AUTO_LOGIN_AS_USER at boot time for all workers when --login is used#3485
Conversation
…login is set When the CLI's --login flag is true, inject the PLAYGROUND_AUTO_LOGIN_AS_USER constant into the merged constants so it flows through the boot-time path alongside other --define constants. This ensures the auto-login constant is set before the server starts accepting requests, rather than relying on the blueprint login step which runs on a single pool-proxied worker. An explicit --define PLAYGROUND_AUTO_LOGIN_AS_USER override is respected. Made-with: Cursor
Previously, mergeDefinedConstants was only passed to bootWordPress which runs on a single pool-proxied worker. While the shared nativeInternalDirPath means consts.json is visible across workers, feeding constants through bootRequestHandler ensures they are applied at PHP instance creation time and survive runtime rotation without relying on file-level sharing. This also fixes --define/--define-bool/--define-number for V1 workers that were not the one selected by the pool proxy for bootWordPress. Made-with: Cursor
Covers: login=true injects the constant, login=false/undefined does not, explicit --define override is preserved, and coexistence with other constants. Made-with: Cursor
The TODO to make defineConstant apply to all workers is addressed by injecting the constant at boot time via mergeDefinedConstants. Also corrects the mu-plugin filename from 0-auto-login.php to 1-auto-login.php. Made-with: Cursor
…ia --define When the user provides a custom auto-login username via --define, the boot-time constant from mergeDefinedConstants already handles auto-login. The blueprint login step compiles login:true to username:'admin' and calls defineConstant which would overwrite the custom value in consts.json. Skip the step entirely so the user's --define value is respected. Made-with: Cursor
brandonpayton
left a comment
There was a problem hiding this comment.
@ashfame I'm not sure what to make of this yet. Do we know for sure this is the cause of login flakiness observed in E2E tests? Do we even have E2E tests for Playground CLI? I don't think so, but sometimes I forget things.
AFAICT, the fact that consts.json is shared across PHP-WASM/node workers should be sufficient to define PLAYTOUN_AUTO_LOGIN_AS_USER because consts.json is read within the auto_prepend_script every time PHP is run:
wordpress-playground/packages/php-wasm/universal/src/lib/php.ts
Lines 335 to 354 in fbbe653
The same could be said of Playground on the web which has a single Web Worker with multiple PHP-WASM/web instances. In that case, there is a primary PHP-WASM instance that has a MEMFS filesystem that all secondary PHP-WASM instances access via PROXYFS. So those instances should also be getting the constants for free due to the auto_prepend_script.
If there is indeed flaky auto-login in E2E tests, it might be due to something else.
If there is real flakiness with Playground CLI E2E tests, one place I might wonder about is this bit which tries to clear cookies from previous Playground CLI sessions:
wordpress-playground/packages/playground/cli/src/run-cli.ts
Lines 1622 to 1645 in fbbe653
On another topic:
Regarding defines.ts, I think it would be good to keep that module as generic as possible, without knowledge of specific constants.
Motivation for the change, related issues
When the CLI runs
server --login, the blueprintloginstep callsplayground.defineConstant('PLAYGROUND_AUTO_LOGIN_AS_USER', username)oncethrough the pool proxy. There was an existing TODO acknowledging a potential
issue with this:
In practice, all CLI workers share a native
/internal/directory(
nativeInternalDirPath), so theconsts.jsonfile written bydefineConstantis visible across workers and the login flakiness is hardto reproduce manually. However, the constant is set during blueprint step
execution rather than at PHP boot time. Moving it into the boot-time
constants path:
requests, rather than relying on the shared filesystem being written to
during blueprint execution
requests a new PHP instance is created via
createPhp, which appliesoptions.constants— the proper boot-time path)login.tsAdditionally, the V1 handler's per-worker
bootRequestHandlerwas notforwarding
--define/--define-bool/--define-numberconstants atall. Those constants only reached the single worker that the pool proxy
selected for
bootWordPress. This PR fixes that gap so every V1 workergets CLI-defined constants at PHP instance creation time.
Implementation details
packages/playground/cli/src/defines.tsmergeDefinedConstantsnow accepts an optionalloginbooleanloginis true, injectsPLAYGROUND_AUTO_LOGIN_AS_USER: 'admin'(matching the blueprint login step's default)
--define PLAYGROUND_AUTO_LOGIN_AS_USERoverridepackages/playground/cli/src/blueprints-v1/blueprints-v1-handler.tsconstants: mergeDefinedConstants(this.args)to each worker'sbootRequestHandler, so every worker gets CLI-defined constants at PHPinstance creation time
packages/playground/cli/src/blueprints-v1/worker-thread-v1.tsconstantstoWorkerBootRequestHandlerOptionsconstantsto the@wp-playground/wordpressbootRequestHandler,which applies them in
createPhpfor every PHP instance (including afterruntime rotation)
packages/playground/blueprints/src/lib/steps/login.ts0-auto-login.phpto1-auto-login.phpV2 already passes
mergeDefinedConstants(this.args)per-worker inbootWorker, so thedefines.tschange is sufficient there.Testing Instructions (or ideally a Blueprint)
Unit tests (added in this PR):
Covers: login=true injects the constant, login=false/undefined does not,
explicit
--defineoverride is preserved, and coexistence with otherconstants.
Existing tests (verify no regressions):
Code review notes:
The login flakiness from the missing constant has been observed in E2E tests
but is difficult to reproduce manually, because CLI workers share a native
/internal/directory and theconsts.jsonfile written bydefineConstantis visible across all workers. The primary value of this change is
architectural correctness: constants now flow through the proper boot-time
path (
bootRequestHandler→createPhp) rather than relying on ashared-filesystem side effect of the blueprint
loginstep. This also fixes--defineconstant propagation to all V1 workers, which had the same gap.