Use absolute path fallback instead of file: URI

Drop the `url` crate dependency. When a path can't be relativized
against the source root, emit it as a bare absolute path and let the
CLI's SARIF generator handle URI conversion downstream.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Paolo Tranquilli
2026-05-13 10:28:27 +02:00
parent 57ac0192c0
commit c3cf7c2bca
3 changed files with 7 additions and 22 deletions

1
Cargo.lock generated
View File

@@ -456,7 +456,6 @@ dependencies = [
"tree-sitter",
"tree-sitter-json",
"tree-sitter-ql",
"url",
"yeast",
"zstd",
]

View File

@@ -18,7 +18,6 @@ lazy_static = "1.5.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
chrono = { version = "0.4.42", features = ["serde"] }
url = "2.5"
num_cpus = "1.17.0"
zstd = "0.13.3"
yeast = { path = "../yeast" }

View File

@@ -4,22 +4,15 @@ use std::{
};
/// Given an absolute path, returns a relative path if it's under `source_root`,
/// otherwise a properly-encoded `file:` URI. This is used for diagnostic locations, which
/// otherwise the absolute path as-is. This is used for diagnostic locations, which
/// should use relative paths per the CodeQL diagnostic message format spec.
/// Absolute path fallback is handled downstream by the CLI's SARIF generator.
pub fn relativize_for_diagnostic(path: &Path, source_root: Option<&Path>) -> String {
source_root
.and_then(|root| path.strip_prefix(root).ok())
.and_then(|rel| rel.to_str())
.map(|s| s.to_owned())
.unwrap_or_else(|| path_to_file_uri(path))
}
/// Convert a path to a `file:` URI, using the `url` crate for proper percent-encoding.
/// Falls back to a simple `file://` prefix if the `url` crate can't handle the path.
fn path_to_file_uri(path: &Path) -> String {
url::Url::from_file_path(path)
.map(|u| u.to_string())
.unwrap_or_else(|()| format!("file://{}", path.display()))
.unwrap_or_else(|| path.display().to_string())
}
/// This represents the minimum supported path transformation that is needed to support extracting
@@ -256,23 +249,17 @@ mod tests {
}
#[test]
fn relativize_outside_source_root_produces_file_uri() {
fn relativize_outside_source_root_returns_absolute() {
let path = Path::new("/other/location/foo.rb");
let result = relativize_for_diagnostic(path, Some(Path::new("/home/runner/work/repo")));
assert_eq!(result, "file:///other/location/foo.rb");
assert_eq!(result, "/other/location/foo.rb");
}
#[test]
fn relativize_no_source_root_produces_file_uri() {
fn relativize_no_source_root_returns_absolute() {
let path = Path::new("/home/runner/work/repo/src/foo.rb");
let result = relativize_for_diagnostic(path, None);
assert_eq!(result, "file:///home/runner/work/repo/src/foo.rb");
}
#[test]
fn path_to_file_uri_encodes_spaces() {
let result = path_to_file_uri(Path::new("/home/user/my project/foo.rb"));
assert_eq!(result, "file:///home/user/my%20project/foo.rb");
assert_eq!(result, "/home/runner/work/repo/src/foo.rb");
}
#[test]