Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,52 @@ jobs:
# E2E Playwright tests - builds inline per runner for faster parallel execution
# Previously used a prepare job + artifact sharing, but inline builds allow
# all shards to start immediately in parallel, reducing total wall time.
test-e2e-reprint-import:
if: github.repository == 'WordPress/wordpress-playground' || github.event_name == 'pull_request'
runs-on: ubuntu-latest
timeout-minutes: 40
permissions:
contents: read # Required to clone the repo.
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: ./.github/actions/prepare-playground
- name: Install PHP for the source site
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: sqlite3, pdo_sqlite, gd, mbstring, curl, zip, xml
tools: none
- name: Install Playwright Browser
run: sudo npx playwright install chromium --with-deps
- name: Build app for E2E tests
run: CORS_PROXY_URL=http://127.0.0.1:5263/cors-proxy.php? npx nx e2e:playwright:prepare-app-deploy-and-offline-mode playground-website
- name: Serve a WordPress site with Reprint Server
run: |
bash packages/playground/website/playwright/reprint-site/serve.sh > /tmp/reprint-site.log 2>&1 &
for _ in $(seq 1 120); do
if curl -sf http://127.0.0.1:8181/playground-e2e-manifest.json > /dev/null; then
exit 0
fi
sleep 2
done
echo "The source site did not come up:"
cat /tmp/reprint-site.log
exit 1
- name: Run Playwright reprint-import tests
run: sudo CI=true PLAYWRIGHT_TEST_GROUP=reprint-import REPRINT_E2E_SITE_URL=http://127.0.0.1:8181/ npx playwright test --config=packages/playground/website/playwright/playwright.ci.config.ts --project=chromium
- name: Source site log
if: ${{ failure() }}
run: cat /tmp/reprint-site.log
- uses: actions/upload-artifact@v4
if: ${{ failure() }}
with:
name: playwright-report-reprint-import
path: packages/playground/website/playwright-report/
if-no-files-found: ignore
retention-days: 7

test-e2e-playwright:
if: github.repository == 'WordPress/wordpress-playground' || github.event_name == 'pull_request'
runs-on: ubuntu-latest
Expand Down
113 changes: 113 additions & 0 deletions packages/php-wasm/compile/php/emscripten-sleep.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const { readFileSync, globSync } = require('node:fs');
const { join } = require('node:path');
const { runInNewContext } = require('node:vm');
const { describe, it } = require('node:test');
const assert = require('node:assert/strict');

// Test the compiler input and the checked-in loaders: a source-only change
// would leave existing browser downloads using the slow timer path.
const librarySource = readFileSync(
join(__dirname, 'phpwasm-emscripten-library.js'),
'utf8'
);
const implementations = [
[
'compiler library',
`${librarySource}\nLibraryManager.library.emscripten_sleep;`,
],
...globSync('*/*/php_*.js', {
cwd: join(__dirname, '../../web-builds'),
}).map((path) => {
const source = readFileSync(
join(__dirname, '../../web-builds', path),
'utf8'
);
const start = source.indexOf('var _emscripten_sleep =');
const end = source.indexOf('_emscripten_sleep.sig', start);
assert.ok(start !== -1 && end > start, `Missing sleep in ${path}`);
return [path, `${source.slice(start, end)}\n_emscripten_sleep;`];
}),
];

for (const [name, source] of implementations) {
describe(name, () => {
it('yields zero-delay sleeps to a task, keeping the runtime alive', () => {
const runtime = createRuntime(source);
runtime.sleep(0);
assert.equal(runtime.tasks.length, 1);
assert.equal(runtime.timers.length, 0);
assert.deepEqual(runtime.events, ['keepalive']);
runtime.tasks.shift()();
assert.deepEqual(runtime.events, [
'keepalive',
'release',
'callback',
'wake',
]);
});

it('keeps positive sleeps on the existing timer path', () => {
const runtime = createRuntime(source);
runtime.sleep(125);
assert.equal(runtime.tasks.length, 0);
assert.equal(runtime.timers.length, 1);
assert.equal(runtime.timers[0].delay, 125);
runtime.timers[0].callback();
assert.deepEqual(runtime.events, ['wake']);
});

for (const scheduler of [undefined, {}]) {
it(`uses the timer when postTask is unavailable (${JSON.stringify(scheduler)})`, () => {
const runtime = createRuntime(source, scheduler);
runtime.sleep(0);
assert.equal(runtime.tasks.length, 0);
assert.equal(runtime.timers.length, 1);
assert.equal(runtime.timers[0].delay, 0);
runtime.timers[0].callback();
assert.deepEqual(runtime.events, ['wake']);
});
}

it('lets callUserCallback skip resuming an aborted runtime', () => {
const runtime = createRuntime(source);
runtime.sleep(0);
runtime.context.callUserCallback = () =>
runtime.events.push('aborted');
runtime.tasks.shift()();
assert.deepEqual(runtime.events, [
'keepalive',
'release',
'aborted',
]);
});
});
}

function createRuntime(source, ...schedulerOverride) {
const events = [];
const tasks = [];
const timers = [];
const context = {
LibraryManager: { library: {} },
mergeInto: Object.assign,
autoAddDeps() {},
Asyncify: { handleSleep: (start) => start(() => events.push('wake')) },
safeSetTimeout: (callback, delay) => timers.push({ callback, delay }),
runtimeKeepalivePush: () => events.push('keepalive'),
runtimeKeepalivePop: () => events.push('release'),
callUserCallback: (callback) => {
events.push('callback');
callback();
},
scheduler: schedulerOverride.length
? schedulerOverride[0]
: { postTask: (callback) => tasks.push(callback) },
};
return {
sleep: runInNewContext(source, context),
context,
events,
tasks,
timers,
};
}
26 changes: 26 additions & 0 deletions packages/php-wasm/compile/php/phpwasm-emscripten-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@
'use strict';

const LibraryExample = {
emscripten_sleep__deps: [
'$Asyncify',
'$safeSetTimeout',
'$runtimeKeepalivePush',
'$runtimeKeepalivePop',
'$callUserCallback',
],
emscripten_sleep__sig: 'vi',
emscripten_sleep__async: true,
emscripten_sleep: function (ms) {
return Asyncify.handleSleep((wakeUp) => {
// select() yields between socket reads. Nested zero-delay timers are
// clamped by browsers, adding seconds to even a local curl download.
// Use a task (not a microtask) so network events still get a turn.
if (ms === 0 && globalThis.scheduler?.postTask) {
runtimeKeepalivePush();
globalThis.scheduler.postTask(() => {
runtimeKeepalivePop();
callUserCallback(wakeUp);
});
} else {
safeSetTimeout(wakeUp, ms);
}
});
},

// Emscripten dependencies:
$PHPWASM__deps: ['$allocateUTF8OnStack', '$addOnInit'],
$PHPWASM__postset: 'PHPWASM.init();',
Expand Down
10 changes: 10 additions & 0 deletions packages/php-wasm/compile/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
"name": "php-wasm-compile",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"targets": {
"test": {
"executor": "nx:run-commands",
"inputs": [
"default",
"{workspaceRoot}/packages/php-wasm/web-builds/*/*/php_*.js"
],
"options": {
"command": "node --test packages/php-wasm/compile/php/emscripten-sleep.test.cjs"
}
},
"compile": {
"executor": "nx:run-commands",
"options": {
Expand Down
39 changes: 38 additions & 1 deletion packages/php-wasm/fs-journal/src/lib/fs-journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,45 @@ export function normalizeFilesystemOperations(
let journal = input;
while (true) {
const substitutions: Record<number, any> = {};
// Most download records only affect one file. Looking up that path
// avoids comparing every chunk write with every other file in the site.
// Rebuild after each substitution pass because renames change paths.
const entriesByPath = new Map<string, number[]>();
const directoryRenames: number[] = [];
for (let i = 0; i < journal.length; i++) {
const entry = journal[i];
const path =
entry.operation === 'RENAME' ? entry.toPath : entry.path;
if (!entriesByPath.has(path)) entriesByPath.set(path, []);
entriesByPath.get(path)!.push(i);
if (
entry.operation === 'RENAME' &&
entry.nodeType === 'directory'
) {
directoryRenames.push(i);
}
}
for (let i = journal.length - 1; i >= 0; i--) {
for (let j = i - 1; j >= 0; j--) {
const entry = journal[i];
let candidates: number[];
if (
entry.operation === 'RENAME' &&
entry.nodeType === 'directory'
) {
// Moving a directory may rewrite any earlier descendant path.
candidates = Array.from({ length: i }, (_, j) => j);
} else {
candidates = entriesByPath.get(entry.path) ?? [];
if (entry.operation === 'RENAME') {
// Keep detecting unsupported rename chains through a parent.
candidates = [
...new Set([...candidates, ...directoryRenames]),
];
}
}
// Substitutions depend on visiting earlier records newest-first.
for (const j of [...candidates].sort((a, b) => b - a)) {
if (j >= i) continue;
const formerType = checkRelationship(journal[i], journal[j]);
if (formerType === 'none') {
continue;
Expand Down
27 changes: 27 additions & 0 deletions packages/php-wasm/fs-journal/src/test/fs-journal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,33 @@ describe('Journal MemFS', () => {
});

describe('normalizeFilesystemOperations()', () => {
it('normalizes a large download without scanning unrelated paths for every write', () => {
// Downloads write each file in chunks. Comparing all pairs of these
// records blocks the PHP worker, including error and log delivery.
// Count path reads rather than timing the test on different machines.
let pathReads = 0;
const journal: FilesystemOperation[] = [];
const expected: FilesystemOperation[] = [];
for (let i = 0; i < 33057; i++) {
const path = `/wordpress/wp-content/uploads/file-${i}`;
for (let chunk = 0; chunk < 6; chunk++) {
journal.push({
operation: chunk === 0 ? 'CREATE' : 'WRITE',
nodeType: 'file',
get path() {
if (++pathReads > 3000000) {
throw new Error(
'Bulk download normalization scanned too many paths'
);
}
return path;
},
});
}
expected.push({ operation: 'WRITE', nodeType: 'file', path });
}
expect(normalizeFilesystemOperations(journal)).toEqual(expected);
});
it('Normalizes CREATE and WRITE + multiple WRITE file ops to a single WRITE', () => {
const expected = [
{ operation: 'WRITE', path: '/test', nodeType: 'file' },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, it, expect, vi } from 'vitest';
import { collectPhpLogs } from './collect-php-logs';
import type { UniversalPHP } from '../types';
import type { Logger } from '../logger';

describe('PHP log collection after a site import', () => {
it('reads appended records once and restarts after replacement or removal', async () => {
const logs = createCollector();
await logs.request('old log');
await logs.request('old log\nnew error');
await logs.request('old log\nnew error');
await logs.request('fatal');
await logs.request('other'); // Same length, but an imported file.
await logs.request('');
await logs.request('fatal');
expect(logs.messages()).toEqual([
'old log',
'\nnew error',
'fatal',
'other',
'fatal',
]);
});
it('does not share offsets between Playgrounds', async () => {
const first = createCollector();
const second = createCollector();
await first.request('a much longer old log');
await second.request('error');
expect(second.messages()).toEqual(['error']);
});
});

function createCollector() {
const callbacks: Record<string, () => Promise<void>> = {};
let log = '';
const logger = { logMessage: vi.fn() };
collectPhpLogs(
logger as unknown as Logger,
{
addEventListener: (name: string, callback: () => Promise<void>) => {
callbacks[name] = callback;
},
fileExists: async () => !!log,
readFileAsText: async () => log,
} as unknown as UniversalPHP
);
return {
request: async (next: string) => {
log = next;
await callbacks['request.end']();
},
messages: () =>
logger.logMessage.mock.calls.map(([record]) => record.message),
};
}
12 changes: 8 additions & 4 deletions packages/php-wasm/logger/src/lib/collectors/collect-php-logs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { UniversalPHP, PHPRequestErrorEvent } from '../types';
import { type Logger, LogPrefix, LogSeverity } from '../logger';

let lastPHPLogLength = 0;
export const errorLogPath = '/wordpress/wp-content/debug.log';

/**
Expand All @@ -26,16 +25,21 @@ export const collectPhpLogs = (
loggerInstance: Logger,
playground: UniversalPHP
) => {
let lastPHPLog = '';
playground.addEventListener('request.end', async () => {
const log = await getRequestPhpErrorLog(playground);
if (log.length > lastPHPLogLength) {
const currentLog = log.substring(lastPHPLogLength);
// Imports can replace debug.log with a shorter or unrelated file. Track
// each Playground separately and only skip a prefix we actually read.
const currentLog = log.startsWith(lastPHPLog)
? log.substring(lastPHPLog.length)
: log;
lastPHPLog = log;
if (currentLog) {
loggerInstance.logMessage({
message: currentLog,
severity: LogSeverity.Log,
raw: true,
});
lastPHPLogLength = log.length;
}
});
playground.addEventListener('request.error', (event) => {
Expand Down
Loading