Skip to content
Merged
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
18 changes: 15 additions & 3 deletions packages/runtime-core/src/recipe-source-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ export function composerManagedHostEnv(): Record<string, string> {
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",
}
}
Expand Down Expand Up @@ -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[]
Expand All @@ -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,
}
Expand Down
33 changes: 32 additions & 1 deletion tests/recipe-extra-plugin-composer-preparation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
try {
await stat(path)
Expand Down
32 changes: 31 additions & 1 deletion tests/source-package-compiler-primitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
COMPOSER_SOURCE_PACKAGE_INSTALL_ARGS,
compileRecipeTemplate,
composerManagedHostCommandConfig,
composerManagedHostEnv,
fixtureImportDeterministicIdPlan,
prepareRecipeSourcePackageSync,
sourcePackagePathAllowed,
Expand All @@ -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
Expand Down
Loading