Skip to content

Commit

Permalink
Improve error handling when PathSource is relative
Browse files Browse the repository at this point in the history
When editing dependencies with cargo, if a relative PathSource is
supplied cargo panics with "both paths are absolute". This is the
opposite of what's actually wrong leading to confusion.

Instead, use the same error formatting we use in other diff_paths calls
and return that error instead of panicking.
  • Loading branch information
Morganamilo committed Nov 23, 2024
1 parent 88edf01 commit 4e9055c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/cargo/util/toml_mut/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::borrow::Cow;
use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};

use anyhow::Context;
use cargo_util_schemas::manifest::PathBaseName;
use indexmap::IndexSet;
use itertools::Itertools;
Expand Down Expand Up @@ -748,7 +749,13 @@ fn path_field<'a>(
} else {
Cow::Borrowed(crate_root)
};
let relpath = pathdiff::diff_paths(&source.path, relative_to).expect("both paths are absolute");
let relpath = pathdiff::diff_paths(&source.path, &relative_to).with_context(|| {
format!(
"path comparison requires two absolute paths; path_source: `{}`, workspace_path: `{}`",
source.path.display(),
relative_to.display()
)
})?;
let relpath = relpath.to_str().unwrap().replace('\\', "/");
Ok(relpath)
}
Expand Down

0 comments on commit 4e9055c

Please sign in to comment.