Skip to content

Support for overriding dl and api in config.json of registry index #11179

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

Closed
wants to merge 1 commit into from
Closed
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
74 changes: 70 additions & 4 deletions src/cargo/core/source/source_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::core::PackageId;
use crate::sources::registry::CRATES_IO_HTTP_INDEX;
use crate::sources::registry::{RegistryConfig, 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 @@ -39,6 +39,9 @@ struct SourceIdInner {
/// WARNING: this is not always set for alt-registries when the name is
/// not known.
name: Option<String>,
/// The registry config. This config is set by cargo config file and will
/// override config.json of the registry.
registry_config: Option<RegistryConfig>,
}

/// The possible kinds of code source. Along with `SourceIdInner`, this fully defines the
Expand Down Expand Up @@ -81,6 +84,27 @@ impl SourceId {
url,
precise: None,
name: name.map(|n| n.into()),
registry_config: None,
});
Ok(source_id)
}

/// Creates a `SourceId` object from the kind, URL and a RegistryConfig.
///
/// The provided RegistryConfig will override config.json of the registry.
fn new_with_registry_config(
kind: SourceKind,
url: Url,
name: Option<&str>,
registry_config: Option<RegistryConfig>,
) -> CargoResult<SourceId> {
let source_id = SourceId::wrap(SourceIdInner {
kind,
canonical_url: CanonicalUrl::new(&url)?,
url,
precise: None,
name: name.map(|n| n.into()),
registry_config,
});
Ok(source_id)
}
Expand Down Expand Up @@ -178,9 +202,27 @@ impl SourceId {
SourceId::new(SourceKind::Registry, url.clone(), None)
}

/// Creates a `SourceId` from a remote registry URL with given name.
pub fn for_alt_registry(url: &Url, name: &str) -> CargoResult<SourceId> {
SourceId::new(SourceKind::Registry, url.clone(), Some(name))
/// Creates a `SourceId` from a remote registry URL with given name and the optional
/// registry config(dl and api).
pub fn for_alt_registry(
url: &Url,
name: &str,
dl: Option<&str>,
api: Option<&str>,
) -> CargoResult<SourceId> {
SourceId::new_with_registry_config(
SourceKind::Registry,
url.clone(),
Some(name),
if let Some(dl) = dl {
Some(RegistryConfig {
dl: dl.into(),
api: api.map(|api| api.into()),
})
} else {
None
},
)
}

/// Creates a SourceId from a local registry path.
Expand Down Expand Up @@ -228,6 +270,7 @@ impl SourceId {
url,
precise: None,
name: Some(key.to_string()),
registry_config: None,
}))
}

Expand Down Expand Up @@ -294,6 +337,29 @@ impl SourceId {
matches!(self.inner.kind, SourceKind::Registry)
}

/// Return `RegistryConfig` set by cargo config file.
pub fn get_registry_config(&self) -> Option<RegistryConfig> {
self.inner.registry_config.clone()
}

/// Override ori_config with the `RegistryConfig` which set by cargo config file,
/// return the overrided version of `RegistryConfig`.
pub fn override_registry_config(
&self,
ori_config: Option<RegistryConfig>,
) -> Option<RegistryConfig> {
let registry_config = self.get_registry_config();
ori_config.map_or(registry_config.clone(), |mut v| {
if let Some(registry_config) = registry_config {
v.dl = registry_config.dl.clone();
if registry_config.api.is_some() {
v.api = registry_config.api;
}
}
Some(v)
})
}

/// Returns `true` if this source from a Git repository.
pub fn is_git(self) -> bool {
matches!(self.inner.kind, SourceKind::Git(_))
Expand Down
13 changes: 12 additions & 1 deletion src/cargo/sources/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ struct SourceConfigDef {
directory: Option<ConfigRelativePath>,
/// A registry source. Value is a URL.
registry: OptValue<String>,
/// A URL to download crates. Override the dl field in config.json.
dl: OptValue<String>,
/// A URL to for registry api. Override the api field value in config.json.
api: OptValue<String>,
/// A local registry source.
local_registry: Option<ConfigRelativePath>,
/// A git source. Value is a URL.
Expand Down Expand Up @@ -216,7 +220,14 @@ restore the source replacement configuration to continue the build
let mut srcs = Vec::new();
if let Some(registry) = def.registry {
let url = url(&registry, &format!("source.{}.registry", name))?;
srcs.push(SourceId::for_alt_registry(&url, &name)?);
let dl = def.dl.map(|v| v.val.clone());
let api = def.api.map(|v| v.val.clone());
srcs.push(SourceId::for_alt_registry(
&url,
&name,
dl.as_deref(),
api.as_deref(),
)?);
}
if let Some(local_registry) = def.local_registry {
let path = local_registry.resolve_path(self.config);
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/sources/registry/http_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
match fs::read(&config_json_path) {
Ok(raw_data) => match serde_json::from_slice(&raw_data) {
Ok(json) => {
self.registry_config = Some(json);
self.registry_config = self.source_id.override_registry_config(Some(json));
return Poll::Ready(Ok(self.registry_config.clone()));
}
Err(e) => log::debug!("failed to decode cached config.json: {}", e),
Expand Down
4 changes: 3 additions & 1 deletion src/cargo/sources/registry/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
match ready!(self.load(Path::new(""), Path::new("config.json"), None)?) {
LoadResponse::Data { raw_data, .. } => {
trace!("config loaded");
Poll::Ready(Ok(Some(serde_json::from_slice(&raw_data)?)))
Poll::Ready(Ok(self
.source_id
.override_registry_config(Some(serde_json::from_slice(&raw_data)?))))
}
_ => Poll::Ready(Ok(None)),
}
Expand Down