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 third party registry support #945

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
16 changes: 16 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct GlobalConfig {
stdout: AutoStream<Box<dyn Write + 'static>>,
stderr: AutoStream<Box<dyn Write + 'static>>,
feature_flags: HashSet<FeatureFlag>,
/// Registry name to look up crates in
registry: Option<String>,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure if this was the best place to stash the registry flag from the cli. It seems like its okay, but I can't say I did a deep dive of the code to fully understand all the components.

}

impl Default for GlobalConfig {
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -316,6 +319,19 @@ impl GlobalConfig {
pub fn feature_flags(&self) -> &HashSet<FeatureFlag> {
&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.
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -532,6 +536,11 @@ struct CheckRelease {
#[arg(long = "target")]
build_target: Option<String>,

/// Name of registry to use for crate lookups. Used with default behavior
/// and with `--baseline-version`.
#[arg(long = "registry")]
registry: Option<String>,

#[clap(flatten)]
unstable_options: UnstableOptions,
}
Expand Down
5 changes: 4 additions & 1 deletion src/rustdoc_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
66 changes: 43 additions & 23 deletions src/rustdoc_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -634,6 +636,8 @@ pub(crate) struct RustdocFromRegistry {
target_root: PathBuf,
version: Option<semver::Version>,
index: tame_index::index::ComboIndex,
/// The url of the index for the given registry
index_url: String,
}

impl core::fmt::Debug for RustdocFromRegistry {
Expand All @@ -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<Self> {
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
// <https://doc.rust-lang.org/cargo/reference/source-replacement.html>
// 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
// <https://doc.rust-lang.org/cargo/reference/source-replacement.html>
// 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")?;

Expand Down Expand Up @@ -706,6 +724,7 @@ impl RustdocFromRegistry {
target_root: target_root.to_owned(),
version: None,
index,
index_url: index_url_str,
})
}

Expand Down Expand Up @@ -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,
Expand Down
Loading