Skip to content

Commit 373160d

Browse files
committed
Fix Windows: use dunce::canonicalize to avoid \\?\ UNC paths
Root cause: std::fs::canonicalize on Windows produces \\?\ prefixed paths that libgit2 mishandles during repository discovery and diffing, and that cause path mismatches in cargo metadata output. Fixes: - Use dunce::canonicalize instead of std::fs::canonicalize in CLI (dunce strips the \\?\ prefix when safe to do so) - Refresh git index from disk before computing diffs to pick up changes made by external git CLI commands
1 parent 6d05f64 commit 373160d

5 files changed

Lines changed: 9 additions & 19 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ clap_complete = "4"
2525
git2 = "0.20"
2626
petgraph = "0.7"
2727
colored = "3"
28+
dunce = "1"
2829
which = "7"
2930
glob = "0.3"
3031
tracing = "0.1"

crates/affected-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ anyhow.workspace = true
2424
serde_json.workspace = true
2525
serde.workspace = true
2626
colored.workspace = true
27+
dunce.workspace = true
2728
tracing.workspace = true
2829
tracing-subscriber.workspace = true
2930
indicatif.workspace = true

crates/affected-cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ fn main() -> Result<()> {
208208
.without_time()
209209
.init();
210210

211-
let root = std::fs::canonicalize(&cli.root)?;
211+
let root = dunce::canonicalize(&cli.root)?;
212212

213213
match cli.command {
214214
Commands::Test {

crates/affected-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ anyhow.workspace = true
1717
serde.workspace = true
1818
serde_json.workspace = true
1919
toml.workspace = true
20+
dunce.workspace = true
2021
git2.workspace = true
2122
petgraph.workspace = true
2223
which.workspace = true

crates/affected-core/src/git.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,9 @@ pub struct GitDiff {
99
pub repo_root: PathBuf,
1010
}
1111

12-
/// Strip the `\\?\` extended-length path prefix on Windows.
13-
/// libgit2 mishandles these paths, causing discovery and diff failures.
14-
fn normalize_path(path: &Path) -> PathBuf {
15-
#[cfg(windows)]
16-
{
17-
let s = path.to_string_lossy();
18-
if let Some(stripped) = s.strip_prefix(r"\\?\") {
19-
return PathBuf::from(stripped);
20-
}
21-
}
22-
path.to_path_buf()
23-
}
24-
2512
/// Compute changed files between a base ref and HEAD (plus uncommitted changes).
2613
pub fn changed_files(repo_path: &Path, base_ref: &str) -> Result<GitDiff> {
27-
let normalized = normalize_path(repo_path);
28-
let repo = Repository::discover(&normalized).context("Not a git repository")?;
14+
let repo = Repository::discover(repo_path).context("Not a git repository")?;
2915

3016
let repo_root = repo
3117
.workdir()
@@ -51,7 +37,9 @@ pub fn changed_files(repo_path: &Path, base_ref: &str) -> Result<GitDiff> {
5137

5238
// Refresh index from disk to pick up changes made by the git CLI
5339
let mut index = repo.index().context("Could not read index")?;
54-
index.read(true).context("Could not refresh index from disk")?;
40+
index
41+
.read(true)
42+
.context("Could not refresh index from disk")?;
5543

5644
let mut files = HashSet::new();
5745

@@ -97,8 +85,7 @@ pub fn changed_files(repo_path: &Path, base_ref: &str) -> Result<GitDiff> {
9785
/// Compute the merge-base between HEAD and the given branch.
9886
/// Returns the commit SHA as a string. Used when `--merge-base` is passed.
9987
pub fn merge_base(repo_path: &Path, branch: &str) -> Result<String> {
100-
let normalized = normalize_path(repo_path);
101-
let repo = Repository::discover(&normalized).context("Not a git repository")?;
88+
let repo = Repository::discover(repo_path).context("Not a git repository")?;
10289

10390
debug!("Computing merge-base between HEAD and '{}'", branch);
10491

0 commit comments

Comments
 (0)