Skip to content

Commit

Permalink
Auto merge of rust-lang#10725 - arlosi:http-cratesio, r=Eh2406
Browse files Browse the repository at this point in the history
Make -Z http-registry use index.crates.io when accessing crates-io

Use `sparse+https://index.crates.io/` to access crates.io when `-Z http-registry` is set.

* `Cargo.lock` files still emit the github URL `https://github.com/rust-lang/crates.io-index`.
* Allows publishing to a source-replaced `crates-io` only for `index.crates.io`. Other source-replacements of `crates-io` are still blocked.

Fixes rust-lang#10722
r? `@Eh2406`
  • Loading branch information
bors committed Jun 7, 2022
2 parents bc3b99d + 445db94 commit 85e457e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/cargo/core/source/source_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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};
Expand Down Expand Up @@ -206,6 +207,18 @@ impl SourceId {
})
}

/// Returns the `SourceId` corresponding to the main repository, using the
/// http index if allowed.
pub fn crates_io_maybe_http(config: &Config) -> CargoResult<SourceId> {
if config.cli_unstable().http_registry {
config.check_registry_index_not_set()?;
let url = CRATES_IO_HTTP_INDEX.into_url().unwrap();
SourceId::new(SourceKind::Registry, url, Some(CRATES_IO_REGISTRY))
} else {
Self::crates_io(config)
}
}

/// Gets the `SourceId` associated with given name of the remote registry.
pub fn alt_registry(config: &Config, key: &str) -> CargoResult<SourceId> {
let url = config.get_registry_index(key)?;
Expand Down Expand Up @@ -356,7 +369,8 @@ impl SourceId {
SourceKind::Registry => {}
_ => return false,
}
self.inner.url.as_str() == CRATES_IO_INDEX
let url = self.inner.url.as_str();
url == CRATES_IO_INDEX || url == CRATES_IO_HTTP_INDEX
}

/// Hashes `self`.
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ fn verify_dependencies(
if super::check_dep_has_version(dep, true)? {
continue;
}
// Allow publishing to crates.io with index.crates.io as a source replacement.
if registry_src.is_default_registry() && dep.source_id().is_default_registry() {
continue;
}
// TomlManifest::prepare_for_publish will rewrite the dependency
// to be just the `version` field.
if dep.source_id() != registry_src {
Expand Down
11 changes: 10 additions & 1 deletion src/cargo/sources/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ impl<'cfg> SourceConfigMap<'cfg> {
replace_with: None,
},
)?;
if config.cli_unstable().http_registry {
base.add(
CRATES_IO_REGISTRY,
SourceConfig {
id: SourceId::crates_io_maybe_http(config)?,
replace_with: None,
},
)?;
}
Ok(base)
}

Expand Down Expand Up @@ -248,7 +257,7 @@ restore the source replacement configuration to continue the build
check_not_set("rev", def.rev)?;
}
if name == CRATES_IO_REGISTRY && srcs.is_empty() {
srcs.push(SourceId::crates_io(self.config)?);
srcs.push(SourceId::crates_io_maybe_http(self.config)?);
}

match srcs.len() {
Expand Down
1 change: 1 addition & 0 deletions src/cargo/sources/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ use crate::util::{restricted_names, CargoResult, Config, Filesystem, OptVersionR

const PACKAGE_SOURCE_LOCK: &str = ".cargo-ok";
pub const CRATES_IO_INDEX: &str = "https://github.com/rust-lang/crates.io-index";
pub const CRATES_IO_HTTP_INDEX: &str = "sparse+https://index.crates.io/";
pub const CRATES_IO_REGISTRY: &str = "crates-io";
pub const CRATES_IO_DOMAIN: &str = "crates.io";
const CRATE_TEMPLATE: &str = "{crate}";
Expand Down

0 comments on commit 85e457e

Please sign in to comment.