mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:20:47 +00:00
Preserve descriptive labels on local file links (#42123)
## What changed - Render descriptive Markdown labels alongside canonical local file targets instead of discarding them. - Collapse labels that resolve to the same path, accounting for relative and absolute forms, `file://` URLs, percent encoding, case, separators, and location suffixes. - Preserve `~/` destinations for display and keep absolute paths unless they can be shortened relative to the session working directory. ## Testing - Add unit and snapshot coverage for descriptive and path-equivalent labels, invalid percent encoding, trailing separators, Unix and Windows paths, UNC paths, `file://` URLs, and table wrapping. GitOrigin-RevId: 636bf4485b2899d96c6273be3f3b7f38728c9013
This commit is contained in:
committed by
copyberry
parent
9112564114
commit
0276f2ee55
@@ -0,0 +1,299 @@
|
||||
//! Local file-link parsing, label comparison, and display for Markdown transcripts.
|
||||
//!
|
||||
//! Markdown rendering intentionally treats local file links differently from normal web links. For
|
||||
//! local paths, transcripts always show the real file target (including normalized location
|
||||
//! suffixes) and can shorten absolute paths relative to a known working directory. Descriptive
|
||||
//! Markdown labels remain visible alongside that target, while path-like labels collapse to the
|
||||
//! canonical target to avoid duplicate file references.
|
||||
//!
|
||||
|
||||
use codex_utils_string::normalize_markdown_hash_location_suffix;
|
||||
use regex_lite::Regex;
|
||||
use std::path::Path;
|
||||
use std::sync::LazyLock;
|
||||
use url::Url;
|
||||
|
||||
static COLON_LOCATION_SUFFIX_RE: LazyLock<Regex> =
|
||||
LazyLock::new(
|
||||
|| match Regex::new(r":\d+(?::\d+)?(?:[-–]\d+(?::\d+)?)?$") {
|
||||
Ok(regex) => regex,
|
||||
Err(error) => panic!("invalid location suffix regex: {error}"),
|
||||
},
|
||||
);
|
||||
|
||||
// Covered by load_location_suffix_regexes.
|
||||
static HASH_LOCATION_SUFFIX_RE: LazyLock<Regex> =
|
||||
LazyLock::new(|| match Regex::new(r"^L\d+(?:C\d+)?(?:-L\d+(?:C\d+)?)?$") {
|
||||
Ok(regex) => regex,
|
||||
Err(error) => panic!("invalid hash location regex: {error}"),
|
||||
});
|
||||
|
||||
pub(super) fn is_local_path_like_link(dest_url: &str) -> bool {
|
||||
dest_url.starts_with("file://")
|
||||
|| dest_url.starts_with('/')
|
||||
|| dest_url.starts_with("~/")
|
||||
|| dest_url.starts_with("./")
|
||||
|| dest_url.starts_with("../")
|
||||
|| dest_url.starts_with("\\\\")
|
||||
|| matches!(
|
||||
dest_url.as_bytes(),
|
||||
[drive, b':', separator, ..]
|
||||
if drive.is_ascii_alphabetic() && matches!(separator, b'/' | b'\\')
|
||||
)
|
||||
}
|
||||
|
||||
/// Decide whether a local-file link label adds meaning beyond its canonical target.
|
||||
///
|
||||
/// Matching path-like labels collapse to the target; prose labels remain visible.
|
||||
pub(super) fn should_render_local_link_label(label: &str, destination: &str) -> bool {
|
||||
let label = label.trim();
|
||||
if label.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let Some(parsed_label) = comparable_local_link_path(label) else {
|
||||
return true;
|
||||
};
|
||||
let Some(target) = comparable_local_link_path(destination) else {
|
||||
return true;
|
||||
};
|
||||
let target_path = trim_trailing_local_path_separator(target.trim_start_matches("./"));
|
||||
let has_boundary_suffix = |path: &str, suffix: &str| {
|
||||
!suffix.is_empty()
|
||||
&& path
|
||||
.strip_suffix(suffix)
|
||||
.is_some_and(|prefix| prefix.is_empty() || prefix.ends_with('/'))
|
||||
};
|
||||
// Labels can spell a filename literally or URL-encode it. Compare both without decoding
|
||||
// the destination twice (for example, percent%2520.rs denotes percent%20.rs).
|
||||
let literal_label = normalize_local_link_path_text(label).to_lowercase();
|
||||
![literal_label, parsed_label].iter().any(|label| {
|
||||
let label_path = trim_trailing_local_path_separator(label.trim_start_matches("./"));
|
||||
has_boundary_suffix(target_path, label_path)
|
||||
|| (is_absolute_local_link_path(label_path)
|
||||
&& has_boundary_suffix(label_path, target_path))
|
||||
})
|
||||
}
|
||||
|
||||
/// Normalize original Markdown strings for comparison only, never already-rendered path text.
|
||||
/// Case and URL spelling are intentionally forgiving; the visible target retains its spelling.
|
||||
fn comparable_local_link_path(text: &str) -> Option<String> {
|
||||
let text = if text
|
||||
.get(..7)
|
||||
.is_some_and(|scheme| scheme.eq_ignore_ascii_case("file://"))
|
||||
{
|
||||
std::borrow::Cow::Owned(format!("file://{}", &text[7..]))
|
||||
} else {
|
||||
std::borrow::Cow::Borrowed(text)
|
||||
};
|
||||
let (mut path, _) = parse_local_link_target(&text)?;
|
||||
if text.starts_with("file://") {
|
||||
let url = Url::parse(&text).ok()?;
|
||||
// The display parser's fallback preserves URL escapes on platforms that cannot convert
|
||||
// this URL to a native path. Decode that fallback exactly once for comparison as well.
|
||||
if url.to_file_path().is_err() {
|
||||
path = urlencoding::decode(&path)
|
||||
.unwrap_or(std::borrow::Cow::Borrowed(&path))
|
||||
.into_owned();
|
||||
}
|
||||
// Unix URL conversion retains the slash before a Windows drive; ignore it here only.
|
||||
if matches!(path.as_bytes(), [b'/', drive, b':', b'/', ..] if drive.is_ascii_alphabetic()) {
|
||||
path.remove(0);
|
||||
}
|
||||
}
|
||||
Some(normalize_local_link_path_text(&path).to_lowercase())
|
||||
}
|
||||
|
||||
/// Parse a local link target into normalized path text plus an optional location suffix.
|
||||
///
|
||||
/// This accepts the path shapes Codex emits today: `file://` URLs, absolute and relative paths,
|
||||
/// `~/...`, Windows paths, and `#L..C..` or `:line:col` suffixes.
|
||||
pub(super) fn render_local_link_target(dest_url: &str, cwd: Option<&Path>) -> Option<String> {
|
||||
let (path_text, location_suffix) = parse_local_link_target(dest_url)?;
|
||||
let mut rendered = display_local_link_path(&path_text, cwd);
|
||||
if let Some(location_suffix) = location_suffix {
|
||||
rendered.push_str(&location_suffix);
|
||||
}
|
||||
Some(rendered)
|
||||
}
|
||||
|
||||
/// Split a local-link destination into `(normalized_path_text, location_suffix)`.
|
||||
///
|
||||
/// The returned path text never includes a trailing `#L..` or `:line[:col]` suffix. Path
|
||||
/// normalization preserves `~/...` and rewrites path separators into display-stable forward
|
||||
/// slashes. The suffix, when present, is returned separately in normalized markdown form.
|
||||
///
|
||||
/// Returns `None` only when the destination looks like a `file://` URL but cannot be parsed into a
|
||||
/// local path. Plain path-like inputs always return `Some(...)` even if they are relative.
|
||||
fn parse_local_link_target(dest_url: &str) -> Option<(String, Option<String>)> {
|
||||
if dest_url.starts_with("file://") {
|
||||
let url = Url::parse(dest_url).ok()?;
|
||||
let path_text = file_url_to_local_path_text(&url)?;
|
||||
let location_suffix = url
|
||||
.fragment()
|
||||
.and_then(normalize_hash_location_suffix_fragment);
|
||||
return Some((path_text, location_suffix));
|
||||
}
|
||||
|
||||
let mut path_text = dest_url;
|
||||
let mut location_suffix = None;
|
||||
// Prefer `#L..` style fragments when both forms are present so URLs like `path#L10` do not
|
||||
// get misparsed as a plain path ending in `:10`.
|
||||
if let Some((candidate_path, fragment)) = dest_url.rsplit_once('#')
|
||||
&& let Some(normalized) = normalize_hash_location_suffix_fragment(fragment)
|
||||
{
|
||||
path_text = candidate_path;
|
||||
location_suffix = Some(normalized);
|
||||
}
|
||||
if location_suffix.is_none()
|
||||
&& let Some(suffix) = extract_colon_location_suffix(path_text)
|
||||
{
|
||||
let path_len = path_text.len().saturating_sub(suffix.len());
|
||||
path_text = &path_text[..path_len];
|
||||
location_suffix = Some(suffix);
|
||||
}
|
||||
|
||||
let decoded_path_text =
|
||||
urlencoding::decode(path_text).unwrap_or(std::borrow::Cow::Borrowed(path_text));
|
||||
Some((
|
||||
normalize_local_link_path_text(&decoded_path_text),
|
||||
location_suffix,
|
||||
))
|
||||
}
|
||||
|
||||
/// Normalize a hash fragment like `L12` or `L12C3-L14C9` into the display suffix we render.
|
||||
///
|
||||
/// Returns `None` for fragments that are not location references. This deliberately ignores other
|
||||
/// `#...` fragments so non-location hashes stay part of the path text.
|
||||
fn normalize_hash_location_suffix_fragment(fragment: &str) -> Option<String> {
|
||||
HASH_LOCATION_SUFFIX_RE
|
||||
.is_match(fragment)
|
||||
.then(|| format!("#{fragment}"))
|
||||
.and_then(|suffix| normalize_markdown_hash_location_suffix(&suffix))
|
||||
}
|
||||
|
||||
/// Extract a trailing `:line`, `:line:col`, or range suffix from a plain path-like string.
|
||||
///
|
||||
/// The suffix must occur at the end of the input; embedded colons elsewhere in the path are left
|
||||
/// alone. This is what keeps Windows drive letters like `C:/...` from being misread as locations.
|
||||
fn extract_colon_location_suffix(path_text: &str) -> Option<String> {
|
||||
COLON_LOCATION_SUFFIX_RE
|
||||
.find(path_text)
|
||||
.filter(|matched| matched.end() == path_text.len())
|
||||
.map(|matched| matched.as_str().to_string())
|
||||
}
|
||||
|
||||
/// Convert a `file://` URL into the normalized local-path text used for transcript rendering.
|
||||
///
|
||||
/// This prefers `Url::to_file_path()` for standard file URLs. When that rejects Windows-oriented
|
||||
/// encodings, we reconstruct a display path from the host/path parts so UNC paths and drive-letter
|
||||
/// URLs still render sensibly.
|
||||
fn file_url_to_local_path_text(url: &Url) -> Option<String> {
|
||||
if let Ok(path) = url.to_file_path() {
|
||||
return Some(normalize_local_link_path_text(&path.to_string_lossy()));
|
||||
}
|
||||
|
||||
// Fall back to string reconstruction for cases `to_file_path()` rejects, especially UNC-style
|
||||
// hosts and Windows drive paths encoded in URL form.
|
||||
let mut path_text = url.path().to_string();
|
||||
if let Some(host) = url.host_str()
|
||||
&& !host.is_empty()
|
||||
&& host != "localhost"
|
||||
{
|
||||
path_text = format!("//{host}{path_text}");
|
||||
} else if matches!(
|
||||
path_text.as_bytes(),
|
||||
[b'/', drive, b':', b'/', ..] if drive.is_ascii_alphabetic()
|
||||
) {
|
||||
path_text.remove(0);
|
||||
}
|
||||
|
||||
Some(normalize_local_link_path_text(&path_text))
|
||||
}
|
||||
|
||||
/// Normalize local-path text into the transcript display form.
|
||||
///
|
||||
/// Display normalization is intentionally lexical: it does not touch the filesystem, resolve
|
||||
/// symlinks, or collapse `.` / `..`. It only converts separators to forward slashes and rewrites
|
||||
/// UNC-style `\\\\server\\share` inputs into `//server/share` so later prefix checks operate on a
|
||||
/// stable representation.
|
||||
fn normalize_local_link_path_text(path_text: &str) -> String {
|
||||
// Render all local link paths with forward slashes so display and prefix stripping are stable
|
||||
// across mixed Windows and Unix-style inputs.
|
||||
if let Some(rest) = path_text.strip_prefix("\\\\") {
|
||||
format!("//{}", rest.replace('\\', "/").trim_start_matches('/'))
|
||||
} else {
|
||||
path_text.replace('\\', "/")
|
||||
}
|
||||
}
|
||||
|
||||
fn is_absolute_local_link_path(path_text: &str) -> bool {
|
||||
path_text.starts_with('/')
|
||||
|| path_text.starts_with("//")
|
||||
|| matches!(
|
||||
path_text.as_bytes(),
|
||||
[drive, b':', b'/', ..] if drive.is_ascii_alphabetic()
|
||||
)
|
||||
}
|
||||
|
||||
/// Remove trailing separators from a local path without destroying root semantics.
|
||||
///
|
||||
/// Roots like `/`, `//`, and `C:/` stay intact so callers can still distinguish "the root itself"
|
||||
/// from "a path under the root".
|
||||
fn trim_trailing_local_path_separator(path_text: &str) -> &str {
|
||||
if path_text == "/" || path_text == "//" {
|
||||
return path_text;
|
||||
}
|
||||
if matches!(path_text.as_bytes(), [drive, b':', b'/'] if drive.is_ascii_alphabetic()) {
|
||||
return path_text;
|
||||
}
|
||||
path_text.trim_end_matches('/')
|
||||
}
|
||||
|
||||
/// Strip `cwd_text` from the start of `path_text` when `path_text` is strictly underneath it.
|
||||
///
|
||||
/// Returns the relative remainder without a leading slash. If the path equals the cwd exactly, this
|
||||
/// returns `None` so callers can keep rendering the full path instead of collapsing it to an empty
|
||||
/// string.
|
||||
fn strip_local_path_prefix<'a>(path_text: &'a str, cwd_text: &str) -> Option<&'a str> {
|
||||
let path_text = trim_trailing_local_path_separator(path_text);
|
||||
let cwd_text = trim_trailing_local_path_separator(cwd_text);
|
||||
if path_text == cwd_text {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Treat filesystem roots specially so `/tmp/x` under `/` becomes `tmp/x` instead of being
|
||||
// left unchanged by the generic prefix-stripping branch.
|
||||
if cwd_text == "/" || cwd_text == "//" {
|
||||
return path_text.strip_prefix('/');
|
||||
}
|
||||
|
||||
path_text
|
||||
.strip_prefix(cwd_text)
|
||||
.and_then(|rest| rest.strip_prefix('/'))
|
||||
}
|
||||
|
||||
/// Choose the visible path text for a local link after normalization.
|
||||
///
|
||||
/// Relative paths (including `~/...`) stay relative. Absolute paths prefer cwd-relative display
|
||||
/// and otherwise stay absolute: the frontend home may differ from the execution host's home.
|
||||
/// This is display logic only, not filesystem canonicalization.
|
||||
fn display_local_link_path(path_text: &str, cwd: Option<&Path>) -> String {
|
||||
let path_text = normalize_local_link_path_text(path_text);
|
||||
if !is_absolute_local_link_path(&path_text) {
|
||||
return path_text;
|
||||
}
|
||||
|
||||
if let Some(cwd) = cwd {
|
||||
// Only the session cwd is known to refer to the execution host.
|
||||
let cwd_text = normalize_local_link_path_text(&cwd.to_string_lossy());
|
||||
if let Some(stripped) = strip_local_path_prefix(&path_text, &cwd_text) {
|
||||
return stripped.to_string();
|
||||
}
|
||||
}
|
||||
|
||||
path_text
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "local_links_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,21 @@
|
||||
//! Tests for local-link parsing and root-preserving separator normalization.
|
||||
|
||||
use super::COLON_LOCATION_SUFFIX_RE;
|
||||
use super::HASH_LOCATION_SUFFIX_RE;
|
||||
use super::trim_trailing_local_path_separator;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn load_location_suffix_regexes() {
|
||||
let _colon = &*COLON_LOCATION_SUFFIX_RE;
|
||||
let _hash = &*HASH_LOCATION_SUFFIX_RE;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trailing_separator_trimming_preserves_local_roots() {
|
||||
let paths = ["/", "//", "C:/", "dir/", "//server/share/", "C:/dir/"];
|
||||
assert_eq!(
|
||||
paths.map(trim_trailing_local_path_separator),
|
||||
["/", "//", "C:/", "dir", "//server/share", "C:/dir"]
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user