Skip to content

WIP: wrote 3 tests about improving the error message in the path dependency#16138

Draft
ibilalkayy wants to merge 4 commits intorust-lang:masterfrom
ibilalkayy:path_dependency_msg_improvement
Draft

WIP: wrote 3 tests about improving the error message in the path dependency#16138
ibilalkayy wants to merge 4 commits intorust-lang:masterfrom
ibilalkayy:path_dependency_msg_improvement

Conversation

@ibilalkayy
Copy link
Copy Markdown
Contributor

@ibilalkayy ibilalkayy commented Oct 22, 2025

What does this PR try to resolve?

This PR is trying to improve the error message when the wrong package is found in the path dependency. Currently the PR started from the tests.

How to test and review this PR?

There are 3 tests added that cover each case that is brought up in this issue.

  • invalid_package_name_in_path()

    • It reads the main manifest file to find that the definitely_not_bar exists in the crates/bar and it goes to the crates/bar to find that it is not present there and it gives the error.
  • invalid_package_in_subdirectory()

    • It reads the main manifest file to find that the definitely_not_bar exists in the crates/bar and after going there, it does not find it. Then it goes more deeper in the definitely_not_bar to find that the manifest file exists there and give use the helpful message.
  • invalid_manifest_in_path()

    • It reads the main manifest file to find that definitely_not_bar exists in the crates/bar but after going there, it finds that there are other two packages but definitely_not_bar is not present and it gives the message.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 22, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Oct 22, 2025

r? @ehuss

rustbot has assigned @ehuss.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@epage epage marked this pull request as draft October 22, 2025 14:20
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 22, 2025
Comment thread tests/testsuite/path.rs Outdated
Comment thread tests/testsuite/path.rs Outdated
Comment thread tests/testsuite/path.rs Outdated
@epage epage changed the title wrote 3 tests about improving the error message in the path dependency WIP: wrote 3 tests about improving the error message in the path dependency Oct 22, 2025
@ibilalkayy ibilalkayy force-pushed the path_dependency_msg_improvement branch from 2a4dc4d to 2bf90ff Compare October 29, 2025 14:15
Comment thread tests/testsuite/path.rs Outdated
@ibilalkayy ibilalkayy force-pushed the path_dependency_msg_improvement branch from 2bf90ff to 4d041ea Compare October 29, 2025 16:29
@rustbot rustbot added A-cli-help Area: built-in command-line help A-completions Area: shell completions A-documenting-cargo-itself Area: Cargo's documentation A-timings Area: timings Command-test labels Oct 29, 2025
@rustbot

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. label Oct 29, 2025
@ibilalkayy
Copy link
Copy Markdown
Contributor Author

ibilalkayy commented Oct 29, 2025

@epage I noticed that there were 7 spaces missing on both lines. So I added them and the test got passed.

I have squashed and pushed the code, but these spaces are not showing. Maybe it's a GitHub thing.

There is a conflict that came after your changes. Should I resolve the error by keeping this function or remove it?

function get_codegen_section_x(sections) {...}

@ibilalkayy ibilalkayy force-pushed the path_dependency_msg_improvement branch 3 times, most recently from a12c57e to 9c538bb Compare November 5, 2025 17:00
@rustbot rustbot added the A-dependency-resolution Area: dependency resolution and the resolver label Nov 26, 2025
Comment thread src/cargo/core/resolver/errors.rs Outdated
use super::context::ResolverContext;
use super::types::{ConflictMap, ConflictReason};

fn debug_source_path() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these are for local debugging purpose, please remove.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was showing where they are at in experimenting while they then work to change this to fit in

Comment thread tests/testsuite/path.rs Outdated
edition = "2015"
authors = []

[workspace]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is [workspace] here essential? If not maybe we should remove.

Comment thread src/cargo/core/resolver/errors.rs Outdated
let gctx = GlobalContext::default().unwrap();
let sid = SourceId::for_path(path).unwrap();

fn debug_source_path(msg: &mut String, path: &Path, gctx: &GlobalContext, sid: SourceId) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should go below where they are used so that the first item in the file is the starting point for reading it and going further down just adds more context

@ibilalkayy ibilalkayy force-pushed the path_dependency_msg_improvement branch from a9a8dc1 to 9695b25 Compare January 13, 2026 07:23
&mut msg,
"no matching package named `{}` found",
dep.package_name()
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved from out of the else block, into it.

@ibilalkayy ibilalkayy force-pushed the path_dependency_msg_improvement branch 2 times, most recently from 196d02a to a025762 Compare February 18, 2026 17:23
@ibilalkayy
Copy link
Copy Markdown
Contributor Author

ibilalkayy commented Mar 5, 2026

Hey @epage, I have now written my doubts and confusion in the comment that you can review it.

Here is the code where I wrote comment: https://github.com/rust-lang/cargo/pull/16138/changes

There is one functionality about the invalid_manifest_in_path remaining that needs to be written and called out

Plus I am not sure why the tests are passing locally but failing here with these two functions.

@ibilalkayy ibilalkayy requested a review from epage March 5, 2026 23:22
@ibilalkayy ibilalkayy force-pushed the path_dependency_msg_improvement branch 2 times, most recently from 754b8ad to 94cd203 Compare March 6, 2026 18:54
Comment thread tests/testsuite/path.rs
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] no matching package named `bar` found at `definitely_not_bar`
[NOTE] required by package `foo v0.5.0 ([ROOT]/foo)`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this test is failing. Tests should be atomic which includes tests passing. This should be showing the existing behavior so that the following commit then changes that behavior so we can see the new behavior.

Comment thread src/cargo/core/resolver/errors.rs Outdated

let global_context = match gctx {
Some(context) => context,
None => &GlobalContext::new(Shell::new(), PathBuf::new(), PathBuf::new()),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems suspicious for us to create global contexts on the fly

Comment thread src/cargo/core/resolver/errors.rs Outdated
Comment on lines +414 to +417
&mut msg,
"no matching package named `{}` found at `{}`",
name.name,
dep.package_name()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammatically, this reads in an odd way.

"at" in a situation like this implies a location which would likely to be assumed to be a path but to say "package foo not found in package bar" is odd because they are both packages and a package cannot have packages inside of it.

I would instead recommend having a note naming the package that does exist at a specific path

Comment thread src/cargo/core/resolver/errors.rs Outdated
let root_package =
inspect_root_package(&mut msg, Path::new(&path), global_context, sid);

if let Some(name) = root_package {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if a root package is not found?

Comment thread src/cargo/core/resolver/errors.rs Outdated
) -> Option<RootPackageInfo> {
let mut ps = PathSource::new(path, sid, gctx);

ps.root_package().expect("failed to get the root");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expects are an assert and should name the condition that is upholding this ("I expect foo to be true")

Comment thread src/cargo/core/resolver/errors.rs Outdated
.source_id()
.url()
.to_file_path()
.expect("[ERROR]: failed to get the path");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expects are an assert and should name the condition that is upholding this ("I expect foo to be true")

Comment thread src/cargo/core/resolver/errors.rs Outdated

let pkg = ps
.read_package()
.expect("failed to read the package in root");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we getting the root package, throwing it away, then calling read_package?

Comment thread src/cargo/core/resolver/errors.rs Outdated
.read_package()
.expect("failed to read the package in root");

let package_info = RootPackageInfo {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a struct for this?

Comment thread src/cargo/core/resolver/errors.rs Outdated
Comment on lines +426 to +429
let _ = writeln!(
&mut msg,
"help: package `{}` exists at `{}`",
name.name, name.name
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The at is giving the package name

Comment thread src/cargo/core/resolver/errors.rs Outdated
Comment on lines +454 to +457
let _ = writeln!(
&mut msg,
"help: package `{}` exists at `{}`",
name.name, name.name
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

_path: PathBuf,
}

fn inspect_recursive_packages(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this name doesn't say what it is doing

Comment thread src/cargo/core/resolver/errors.rs Outdated
Comment on lines +624 to +628
msg: &mut String,
path: &Path,
gctx: &GlobalContext,
sid: SourceId,
) -> Option<RootPackageInfo> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit unusual to have a function like this also modify msg

Comment thread src/cargo/core/resolver/errors.rs Outdated
}

fn inspect_recursive_packages(
msg: &mut String,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit unusual to have a function like this also modify msg

Comment thread src/cargo/core/resolver/errors.rs Outdated
} else if let Some(name) = recursive_packages {
let _ = writeln!(
&mut msg,
"no matching package named `{}` found at `{}`",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We sure are repeating this a lot

Comment thread src/cargo/core/resolver/errors.rs Outdated
let manifest = pkg.manifest_path();
let pkg_dir = manifest
.parent()
.expect("failed to take the parent path")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto about expect

Comment thread src/cargo/core/resolver/errors.rs Outdated
Comment on lines +717 to +719
// } else {
// let list = rps.list_files(&pkg).unwrap();
// println!("{:?}", list);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code

Comment thread src/cargo/core/resolver/errors.rs Outdated
Comment on lines +711 to +714
let package_info = RecursivePackageInfo {
name: pkg.name().to_string(),
_path: pkg_dir,
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a struct?

@ibilalkayy ibilalkayy force-pushed the path_dependency_msg_improvement branch from 94cd203 to f216f9b Compare March 18, 2026 11:13
@ibilalkayy ibilalkayy force-pushed the path_dependency_msg_improvement branch from f892af2 to 951c119 Compare March 22, 2026 07:06
@ibilalkayy
Copy link
Copy Markdown
Contributor Author

ibilalkayy commented Mar 22, 2026

@epage As you have said, I should divide the tests into atomic commits by separating the actual and expected behaviour. I did it for the root package, and it worked, but for the recursive packages, the test is failing by asking me to write the actual behavior message, even though I want to change it.

The problem I noticed is that the logic is not even triggering the recursive package messages, and even if I make any change in the logic, it still gives the same old message of the actual behaviour. This thing prevents me from moving forward even after making multiple changes like

  1. flipping the conditional statement b/w root and the recursive package.
  2. instead of writing Some(package_name) = root_package and Some((package_name, package_path)) = recursive_packages under the last else statement, I continued the flow by putting more else if statements for these two conditions and wrote the else statement afterwards.

Both of these approaches didn't work. Plus, I also made the changes in the code as you said.

@ibilalkayy ibilalkayy requested a review from epage March 22, 2026 07:18
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 27, 2026

☔ The latest upstream changes (possibly #16795) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cli-help Area: built-in command-line help A-completions Area: shell completions A-dependency-resolution Area: dependency resolution and the resolver A-documenting-cargo-itself Area: Cargo's documentation A-timings Area: timings Command-test S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants