Skip to content

Commit 9f62061

Browse files
committed
fix(info): default to local without explicit reg
When no registry is specified in the cargo info command, default to using the local crates.io registry.
1 parent c911e65 commit 9f62061

File tree

5 files changed

+57
-35
lines changed

5 files changed

+57
-35
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/with_default_registry_configured/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ fn case() {
1616
.arg_line("--verbose foo")
1717
.current_dir(cwd)
1818
.assert()
19-
.failure()
20-
.stdout_eq("")
21-
.stderr_eq(file!["stderr.term.svg"]);
19+
.success()
20+
.stdout_eq(file!["stdout.term.svg"])
21+
.stderr_eq("");
2222

2323
assert_ui().subset_matches(current_dir!().join("out"), &project_root);
2424
}

tests/testsuite/cargo_info/with_default_registry_configured/stderr.term.svg

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)