Avoid reading the current directory when resolving absolute paths (#50558)

## Why

`AbsolutePathBuf::relative_to_current_dir` previously read the current directory even for absolute paths, causing resolution to fail if the current directory had been deleted.

## What changed

Route absolute inputs through `from_absolute_path` without querying the current directory. Relative inputs continue to resolve against the current directory.

## Testing

Extend the Unix regression test to verify that `relative_to_current_dir` succeeds with a deleted current directory and produces the same normalized path as `from_absolute_path`.

GitOrigin-RevId: 6bc6cb43d9f4d57359639708c4d30524d89cfa92
This commit is contained in:
Eric Traut
2026-10-03 07:14:12 +00:00
committed by copyberry
parent 6b43e6fe1f
commit 19e554bb70
+12 -3
View File
@@ -85,6 +85,10 @@ impl AbsolutePathBuf {
/// Construct an absolute path from `path`, resolving relative paths against
/// the process current working directory.
pub fn relative_to_current_dir<P: AsRef<Path>>(path: P) -> std::io::Result<Self> {
if path.as_ref().is_absolute() {
return Self::from_absolute_path(path);
}
Ok(Self::resolve_path_against_base(
path,
std::env::current_dir()?,
@@ -428,9 +432,9 @@ mod tests {
#[cfg(unix)]
#[test]
fn from_absolute_path_does_not_read_current_dir_when_path_is_absolute() {
fn absolute_paths_do_not_read_current_dir() {
let status = Command::new(std::env::current_exe().expect("current test binary"))
.arg("from_absolute_path_with_removed_current_dir_child")
.arg("absolute_paths_with_removed_current_dir_child")
.arg("--ignored")
.env("CODEX_ABSOLUTE_PATH_REMOVED_CWD_CHILD", "1")
.status()
@@ -442,7 +446,7 @@ mod tests {
#[cfg(unix)]
#[test]
#[ignore]
fn from_absolute_path_with_removed_current_dir_child() {
fn absolute_paths_with_removed_current_dir_child() {
if std::env::var_os("CODEX_ABSOLUTE_PATH_REMOVED_CWD_CHILD").is_none() {
return;
}
@@ -458,12 +462,17 @@ mod tests {
"/tmp/codex/../codex-home/plugins/cache",
))
.expect("absolute path should not require current dir");
let relative_to_current_dir_path = AbsolutePathBuf::relative_to_current_dir(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")
);
assert_eq!(relative_to_current_dir_path, path);
}
#[test]