-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error handling when PathSource is relative #14854
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @weihanglo (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
@@ -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(|| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pull request. This change looks reasonable, although it is not immediately clear to when this would happen. Could you provide a reproducible example of it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah here's the code I was using to test this. It's a newly initialised cargo project with cargo and anyhow as a dependencies. (Also attached as tarball if useful cargo-path-source-error.tar.gz)
use std::{env::current_dir, path::Path};
use cargo::{
core::{Features, Workspace},
util::toml_mut::{
dependency::{PathSource, Source},
manifest::LocalManifest,
},
GlobalContext,
};
fn main() -> anyhow::Result<()> {
let cd = current_dir()?;
let gctx = GlobalContext::default().unwrap();
let feat = Features::default();
// Assume we're running via `cargo run` and current_dir is a cargo project just for the sake of
// example
let ws = Workspace::new(&cd.join("Cargo.toml"), &gctx)?;
let mut m = LocalManifest::try_new(&cd.join("Cargo.toml"))?;
let d = m
.get_dependency_versions("cargo", &ws, &feat)
.collect::<Vec<_>>();
for (_, dep) in d {
let dep = dep?;
// Errors because "../cargo is relative".
// Path should be absolute, cargo then does its own
// processing to make it relative to the Workspace.
// Error doesn't actually happen until call to insert_into_table().
let s = Source::Path(PathSource::new(Path::new("../cargo")));
let dep = dep.clone().set_source(s);
m.insert_into_table(&["dependencies".to_string()], &dep, &gctx, &cd, &feat)?;
}
Ok(())
}
Against cargo master
morganamilo@songbird ~git/foo % c r -q
thread 'main' panicked at /Users/morganamilo/git/cargo/src/cargo/util/toml_mut/dependency.rs:751:67:
both paths are absolute
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Against this PR
morganamilo@songbird ~git/foo % c r -q
Error: path comparison requires two absolute paths; path_source: `../cargo`, workspace_path: `/Users/morganamilo/git/foo`
I still don't think the error message is the best but I thought it would be best to follow the error messages used elsewhere to keep the PR simple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the reproduction! Actually, I was expecting a bug in cargo
the executable, but it turns out from the use of cargo
as a dependency. Just remind that depending on cargo
as a library has drawbacks, and it serves mainly for the cargo
official executable.
If it were a bug in cargo
binary itself, we should better provide an actionable message and tell people how to fix it. In this case, the PathSource::path
is expected to be an absolute path. An expect
call is a good balance between code complexity and traceable message for maintainers, hence no extra context here.
Instead of adding extra context, I slightly lean towards caller passing absolute path to PathSource::new
, as there is no action users could take. Granted, the doc comment of PathSource::new
didn't mention this requirement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, some other usages of diff_paths
may not need extra anyhow contexts either IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, if that's the philosophy of libcargo I'll change it back to except and just fix the error message to not be the opposite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Merging.
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. This commit changes the error message to say what is actually wrong.
4e9055c
to
6b74183
Compare
Update cargo and books 10 commits in 66221abdeca2002d318fde6efff516aab091df0e..4c39aaff66862cc0da52fe529aa1990bb8bb9a22 2024-11-19 21:30:02 +0000 to 2024-11-25 16:36:17 +0000 - feat: Stabilize Edition 2024 (rust-lang/cargo#14828) - Improve error handling when PathSource is relative (rust-lang/cargo#14854) - test: address test output nondeterminism (rust-lang/cargo#14855) - chore: move supports-unicode to workspace deps (rust-lang/cargo#14853) - Check build target supports std when building with -Zbuild-std=std (rust-lang/cargo#14183) - fix(publish): Allow dry-run of a non-bumped workspace (rust-lang/cargo#14847) - test: Switch from 'exec_with_output' to 'run' (rust-lang/cargo#14848) - test(rustflags): Verify -Cmetadata directly, not through -Cextra-filename (rust-lang/cargo#14846) - chore: remove bors mentions (rust-lang/cargo#14845) - Clarify how `cargo::metadata` env var is selected (rust-lang/cargo#14842) ## nomicon 1 commits in eac89a3cbe6c4714e5029ae8b5a1c556fd4e8c42..0674321898cd454764ab69702819d39a919afd68 2024-11-16 14:05:28 +0000 to 2024-11-19 12:35:48 +0000 - races: Clarify a “mostly” that might be misread (rust-lang/nomicon#468) ## reference 12 commits in 41ccb0e6478305401dad92e8fd3d04a4304edb4c..5c86c739ec71b8bc839310ff47fa94e94635bba9 2024-11-15 21:45:16 +0000 to 2024-11-25 17:23:35 +0000 - Document `gen` keyword as reserved in Rust 2024 (rust-lang/reference#1501) - 2024: Update `expr` macro fragment specifier (rust-lang/reference#1639) - Add rust_2024 prelude (rust-lang/reference#1552) - 2024: Add reserved syntax (rust-lang/reference#1652) - Add Lifetime Capture Rules 2024 (rust-lang/reference#1601) - Add a section dedicated to Edition 2024 changes to temporary scopes (rust-lang/reference#1592) - 2024: Add unsafe attribute differences (rust-lang/reference#1579) - 2024: Add updates for unsafe extern blocks (rust-lang/reference#1565) - Fix rule for lazy boolean temporary drop scope (rust-lang/reference#1681) - Raw lifetimes (rust-lang/reference#1603) - Fix some missing emdashes (rust-lang/reference#1676) - Added an additional example of lifetime elision (rust-lang/reference#1678) ## rustc-dev-guide 6 commits in b679e71..787b416 2024-11-18 16:18:15 +0800 to 2024-11-22 11:17:57 +0000 - Remove constants section as it is outdated - Flatten generic parameter defs section - Add instructions to test error code docs (rust-lang/rustc-dev-guide#2145) - Reorganize the "Source Code Representation" chapters (rust-lang/rustc-dev-guide#2142) - Make `Diag` a clickable link in Suggestion section (rust-lang/rustc-dev-guide#2140) - update for rustc_intrinsic_const_stable_indirect (rust-lang/rustc-dev-guide#2138) ## edition-guide 6 commits in 915f9b319c2823f310430ecdecd86264a7870d7e..f48b0e842a3911c63240e955d042089e9e0894c7 2024-11-06 07:23:07 +0000 to 2024-11-25 16:20:27 +0000 - Update for 2024 stabilization (rust-lang/edition-guide#338) - Enable triagebot merge-conflicts and shortcuts (rust-lang/edition-guide#336) - Organize the 2024 chapters into sub-chapters by category (rust-lang/edition-guide#334) - Fix broken Cargo Book link in cargo-resolver.md (rust-lang/edition-guide#335) - Edition 2024 guide for temporary lifetime changes (rust-lang/edition-guide#318) - 2024: rustfmt sorting (rust-lang/edition-guide#332)
Update cargo and books 10 commits in 66221abdeca2002d318fde6efff516aab091df0e..4c39aaff66862cc0da52fe529aa1990bb8bb9a22 2024-11-19 21:30:02 +0000 to 2024-11-25 16:36:17 +0000 - feat: Stabilize Edition 2024 (rust-lang/cargo#14828) - Improve error handling when PathSource is relative (rust-lang/cargo#14854) - test: address test output nondeterminism (rust-lang/cargo#14855) - chore: move supports-unicode to workspace deps (rust-lang/cargo#14853) - Check build target supports std when building with -Zbuild-std=std (rust-lang/cargo#14183) - fix(publish): Allow dry-run of a non-bumped workspace (rust-lang/cargo#14847) - test: Switch from 'exec_with_output' to 'run' (rust-lang/cargo#14848) - test(rustflags): Verify -Cmetadata directly, not through -Cextra-filename (rust-lang/cargo#14846) - chore: remove bors mentions (rust-lang/cargo#14845) - Clarify how `cargo::metadata` env var is selected (rust-lang/cargo#14842) ## nomicon 1 commits in eac89a3cbe6c4714e5029ae8b5a1c556fd4e8c42..0674321898cd454764ab69702819d39a919afd68 2024-11-16 14:05:28 +0000 to 2024-11-19 12:35:48 +0000 - races: Clarify a “mostly” that might be misread (rust-lang/nomicon#468) ## reference 12 commits in 41ccb0e6478305401dad92e8fd3d04a4304edb4c..5c86c739ec71b8bc839310ff47fa94e94635bba9 2024-11-15 21:45:16 +0000 to 2024-11-25 17:23:35 +0000 - Document `gen` keyword as reserved in Rust 2024 (rust-lang/reference#1501) - 2024: Update `expr` macro fragment specifier (rust-lang/reference#1639) - Add rust_2024 prelude (rust-lang/reference#1552) - 2024: Add reserved syntax (rust-lang/reference#1652) - Add Lifetime Capture Rules 2024 (rust-lang/reference#1601) - Add a section dedicated to Edition 2024 changes to temporary scopes (rust-lang/reference#1592) - 2024: Add unsafe attribute differences (rust-lang/reference#1579) - 2024: Add updates for unsafe extern blocks (rust-lang/reference#1565) - Fix rule for lazy boolean temporary drop scope (rust-lang/reference#1681) - Raw lifetimes (rust-lang/reference#1603) - Fix some missing emdashes (rust-lang/reference#1676) - Added an additional example of lifetime elision (rust-lang/reference#1678) ## rustc-dev-guide 6 commits in b679e71..787b416 2024-11-18 16:18:15 +0800 to 2024-11-22 11:17:57 +0000 - Remove constants section as it is outdated - Flatten generic parameter defs section - Add instructions to test error code docs (rust-lang/rustc-dev-guide#2145) - Reorganize the "Source Code Representation" chapters (rust-lang/rustc-dev-guide#2142) - Make `Diag` a clickable link in Suggestion section (rust-lang/rustc-dev-guide#2140) - update for rustc_intrinsic_const_stable_indirect (rust-lang/rustc-dev-guide#2138) ## edition-guide 6 commits in 915f9b319c2823f310430ecdecd86264a7870d7e..f48b0e842a3911c63240e955d042089e9e0894c7 2024-11-06 07:23:07 +0000 to 2024-11-25 16:20:27 +0000 - Update for 2024 stabilization (rust-lang/edition-guide#338) - Enable triagebot merge-conflicts and shortcuts (rust-lang/edition-guide#336) - Organize the 2024 chapters into sub-chapters by category (rust-lang/edition-guide#334) - Fix broken Cargo Book link in cargo-resolver.md (rust-lang/edition-guide#335) - Edition 2024 guide for temporary lifetime changes (rust-lang/edition-guide#318) - 2024: rustfmt sorting (rust-lang/edition-guide#332)
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.