From 28dfdccace8cbf2fc8a3ff30cddc10962779fc45 Mon Sep 17 00:00:00 2001 From: Andrew Burkett Date: Wed, 25 Sep 2024 09:46:47 -0700 Subject: [PATCH 1/2] Add third party registry support This adds a `--registry` command line flag to the check release capabilities. The argument to the flag is the name of the registry to use to lookup the crates when using the default behavior or --baseline-version. If left unset, the default behavior remains of --checking crates.io. --- src/config.rs | 16 +++++++++++ src/main.rs | 9 +++++++ src/rustdoc_cmd.rs | 5 +++- src/rustdoc_gen.rs | 66 ++++++++++++++++++++++++++++++---------------- 4 files changed, 72 insertions(+), 24 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4dd0a3d2..b769368a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,6 +16,8 @@ pub struct GlobalConfig { stdout: AutoStream>, stderr: AutoStream>, feature_flags: HashSet, + /// Registry name to look up crates in + registry: Option, } impl Default for GlobalConfig { @@ -41,6 +43,7 @@ impl GlobalConfig { stdout: AutoStream::new(Box::new(std::io::stdout()), stdout_choice), stderr: AutoStream::new(Box::new(std::io::stderr()), stderr_choice), feature_flags: HashSet::new(), + registry: None, } } @@ -316,6 +319,19 @@ impl GlobalConfig { pub fn feature_flags(&self) -> &HashSet { &self.feature_flags } + + /// Set (overwrite) the name of the registry to use for crate lookup + #[inline] + pub fn set_registry(&mut self, registry: String) -> &mut Self { + self.registry = Some(registry); + self + } + + /// Return the name of the registry to use for crate lookup + #[inline] + pub fn registry(&self) -> Option<&str> { + self.registry.as_deref() + } } /// A feature flag for gating unstable `cargo-semver-checks` features. diff --git a/src/main.rs b/src/main.rs index ef133cfb..05f6a764 100644 --- a/src/main.rs +++ b/src/main.rs @@ -156,6 +156,10 @@ fn main() { None => args.check_release, }; + if let Some(registry) = &check_release.registry { + config.set_registry(registry.clone()); + } + let check: cargo_semver_checks::Check = check_release.into(); let report = exit_on_error(config.is_error(), || check.check_release(&mut config)); @@ -532,6 +536,11 @@ struct CheckRelease { #[arg(long = "target")] build_target: Option, + /// Name of registry to use for crate lookups. Used with default behavior + /// and with `--baseline-version`. + #[arg(long = "registry")] + registry: Option, + #[clap(flatten)] unstable_options: UnstableOptions, } diff --git a/src/rustdoc_cmd.rs b/src/rustdoc_cmd.rs index 507448f4..19f7a1fd 100644 --- a/src/rustdoc_cmd.rs +++ b/src/rustdoc_cmd.rs @@ -478,11 +478,14 @@ fn create_placeholder_rustdoc_manifest( }, dependencies: { let project_with_features: DependencyDetail = match crate_source { - CrateSource::Registry { version, .. } => DependencyDetail { + CrateSource::Registry { + version, index_url, .. + } => DependencyDetail { // We need the *exact* version as a dependency, or else cargo will // give us the latest semver-compatible version which is not we want. // Fixes: https://github.com/obi1kenobi/cargo-semver-checks/issues/261 version: Some(format!("={version}")), + registry_index: Some(index_url.clone()), features: crate_source .feature_list_from_config(config, crate_data.feature_config), default_features: matches!( diff --git a/src/rustdoc_gen.rs b/src/rustdoc_gen.rs index 7c3c9fa2..73533063 100644 --- a/src/rustdoc_gen.rs +++ b/src/rustdoc_gen.rs @@ -16,6 +16,8 @@ use crate::GlobalConfig; pub(crate) enum CrateSource<'a> { Registry { crate_: &'a tame_index::IndexVersion, + /// The url of the registry index that holds the specified crate + index_url: String, version: String, }, ManifestPath { @@ -634,6 +636,8 @@ pub(crate) struct RustdocFromRegistry { target_root: PathBuf, version: Option, index: tame_index::index::ComboIndex, + /// The url of the index for the given registry + index_url: String, } impl core::fmt::Debug for RustdocFromRegistry { @@ -648,32 +652,46 @@ impl core::fmt::Debug for RustdocFromRegistry { impl RustdocFromRegistry { pub fn new(target_root: &std::path::Path, config: &mut GlobalConfig) -> anyhow::Result { - let index_url = tame_index::IndexUrl::crates_io( - // This is the config root, where .cargo/config.toml configuration files - // are crawled to determine if crates.io has been source replaced - // - // if not specified it defaults to the current working directory, - // which is the same default that cargo uses, though note this can be - // extremely confusing if one can specify the manifest path of the - // crate from a different current working directory, though AFAICT - // this is not how this binary works - None, - // If set this overrides the CARGO_HOME that is used for both finding - // the "global" default config if not overriden during directory - // traversal to the root, as well as where the various registry - // indices/git sources are rooted. This is generally only useful - // for testing - None, - // If set, overrides the version of the cargo binary used, this is used - // as a fallback to determine if the version is 1.70.0+, which means - // the default crates.io registry to use is the sparse registry, else - // it is the old git registry - None, - ) - .context("failed to obtain crates.io url")?; + let index_url = match config.registry() { + Some(registry_name) => { + tame_index::IndexUrl::for_registry_name( + // No need to override the config root. See comment below for more information. + None, + // No need to override the cargo home. See comment below for more information. + None, + registry_name, + ) + .with_context(|| format!("failed to obtain url for registry '{}'", registry_name))? + } + None => tame_index::IndexUrl::crates_io( + // This is the config root, where .cargo/config.toml configuration files + // are crawled to determine if crates.io has been source replaced + // + // if not specified it defaults to the current working directory, + // which is the same default that cargo uses, though note this can be + // extremely confusing if one can specify the manifest path of the + // crate from a different current working directory, though AFAICT + // this is not how this binary works + None, + // If set this overrides the CARGO_HOME that is used for both finding + // the "global" default config if not overriden during directory + // traversal to the root, as well as where the various registry + // indices/git sources are rooted. This is generally only useful + // for testing + None, + // If set, overrides the version of the cargo binary used, this is used + // as a fallback to determine if the version is 1.70.0+, which means + // the default crates.io registry to use is the sparse registry, else + // it is the old git registry + None, + ) + .context("failed to obtain crates.io url")?, + }; use tame_index::index::{self, ComboIndexCache}; + let index_url_str = index_url.as_str().to_string(); + let index_cache = ComboIndexCache::new(tame_index::IndexLocation::new(index_url)) .context("failed to open crates.io index cache")?; @@ -706,6 +724,7 @@ impl RustdocFromRegistry { target_root: target_root.to_owned(), version: None, index, + index_url: index_url_str, }) } @@ -818,6 +837,7 @@ impl RustdocGenerator for RustdocFromRegistry { self.target_root.clone(), CrateSource::Registry { version: crate_.version.to_string(), + index_url: self.index_url.clone(), crate_, }, crate_data, From e1a656f93ebe75d0ad5aa4e18b64d847b49c9620 Mon Sep 17 00:00:00 2001 From: Andrew Burkett Date: Wed, 25 Sep 2024 11:06:36 -0700 Subject: [PATCH 2/2] Update README with respect to now supported custom registries --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 572f5ff1..1291c041 100644 --- a/README.md +++ b/README.md @@ -122,9 +122,8 @@ The following flags can be used to explicitly specify a baseline instead: The rustdoc json file to use as a semver baseline ``` -Custom registries are not currently supported -([#160](https://github.com/obi1kenobi/cargo-semver-checks/issues/160)), so crates published on -registries other than crates.io should use one of the other approaches of generating the baseline. +For custom registries, use the `--registry` flag with the name of the registry +that you want to use as configured in your `config.toml`. ### What features does `cargo-semver-checks` enable in the tested crates?