Fix plugin cache panic when cwd is unavailable (#18499)

## Summary

Fixes #16637. (I hit this bug after 11h of work on a long-running task.)

Plugin cache initialization could panic when an already-absolute cache
path was normalized through `AbsolutePathBuf::from_absolute_path`,
because that path still consulted `current_dir()`.

This changes absolute-path normalization so already-absolute paths do
not depend on cwd, and makes plugin cache root construction available as
a fallible path through `PluginStore::try_new()`. Plugin cache subpaths
now use `AbsolutePathBuf::join()` instead of re-absolutizing derived
absolute paths.
This commit is contained in:
Eric Traut
2026-04-18 19:04:53 -07:00
committed by GitHub
parent 53b1570367
commit e3f44ca3b3
5 changed files with 80 additions and 20 deletions

View File

@@ -12,6 +12,10 @@ use std::path::Path;
use std::path::PathBuf;
pub(super) fn absolutize(path: &Path) -> std::io::Result<PathBuf> {
if path.is_absolute() {
return Ok(normalize_path(path));
}
Ok(absolutize_from(path, &std::env::current_dir()?))
}

View File

@@ -328,6 +328,8 @@ mod tests {
use crate::test_support::test_path_buf;
use pretty_assertions::assert_eq;
use std::fs;
#[cfg(unix)]
use std::process::Command;
use tempfile::tempdir;
#[test]
@@ -341,6 +343,46 @@ mod tests {
assert_eq!(abs_path_buf.as_path(), absolute_path.as_path());
}
#[cfg(unix)]
#[test]
fn from_absolute_path_does_not_read_current_dir_when_path_is_absolute() {
let status = Command::new(std::env::current_exe().expect("current test binary"))
.arg("from_absolute_path_with_removed_current_dir_child")
.arg("--ignored")
.env("CODEX_ABSOLUTE_PATH_REMOVED_CWD_CHILD", "1")
.status()
.expect("run child test");
assert!(status.success());
}
#[cfg(unix)]
#[test]
#[ignore]
fn from_absolute_path_with_removed_current_dir_child() {
if std::env::var_os("CODEX_ABSOLUTE_PATH_REMOVED_CWD_CHILD").is_none() {
return;
}
let original_cwd = std::env::current_dir().expect("original cwd");
let temp_dir = tempdir().expect("temp dir");
let removed_cwd = temp_dir.path().to_path_buf();
std::env::set_current_dir(&removed_cwd).expect("enter temp dir");
std::fs::remove_dir(&removed_cwd).expect("remove current dir");
std::env::current_dir().expect_err("current dir should be unavailable");
let path = AbsolutePathBuf::from_absolute_path(test_path_buf(
"/tmp/codex/../codex-home/plugins/cache",
))
.expect("absolute path should not require current dir");
std::env::set_current_dir(original_cwd).expect("restore cwd");
assert_eq!(
path.as_path(),
test_path_buf("/tmp/codex-home/plugins/cache")
);
}
#[test]
fn from_absolute_path_checked_rejects_relative_path() {
let err = AbsolutePathBuf::from_absolute_path_checked("relative/path")