Skip to content
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

Misleading error message when crates.io is replace-with a local source #14697

Open
hoodmane opened this issue Oct 16, 2024 · 6 comments
Open
Assignees
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. A-source-replacement Area: [source] replacement C-bug Category: bug E-easy Experience: Easy S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review

Comments

@hoodmane
Copy link

hoodmane commented Oct 16, 2024

Problem

When using a local source via a replace-with in config.toml and there is a missing package, cargo build gives confusing errors saying that it searched crates-io for the package when it only looked in the local file system. The error it gives is identical to the error from an actually missing package:

error: no matching package named `<package name>` found
location searched: registry `crates-io`
required by package `example v0.1.0`

Possible Solution(s)

If you run cargo seach it gives much more specific information:

$ cargo search ahash
error: crates-io is replaced with non-remote-registry source dir /.../cargo-bug/vendor;
include `--registry crates-io` to use crates.io

It would be great if the original error included this "crates-io is replaced with non-remote-registry source" remark.

Steps

  1. Unpack this zip archive: cargo-bug.zip
  2. Run cargo build in root
  3. Get error:
    error: no matching package named `ahasha` found
    location searched: registry `crates-io`
    required by package `example v0.1.0 (/home/rchatham/Documents/programming/tmp/cargo-bug)`
    

As an alternative, instead of unpacking, make an empty folder, cd into it, make the following files and then do the following shell instructions:

Cargo.toml

[package]
name = "example"
version = "0.1.0"
edition = "2021"

[dependencies]
ahash = { version = "^0.8.9" }

.cargo/config.toml

[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"

Shell instructions

Then:

mkdir src
touch src/lib.rs
mkdir vendor
cargo build

Version

cargo 1.84.0-nightly (15fbd2f 2024-10-08)
release: 1.84.0-nightly
commit-hash: 15fbd2f
commit-date: 2024-10-08
host: x86_64-unknown-linux-gnu
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w 11 Sep 2023
os: Ubuntu 22.4.0 (jammy) [64-bit]

@hoodmane hoodmane added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Oct 16, 2024
@weihanglo
Copy link
Member

Thanks for the report.

It might be improved by using a similar mechanism you pointed out. The relevant code cargo-search handling error resides here and here.

We might want a simpler message for resolver errors, such as

error: no matching package named `ahash` found
location searched: registry `crates-io` (replaced with registry `/path/to/vendor`)
required by package `example v0.1.0 (/home/rchatham/Documents/programming/tmp/cargo-bug)`

Calling SourceId::display_registry_name should give a good enough descriptive name of the source I believe.

@weihanglo weihanglo added E-easy Experience: Easy A-diagnostics Area: Error and warning messages generated by Cargo itself. A-source-replacement Area: [source] replacement S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review A-registry-authentication Area: registry authentication and authorization (authn authz) and removed S-triage Status: This issue is waiting on initial triage. A-registry-authentication Area: registry authentication and authorization (authn authz) labels Oct 17, 2024
@chentodd
Copy link

Hi,

I did some research on the issue the used Registry::describe_source in resolver/errors to get replacement message, and it showed following messages when building cargo-bug.zip:

error:` no matching package named `ahasha` found
location searched: registry `crates-io`, directory source `/home/todd/Desktop/cargo-bug/vendor` (which is replacing registry `crates-io`)
required by package `example v0.1.0 (/home/todd/Desktop/cargo-bug)`

Since I'm new to both open source and Rust, I'm not sure if this is the right way to approach the issue, and I would greatly appreciate any feedback.

Thanks.

@weihanglo
Copy link
Member

Feel free to claim this issue and submit a pull request!

Personally I lean toward keeping the message concise, and prefer showing the replaced source first. Other may have different preference.

@rustbot rustbot assigned duncpro and unassigned duncpro Oct 19, 2024
@chentodd
Copy link

@rustbot claim

@weihanglo
Copy link
Member

I did some research on the issue the used Registry::describe_source in resolver/errors to get replacement message, and it showed following messages when building cargo-bug.zip:

error:` no matching package named `ahasha` found
location searched: registry `crates-io`, directory source `/home/todd/Desktop/cargo-bug/vendor` (which is replacing registry `crates-io`)
required by package `example v0.1.0 (/home/todd/Desktop/cargo-bug)`

I just understood what you meant. We already have such a message, so probably we can just apply this a simple fix, and update all test snapshots.

diff --git a/src/cargo/core/resolver/errors.rs b/src/cargo/core/resolver/errors.rs
index 7fbf4e22c0e..64503b9401a 100644
--- a/src/cargo/core/resolver/errors.rs
+++ b/src/cargo/core/resolver/errors.rs
@@ -355,3 +355,6 @@ pub(super) fn activation_error(
         }
-        msg.push_str(&format!("location searched: {}\n", dep.source_id()));
+        msg.push_str(&format!(
+            "location searched {}\n",
+            registry.describe_source(dep.source_id())
+        ));
         msg.push_str("required by ");

@chentodd
Copy link

Thanks, I'll follow the advice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. A-source-replacement Area: [source] replacement C-bug Category: bug E-easy Experience: Easy S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review
Projects
None yet
Development

No branches or pull requests

4 participants