Skip to content

Commit 7a0d268

Browse files
fix(compile): normalize absolute input paths in generate_header_comment (#645)
* fix(compile): normalize absolute input paths in generate_header_comment When users invoke `ado-aw compile /absolute/path/to/agents/foo.md`, strip the current working directory prefix so the stored source in the @ado-aw header is a relative path (e.g. `agents/foo.md`). This ensures --source filters and auto-discovery recompile work correctly regardless of how the compiler was invoked. Also adds two new unit tests covering absolute paths under CWD. Agent-Logs-Url: https://github.com/githubnext/ado-aw/sessions/847594d9-3bf7-4873-b14c-e072979774b6 Co-authored-by: jamesadevine <4742697+jamesadevine@users.noreply.github.com> * fix(compile): fix normalised → normalized spelling in comment Agent-Logs-Url: https://github.com/githubnext/ado-aw/sessions/847594d9-3bf7-4873-b14c-e072979774b6 Co-authored-by: jamesadevine <4742697+jamesadevine@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jamesadevine <4742697+jamesadevine@users.noreply.github.com>
1 parent 7d197f1 commit 7a0d268

1 file changed

Lines changed: 49 additions & 4 deletions

File tree

src/compile/common.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,12 +1216,29 @@ pub const HEADER_MARKER: &str = "# @ado-aw";
12161216
/// - A human-readable "do not edit" warning
12171217
/// - A machine-readable `@ado-aw` marker with source path and compiler version
12181218
///
1219-
/// The source path is the input path as provided to the compiler (e.g., `agents/my-agent.md`,
1220-
/// `.azdo/pipelines/review.md`, or any other location the user chose). Path separators
1221-
/// are normalized to forward slashes for cross-platform consistency.
1219+
/// The source path is stored as a relative path so that `--source` filters
1220+
/// and auto-discovery recompile work regardless of how the user invoked the
1221+
/// compiler (relative path, absolute path, etc.). Path separators are
1222+
/// normalized to forward slashes for cross-platform consistency.
12221223
pub fn generate_header_comment(input_path: &std::path::Path) -> String {
12231224
let version = env!("CARGO_PKG_VERSION");
1224-
let mut source_path = input_path
1225+
1226+
// If the caller supplied an absolute path (e.g. `ado-aw compile
1227+
// /repo/agents/foo.md`), make it relative to the current working directory
1228+
// so that `--source agents/foo.md` filters can match it. When the path is
1229+
// not under the CWD (unusual), fall back to the original path rather than
1230+
// silently producing a wrong value.
1231+
let relative: std::borrow::Cow<std::path::Path> = if input_path.is_absolute() {
1232+
std::env::current_dir()
1233+
.ok()
1234+
.and_then(|cwd| input_path.strip_prefix(&cwd).ok().map(|p| p.to_path_buf()))
1235+
.map(std::borrow::Cow::Owned)
1236+
.unwrap_or(std::borrow::Cow::Borrowed(input_path))
1237+
} else {
1238+
std::borrow::Cow::Borrowed(input_path)
1239+
};
1240+
1241+
let mut source_path = relative
12251242
.to_string_lossy()
12261243
.replace('\\', "/")
12271244
.replace(['\n', '\r'], "")
@@ -4405,6 +4422,34 @@ mod tests {
44054422
);
44064423
}
44074424

4425+
#[test]
4426+
fn test_generate_header_comment_absolute_path_under_cwd() {
4427+
// Build an absolute path by joining CWD with a relative agent path.
4428+
// generate_header_comment should strip the CWD prefix so the stored
4429+
// source remains relative (matching what --source filters expect).
4430+
let cwd = std::env::current_dir().expect("current dir");
4431+
let abs_path = cwd.join("agents/my-agent.md");
4432+
let header = generate_header_comment(&abs_path);
4433+
assert!(
4434+
header.contains(r#"source="agents/my-agent.md""#),
4435+
"Absolute path under CWD should be stored as relative: {}",
4436+
header
4437+
);
4438+
}
4439+
4440+
#[test]
4441+
fn test_generate_header_comment_absolute_path_subdir() {
4442+
// Absolute path that is nested several directories deep under CWD.
4443+
let cwd = std::env::current_dir().expect("current dir");
4444+
let abs_path = cwd.join(".azdo/pipelines/review.md");
4445+
let header = generate_header_comment(&abs_path);
4446+
assert!(
4447+
header.contains(r#"source=".azdo/pipelines/review.md""#),
4448+
"Nested absolute path should be stored as relative: {}",
4449+
header
4450+
);
4451+
}
4452+
44084453
// ─── generate_source_path ────────────────────────────────────────────────
44094454

44104455
#[test]

0 commit comments

Comments
 (0)