WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 40086e8

Browse files
authored
fix(info): default to local without explicit reg (#16358)
### What does this PR try to resolve? close #16145 [`93c2c40` (#16358)](93c2c40) shouldn't be an error; it should inspect the local package correctly. The reason it is buggy is as follows: The `default_registry` function attempts to retrieve the default registry from the user's configuration; in this case, it is crates-io. ```rust in src/cargo/ops/registry/mod.rs:210-248(get_source_id_with_package_id): (Some(reg_or_index), Some(package_id)) => { let sid = get_initial_source_id_from_registry_or_index(gctx, reg_or_index)?; // crates-io let package_source_id = package_id.source_id(); // local path source // Check if they match or if one replaces the other if sid == package_source_id // false: crates-io != path || is_replacement_for_package_source(gctx, sid, package_source_id)? // false: no replacement for local path { (true, package_source_id) } else { (false, sid) // <-- Returns this: "don't use local package, search in registry" } } ``` In this PR, I modified the cargo-info to prefer the local package if the registry is not explicitly specified. ### How to test and review this PR? Check the unit test. 1. One case for reproducing the described issue. It shouldn't be an error. We should inspect the local package properly. 2. Another case specifies the registry configured in the config file to ensure we can search the registry correctly. r? @ghost
2 parents 6d8524a + 0984acc commit 40086e8

File tree

32 files changed

+304
-2
lines changed

32 files changed

+304
-2
lines changed

src/bin/cargo/commands/info.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
2929
let spec = PackageIdSpec::parse(package)
3030
.with_context(|| format!("invalid package ID specification: `{package}`"))?;
3131

32+
// Check if --registry or --index was explicitly provided
33+
let explicit_registry = args._contains("registry") || args._contains("index");
3234
let reg_or_index = args.registry_or_index(gctx)?;
33-
info(&spec, gctx, reg_or_index)?;
35+
info(&spec, gctx, reg_or_index, explicit_registry)?;
3436
Ok(())
3537
}

src/cargo/ops/registry/info/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub fn info(
2020
spec: &PackageIdSpec,
2121
gctx: &GlobalContext,
2222
reg_or_index: Option<RegistryOrIndex>,
23+
explicit_registry: bool,
2324
) -> CargoResult<()> {
2425
let source_config = SourceConfigMap::new(gctx)?;
2526
let mut registry = PackageRegistry::new_with_source_config(gctx, source_config)?;
@@ -39,8 +40,17 @@ pub fn info(
3940
.and_then(|path| ws.members().find(|p| p.manifest_path() == path))
4041
});
4142
let (mut package_id, is_member) = find_pkgid_in_ws(nearest_package, ws.as_ref(), spec);
43+
44+
// If a local package exists and no explicit registry/index was provided,
45+
// prefer the local package over the default registry
46+
let reg_or_index_to_use = if package_id.is_some() && !explicit_registry {
47+
None
48+
} else {
49+
reg_or_index.as_ref()
50+
};
51+
4252
let (use_package_source_id, source_ids) =
43-
get_source_id_with_package_id(gctx, package_id, reg_or_index.as_ref())?;
53+
get_source_id_with_package_id(gctx, package_id, reg_or_index_to_use)?;
4454
// If we don't use the package's source, we need to query the package ID from the specified registry.
4555
if !use_package_source_id {
4656
package_id = None;

tests/testsuite/cargo_info/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ mod specify_version_within_ws_and_conflict_with_lockfile;
1717
mod specify_version_within_ws_and_match_with_lockfile;
1818
mod transitive_dependency_within_ws;
1919
mod verbose;
20+
mod with_default_registry_configured;
21+
mod with_default_registry_configured_and_specified;
2022
mod with_frozen_outside_ws;
2123
mod with_frozen_within_ws;
2224
mod with_locked_outside_ws;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[registry]
2+
default = "crates-io"

tests/testsuite/cargo_info/with_default_registry_configured/in/Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[workspace]
2+
members = ["crates/*"]
3+
4+
[package]
5+
name = "foo"
6+
version = "0.0.0"
7+
8+
[dependencies]
9+
crate1 = { path = "./crates/crate1" }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "crate1"
3+
version = "0.0.0"
4+
5+
[dependencies]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::prelude::*;
2+
use cargo_test_support::{Project, compare::assert_ui, current_dir, file};
3+
4+
use super::init_registry_without_token;
5+
6+
#[cargo_test]
7+
fn case() {
8+
init_registry_without_token();
9+
10+
let project = Project::from_template(current_dir!().join("in"));
11+
let project_root = project.root();
12+
let cwd = &project_root;
13+
14+
snapbox::cmd::Command::cargo_ui()
15+
.arg("info")
16+
.arg_line("--verbose foo")
17+
.current_dir(cwd)
18+
.assert()
19+
.success()
20+
.stdout_eq(file!["stdout.term.svg"])
21+
.stderr_eq("");
22+
23+
assert_ui().subset_matches(current_dir!().join("out"), &project_root);
24+
}

0 commit comments

Comments
 (0)