From b1f74fd83663d26e14f92300f452a3c997d93d62 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 15 Aug 2026 08:56:04 +0900 Subject: fix(php-rpc): unpack the runtime bundle under the cache dir A worker whose PHP cannot read the bundle out of the executable gets it from an unpacked copy, which went to a directory derived from XDG_CACHE_HOME alone. That ignored COMPOSER_CACHE_DIR, COMPOSER_HOME and the cache-dir setting, and put the files outside the directory clear-cache and the platform conventions cover. The callers now pass Composer's configured cache directory down to base_path(). Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-php-rpc/src/composer_runtime.rs | 34 +++++++++----------------- 1 file changed, 11 insertions(+), 23 deletions(-) (limited to 'crates/shirabe-php-rpc/src') diff --git a/crates/shirabe-php-rpc/src/composer_runtime.rs b/crates/shirabe-php-rpc/src/composer_runtime.rs index db52b776..d5024fad 100644 --- a/crates/shirabe-php-rpc/src/composer_runtime.rs +++ b/crates/shirabe-php-rpc/src/composer_runtime.rs @@ -22,22 +22,24 @@ const OVERRIDE_ENV: &str = "SHIRABE_COMPOSER_PHP_DIR"; /// The path the Composer PHP runtime's files sit under in the worker: the checkout `OVERRIDE_ENV` /// names, or else the bundle, either inside this executable or in the directory it was extracted -/// to. -pub fn base_path() -> anyhow::Result { +/// to. A bundle the worker cannot read in place is unpacked under `cache_dir`, Composer's cache +/// directory; the process answers with the path it resolved first, so a later call's `cache_dir` +/// no longer moves the runtime. +pub fn base_path(cache_dir: &std::path::Path) -> anyhow::Result { static BASE: std::sync::OnceLock> = std::sync::OnceLock::new(); - BASE.get_or_init(|| resolve().map_err(|e| format!("{e:#}"))) + BASE.get_or_init(|| resolve(cache_dir).map_err(|e| format!("{e:#}"))) .clone() .map_err(|e| anyhow::anyhow!(e)) } -fn resolve() -> anyhow::Result { +fn resolve(cache_dir: &std::path::Path) -> anyhow::Result { if let Some(directory) = override_directory()? { return path_to_string(directory); } if worker_opens_bundle()? { return Ok(format!("phar://{ALIAS}")); } - path_to_string(extract()?) + path_to_string(extract_into(&cache_dir.join("runtime"))?) } fn path_to_string(directory: std::path::PathBuf) -> anyhow::Result { @@ -129,12 +131,8 @@ impl LocalFile { } } -/// Unpacks the bundle into a content-addressed directory, so that a worker that cannot read the -/// bundle in place gets the same files from the filesystem. -fn extract() -> anyhow::Result { - extract_into(&cache_directory()?.join("shirabe").join("runtime")) -} - +/// Unpacks the bundle into a content-addressed directory under `root`, so that a worker that +/// cannot read the bundle in place gets the same files from the filesystem. fn extract_into(root: &std::path::Path) -> anyhow::Result { let destination = root.join(BUNDLE_ID); if destination.is_dir() { @@ -159,17 +157,6 @@ fn extract_into(root: &std::path::Path) -> anyhow::Result { Ok(destination) } -fn cache_directory() -> anyhow::Result { - if let Some(directory) = std::env::var_os("XDG_CACHE_HOME") - && !directory.is_empty() - { - return Ok(std::path::PathBuf::from(directory)); - } - let home = std::env::var_os("HOME") - .ok_or_else(|| anyhow::anyhow!("neither XDG_CACHE_HOME nor HOME is set"))?; - Ok(std::path::Path::new(&home).join(".cache")) -} - #[cfg(test)] mod tests { use super::*; @@ -212,7 +199,8 @@ mod tests { return; } - assert_eq!(base_path().unwrap(), format!("phar://{ALIAS}")); + let cache = tempfile::tempdir().unwrap(); + assert_eq!(base_path(cache.path()).unwrap(), format!("phar://{ALIAS}")); } /// The other half of `base_path`: a worker whose PHP cannot open the bundle is handed the -- cgit v1.3.1-4-g156e