From 54c3ef1b61211cbc4195dc35710bf7b1ee0e77e9 Mon Sep 17 00:00:00 2001 From: Thibault Sottiaux Date: Thu, 8 Jan 2026 09:36:24 -0800 Subject: [PATCH] fix: handle file cwd in project doc discovery --- codex-rs/core/src/project_doc.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/codex-rs/core/src/project_doc.rs b/codex-rs/core/src/project_doc.rs index 79f82c4598..8d8afef333 100644 --- a/codex-rs/core/src/project_doc.rs +++ b/codex-rs/core/src/project_doc.rs @@ -134,6 +134,12 @@ pub async fn read_project_docs(config: &Config) -> std::io::Result std::io::Result> { let mut dir = config.cwd.clone(); + if let Ok(metadata) = std::fs::metadata(&dir) + && metadata.is_file() + && let Some(parent) = dir.parent() + { + dir = parent.to_path_buf(); + } if let Ok(canon) = normalize_path(&dir) { dir = canon; } @@ -141,7 +147,7 @@ pub fn discover_project_doc_paths(config: &Config) -> std::io::Result = vec![dir.clone()]; let mut git_root: Option = None; - let mut cursor = dir; + let mut cursor = dir.clone(); while let Some(parent) = cursor.parent() { let git_marker = cursor.join(".git"); let git_exists = match std::fs::metadata(&git_marker) { @@ -174,7 +180,7 @@ pub fn discover_project_doc_paths(config: &Config) -> std::io::Result = Vec::new(); @@ -234,6 +240,7 @@ mod tests { use super::*; use crate::config::ConfigBuilder; use crate::skills::load_skills; + use pretty_assertions::assert_eq; use std::fs; use std::path::PathBuf; use tempfile::TempDir; @@ -393,6 +400,27 @@ mod tests { assert_eq!(res, Some(INSTRUCTIONS.to_string())); } + #[tokio::test] + async fn uses_parent_dir_when_cwd_is_file_outside_git_repo() { + let tmp = tempfile::tempdir().expect("tempdir"); + fs::write(tmp.path().join("AGENTS.md"), "doc").unwrap(); + let file_path = tmp.path().join("notes.txt"); + fs::write(&file_path, "contents").unwrap(); + + let mut cfg = make_config(&tmp, 4096, None).await; + cfg.cwd = file_path; + + let res = get_user_instructions(&cfg, None) + .await + .expect("doc expected"); + assert_eq!(res, "doc"); + + let discovery = discover_project_doc_paths(&cfg).expect("discover paths"); + let expected_path = normalize_path(tmp.path().join("AGENTS.md")) + .unwrap_or_else(|_| tmp.path().join("AGENTS.md")); + assert_eq!(discovery, vec![expected_path]); + } + /// When both the repository root and the working directory contain /// AGENTS.md files, their contents are concatenated from root to cwd. #[tokio::test]