Skip to content

Commit a7cad67

Browse files
[CLI] Fix __dirname not defined error in intl extension (#3094)
## Motivation for the change, related issues Running `npx @wp-playground/cli server --debug` currently fails for v3.0.38. ``` Unhandled rejection: ReferenceError: __dirname is not defined at withIntl (file:///Users/brandon/.npm/_npx/8ab79f7a2a3f35a4/node_modules/@php-wasm/node/index.js:485:20) at async loadNodeRuntime (file:///Users/brandon/.npm/_npx/8ab79f7a2a3f35a4/node_modules/@php-wasm/node/index.js:611:25) at async Object.createPhpRuntime (file:///Users/brandon/.npm/_npx/8ab79f7a2a3f35a4/node_modules/@wp-playground/cli/worker-thread-v1.js:138:48) at async t (file:///Users/brandon/.npm/_npx/8ab79f7a2a3f35a4/node_modules/@wp-playground/wordpress/index.js:451:15) at async PHPProcessManager.phpFactory (file:///Users/brandon/.npm/_npx/8ab79f7a2a3f35a4/node_modules/@php-wasm/universal/index.js:2559:21) at async PHPProcessManager.doSpawn (file:///Users/brandon/.npm/_npx/8ab79f7a2a3f35a4/node_modules/@php-wasm/universal/index.js:2363:17) at async PHPProcessManager.getPrimaryPhp (file:///Users/brandon/.npm/_npx/8ab79f7a2a3f35a4/node_modules/@php-wasm/universal/index.js:2289:135) at async PHPRequestHandler.getPrimaryPhp (file:///Users/brandon/.npm/_npx/8ab79f7a2a3f35a4/node_modules/@php-wasm/universal/index.js:2585:12) at async L (file:///Users/brandon/.npm/_npx/8ab79f7a2a3f35a4/node_modules/@wp-playground/wordpress/index.js:384:13) at async V (file:///Users/brandon/.npm/_npx/8ab79f7a2a3f35a4/node_modules/@wp-playground/wordpress/index.js:380:10) ``` I think our tests for built npm packages should have caught this before merging, so I would like to look into that before merging this fix. cc @mho22 ## Implementation details Use conditional logic to determine the module directory: prefer __dirname when available (CommonJS), fall back to import.meta.dirname (ESM). Also switch to path.join for more robust path construction. ## Testing Instructions (or ideally a Blueprint) - CI
1 parent 5895ec4 commit a7cad67

File tree

2 files changed

+5
-2
lines changed
  • packages

2 files changed

+5
-2
lines changed

packages/php-wasm/node/src/lib/extensions/intl/with-intl.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
} from '@php-wasm/universal';
66
import { LatestSupportedPHPVersion, FSHelpers } from '@php-wasm/universal';
77
import fs from 'fs';
8+
import path from 'path';
89
import { getIntlExtensionModule } from './get-intl-extension-module';
910

1011
export async function withIntl(
@@ -16,7 +17,9 @@ export async function withIntl(
1617
const extension = fs.readFileSync(extensionPath);
1718

1819
const dataName = 'icu.dat';
19-
const dataPath = `${__dirname}/shared/${dataName}`;
20+
const moduleDir =
21+
typeof __dirname !== 'undefined' ? __dirname : import.meta.dirname;
22+
const dataPath = path.join(moduleDir, 'shared', dataName);
2023
const ICUData = fs.readFileSync(dataPath);
2124

2225
return {

packages/playground/test-built-npm-packages/es-modules-and-vitest/run-tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,6 @@ if (numTimedOut > 0) {
117117
console.log(red(`${numTimedOut} / ${results.length} tests timed out`));
118118
}
119119

120-
if (numFailed > 0) {
120+
if (numFailed > 0 || numTimedOut > 0) {
121121
process.exit(1);
122122
}

0 commit comments

Comments
 (0)