diff --git a/packages/runtime-core/src/recipe-source-packages.ts b/packages/runtime-core/src/recipe-source-packages.ts index 09d1979d..295a9bb3 100644 --- a/packages/runtime-core/src/recipe-source-packages.ts +++ b/packages/runtime-core/src/recipe-source-packages.ts @@ -315,7 +315,11 @@ export function composerManagedHostEnv(): Record { const home = process.env.HOME || homedir() return { ...(home ? { HOME: home } : {}), - ...(process.env.COMPOSER_HOME ? { COMPOSER_HOME: process.env.COMPOSER_HOME } : home ? { COMPOSER_HOME: join(home, ".composer") } : {}), + // Let Composer resolve its HOME/XDG defaults unless the host overrides them. + ...Object.fromEntries( + ["COMPOSER_HOME", "COMPOSER_CACHE_DIR", "XDG_CONFIG_HOME", "XDG_CACHE_HOME"] + .flatMap((key) => process.env[key] === undefined ? [] : [[key, process.env[key]!]]), + ), COMPOSER_MIRROR_PATH_REPOS: "1", } } @@ -346,6 +350,14 @@ export const COMPOSER_SOURCE_PACKAGE_INSTALL_ARGS: readonly string[] = [ "--no-plugins", ] +const DEFAULT_COMPOSER_TIMEOUT_MS = 10 * 60_000 + +function composerTimeoutMs(): number { + const value = process.env.WP_CODEBOX_COMPOSER_TIMEOUT_MS ?? "" + const timeoutMs = /^\d+$/.test(value) ? Number(value) : NaN + return Number.isSafeInteger(timeoutMs) && timeoutMs > 0 ? timeoutMs : DEFAULT_COMPOSER_TIMEOUT_MS +} + export function composerManagedHostCommandConfig(options: { cwd: string allowedCwdRoots: string[] @@ -360,8 +372,8 @@ export function composerManagedHostCommandConfig(options: { cwd: options.cwd, env: composerManagedHostEnv(), allowedCwdRoots: options.allowedCwdRoots, - inheritedEnv: ["HOME", "COMPOSER_HOME"], - timeoutMs: options.timeoutMs, + inheritedEnv: ["HOME", "COMPOSER_HOME", "COMPOSER_CACHE_DIR", "XDG_CONFIG_HOME", "XDG_CACHE_HOME"], + timeoutMs: options.timeoutMs ?? composerTimeoutMs(), maxOutputBytes: options.maxOutputBytes ?? 1024 * 1024 * 10, label: options.label, } diff --git a/tests/recipe-extra-plugin-composer-preparation.test.ts b/tests/recipe-extra-plugin-composer-preparation.test.ts index 644fc007..d20c99c9 100644 --- a/tests/recipe-extra-plugin-composer-preparation.test.ts +++ b/tests/recipe-extra-plugin-composer-preparation.test.ts @@ -5,11 +5,42 @@ import { join } from "node:path" import { promisify } from "node:util" import { cleanupRecipePreparedSources, materializeComposerPathRepositoryPackages, prepareRecipeExtraPlugins } from "../packages/cli/src/recipe-sources.js" -import { assertWorkspaceRecipeJsonSchema, type WorkspaceRecipe } from "../packages/runtime-core/src/index.js" +import { assertWorkspaceRecipeJsonSchema, composerManagedHostCommandConfig, type WorkspaceRecipe } from "../packages/runtime-core/src/index.js" import { withTempDir } from "../scripts/test-kit.js" const execFile = promisify(execFileCallback) +// Extra-plugin preparation and source-package hydration share this config. +const originalComposerTimeout = process.env.WP_CODEBOX_COMPOSER_TIMEOUT_MS +const composerPreparationOptions = { + cwd: process.cwd(), + allowedCwdRoots: [process.cwd()], + label: "prepare Composer autoload for recipe extra plugin", +} +try { + delete process.env.WP_CODEBOX_COMPOSER_TIMEOUT_MS + assert.equal(composerManagedHostCommandConfig(composerPreparationOptions).timeoutMs, 10 * 60_000, + "recipe extra-plugin Composer preparation must default to ten minutes") + + process.env.WP_CODEBOX_COMPOSER_TIMEOUT_MS = "900000" + assert.equal(composerManagedHostCommandConfig(composerPreparationOptions).timeoutMs, 900_000, + "operators must be able to override the Composer budget") + assert.equal(composerManagedHostCommandConfig({ ...composerPreparationOptions, timeoutMs: 120_000 }).timeoutMs, 120_000, + "an explicit caller timeout must retain precedence") + + for (const invalid of ["", "0", "-1", "1.5", "60000ms", "1e5", "Infinity", "NaN", "9007199254740992"]) { + process.env.WP_CODEBOX_COMPOSER_TIMEOUT_MS = invalid + assert.equal(composerManagedHostCommandConfig(composerPreparationOptions).timeoutMs, 10 * 60_000, + `invalid Composer timeout ${JSON.stringify(invalid)} must use the default`) + } +} finally { + if (originalComposerTimeout === undefined) { + delete process.env.WP_CODEBOX_COMPOSER_TIMEOUT_MS + } else { + process.env.WP_CODEBOX_COMPOSER_TIMEOUT_MS = originalComposerTimeout + } +} + async function pathExists(path: string): Promise { try { await stat(path) diff --git a/tests/source-package-compiler-primitives.test.ts b/tests/source-package-compiler-primitives.test.ts index 66058005..d3679042 100644 --- a/tests/source-package-compiler-primitives.test.ts +++ b/tests/source-package-compiler-primitives.test.ts @@ -6,6 +6,7 @@ import { COMPOSER_SOURCE_PACKAGE_INSTALL_ARGS, compileRecipeTemplate, composerManagedHostCommandConfig, + composerManagedHostEnv, fixtureImportDeterministicIdPlan, prepareRecipeSourcePackageSync, sourcePackagePathAllowed, @@ -28,9 +29,38 @@ assert.equal(template.recipe.artifacts?.paths?.[0]?.path, "/workspace/fixtures/p const composerPolicy = composerManagedHostCommandConfig({ cwd: process.cwd(), allowedCwdRoots: [process.cwd()], label: "composer policy test" }) assert.equal(composerPolicy.command, "composer") -assert.deepEqual(composerPolicy.inheritedEnv, ["HOME", "COMPOSER_HOME"]) +assert.deepEqual(composerPolicy.inheritedEnv, ["HOME", "COMPOSER_HOME", "COMPOSER_CACHE_DIR", "XDG_CONFIG_HOME", "XDG_CACHE_HOME"]) assert.equal(composerPolicy.allowedCwdRoots?.[0], process.cwd()) +const composerEnvKeys = ["HOME", "COMPOSER_HOME", "COMPOSER_CACHE_DIR", "XDG_CONFIG_HOME", "XDG_CACHE_HOME"] +const originalComposerEnv = Object.fromEntries(composerEnvKeys.map((key) => [key, process.env[key]])) +try { + for (const key of composerEnvKeys) delete process.env[key] + process.env.HOME = "/isolated-home" + assert.deepEqual(composerManagedHostEnv(), { + HOME: "/isolated-home", + COMPOSER_MIRROR_PATH_REPOS: "1", + }, "Composer must resolve its own defaults without a synthesized COMPOSER_HOME") + + for (const key of composerEnvKeys.slice(1)) { + process.env[key] = `/host-overrides/${key}` + } + const expectedEnv = { + ...Object.fromEntries(composerEnvKeys.map((key) => [key, process.env[key]])), + COMPOSER_MIRROR_PATH_REPOS: "1", + } + assert.deepEqual(composerManagedHostEnv(), expectedEnv, "explicit Composer and XDG directories must pass through") + assert.deepEqual(composerManagedHostCommandConfig({ + cwd: process.cwd(), allowedCwdRoots: [process.cwd()], label: "composer environment test", + }).env, expectedEnv, "host commands must receive the Composer and XDG overrides") +} finally { + for (const key of composerEnvKeys) { + const original = originalComposerEnv[key] + if (original === undefined) delete process.env[key] + else process.env[key] = original + } +} + // Hydration produces an autoloader; it must never execute package-supplied // code, and must not depend on the host's `allow-plugins` configuration. // Without `--no-plugins`, any `"type": "wordpress-plugin"` package requiring