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

Add configuration option for controlling crates.io protocol #11215

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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