Skip to content

Commit

Permalink
Auto merge of #11215 - arlosi:sparse-cfg, r=weihanglo
Browse files Browse the repository at this point in the history
Add configuration option for controlling crates.io protocol

### What does this PR try to resolve?

Currently, if `-Z sparse-registry` is passed, then Cargo will access crates.io using the sparse protocol. Since we want to stabilize this feature soon, we need a stable way to control which protocol is used.

This adds a config option `registries.crates-io.protocol` that can be set to either `sparse` or `git`. If the option is unset, it will be `sparse` if `-Z sparse-registry` is enabled, otherwise it will be `git`.

This PR does not stabilize `sparse-registry`. Using `registries.crates-io.protocol` without `-Z sparse-registry` will result in an error.

The next steps after PR are to:
* stabilize `sparse-registry`
* make `sparse` the default protocol

### Additional information
The config option is based on the discussion in this note: https://hackmd.io/`@rust-cargo-team/B13O52Zko`
  • Loading branch information
bors committed Oct 11, 2022
2 parents b8f30cb + d77aef5 commit 8e740bb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/cargo/core/source/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::core::PackageId;
use crate::sources::registry::CRATES_IO_HTTP_INDEX;
use crate::sources::{DirectorySource, CRATES_IO_DOMAIN, CRATES_IO_INDEX, CRATES_IO_REGISTRY};
use crate::sources::{GitSource, PathSource, RegistrySource};
use crate::util::{CanonicalUrl, CargoResult, Config, IntoUrl};
use crate::util::{config, CanonicalUrl, CargoResult, Config, IntoUrl};
use log::trace;
use serde::de;
use serde::ser;
Expand Down Expand Up @@ -215,7 +215,7 @@ impl SourceId {
/// Returns the `SourceId` corresponding to the main repository, using the
/// sparse HTTP index if allowed.
pub fn crates_io_maybe_sparse_http(config: &Config) -> CargoResult<SourceId> {
if config.cli_unstable().sparse_registry {
if Self::crates_io_is_sparse(config)? {
config.check_registry_index_not_set()?;
let url = CRATES_IO_HTTP_INDEX.into_url().unwrap();
SourceId::new(SourceKind::Registry, url, Some(CRATES_IO_REGISTRY))
Expand All @@ -224,6 +224,21 @@ impl SourceId {
}
}

/// Returns whether to access crates.io over the sparse protocol.
pub fn crates_io_is_sparse(config: &Config) -> CargoResult<bool> {
let proto: Option<config::Value<String>> = config.get("registries.crates-io.protocol")?;
let is_sparse = match proto.as_ref().map(|v| v.val.as_str()) {
Some("sparse") => true,
Some("git") => false,
Some(unknown) => anyhow::bail!(
"unsupported registry protocol `{unknown}` (defined in {})",
proto.as_ref().unwrap().definition
),
None => config.cli_unstable().sparse_registry,
};
Ok(is_sparse)
}

/// Gets the `SourceId` associated with given name of the remote registry.
pub fn alt_registry(config: &Config, key: &str) -> CargoResult<SourceId> {
if key == CRATES_IO_REGISTRY {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/sources/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<'cfg> SourceConfigMap<'cfg> {
replace_with: None,
},
)?;
if config.cli_unstable().sparse_registry {
if SourceId::crates_io_is_sparse(config)? {
base.add(
CRATES_IO_REGISTRY,
SourceConfig {
Expand Down
18 changes: 18 additions & 0 deletions tests/testsuite/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2703,6 +2703,24 @@ fn http_requires_z_flag() {
.run();
}

#[cargo_test]
fn protocol_sparse_requires_z_flag() {
cargo_process("install bar")
.with_status(101)
.env("CARGO_REGISTRIES_CRATES_IO_PROTOCOL", "sparse")
.with_stderr("[ERROR] usage of sparse registries requires `-Z sparse-registry`")
.run()
}

#[cargo_test]
fn protocol() {
cargo_process("install bar")
.with_status(101)
.env("CARGO_REGISTRIES_CRATES_IO_PROTOCOL", "invalid")
.with_stderr("[ERROR] unsupported registry protocol `invalid` (defined in environment variable `CARGO_REGISTRIES_CRATES_IO_PROTOCOL`)")
.run()
}

#[cargo_test]
fn http_requires_trailing_slash() {
cargo_process("-Z sparse-registry install bar --index sparse+https://index.crates.io")
Expand Down

0 comments on commit 8e740bb

Please sign in to comment.