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

Support for alternative crates.io registries in cargo #4036

Closed
wants to merge 2 commits 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
6 changes: 5 additions & 1 deletion src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct Manifest {
publish: bool,
replace: Vec<(PackageIdSpec, Dependency)>,
workspace: WorkspaceConfig,
registry: Option<String>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -222,7 +223,8 @@ impl Manifest {
profiles: Profiles,
publish: bool,
replace: Vec<(PackageIdSpec, Dependency)>,
workspace: WorkspaceConfig) -> Manifest {
workspace: WorkspaceConfig,
registry: Option<String>) -> Manifest {
Manifest {
summary: summary,
targets: targets,
Expand All @@ -235,6 +237,7 @@ impl Manifest {
publish: publish,
replace: replace,
workspace: workspace,
registry: registry,
}
}

Expand All @@ -251,6 +254,7 @@ impl Manifest {
pub fn profiles(&self) -> &Profiles { &self.profiles }
pub fn publish(&self) -> bool { self.publish }
pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] { &self.replace }
pub fn registry(&self) -> &Option<String> { &self.registry }
pub fn links(&self) -> Option<&str> {
self.links.as_ref().map(|s| &s[..])
}
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct SerializedPackage<'a> {
targets: &'a [Target],
features: &'a HashMap<String, Vec<String>>,
manifest_path: &'a str,
registry: &'a Option<String>,
}

impl ser::Serialize for Package {
Expand All @@ -49,6 +50,7 @@ impl ser::Serialize for Package {
let license = manmeta.license.as_ref().map(String::as_ref);
let license_file = manmeta.license_file.as_ref().map(String::as_ref);
let description = manmeta.description.as_ref().map(String::as_ref);
let registry = self.manifest.registry();

SerializedPackage {
name: &package_id.name(),
Expand All @@ -62,6 +64,7 @@ impl ser::Serialize for Package {
targets: &self.manifest.targets(),
features: summary.features(),
manifest_path: &self.manifest_path.display().to_string(),
registry: registry,
}.serialize(s)
}
}
Expand Down Expand Up @@ -94,6 +97,7 @@ impl Package {
pub fn version(&self) -> &Version { self.package_id().version() }
pub fn authors(&self) -> &Vec<String> { &self.manifest.metadata().authors }
pub fn publish(&self) -> bool { self.manifest.publish() }
pub fn registry(&self) -> &Option<String> { &self.manifest.registry() }

pub fn has_custom_build(&self) -> bool {
self.targets().iter().any(|t| t.is_custom_build())
Expand Down
27 changes: 20 additions & 7 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use core::{Package, SourceId, Workspace};
use core::dependency::Kind;
use core::manifest::ManifestMetadata;
use ops;
use sources::{RegistrySource};
use sources::RegistrySource;
use util::config;
use util::paths;
use util::{CargoResult, human, ChainError, ToUrl};
Expand Down Expand Up @@ -51,6 +51,17 @@ pub fn publish(ws: &Workspace, opts: &PublishOpts) -> CargoResult<()> {
let (mut registry, reg_id) = registry(opts.config,
opts.token.clone(),
opts.index.clone())?;

if let &Some(ref package_registry) = pkg.registry() {
if let Some(ref index) = opts.index {
if index != package_registry {
bail!("Package registry does not match current registry");
}
} else {
bail!("Custom registry in package definiton, but host not overridden");
}
}

verify_dependencies(pkg, &reg_id)?;

// Prepare a tarball, with a non-surpressable warning if metadata
Expand Down Expand Up @@ -80,12 +91,14 @@ fn verify_dependencies(pkg: &Package, registry_src: &SourceId)
when publishing.\ndependency `{}` does not specify \
a version", dep.name())
}
} else if dep.source_id() != registry_src {
bail!("crates cannot be published to crates.io with dependencies sourced from \
a repository\neither publish `{}` as its own crate on crates.io and \
specify a crates.io version as a dependency or pull it into this \
repository and specify it with a path and version\n(crate `{}` has \
repository path `{}`)", dep.name(), dep.name(), dep.source_id());
} else if !dep.source_id().is_default_registry() {
if dep.source_id() != registry_src {
bail!("crates cannot be published to crates.io with dependencies sourced from \
a repository\neither publish `{}` as its own crate on crates.io and \
specify a crates.io version as a dependency or pull it into this \
repository and specify it with a path and version\n(crate `{}` has \
repository path `{}`)", dep.name(), dep.name(), dep.source_id());
}
}
}
Ok(())
Expand Down
14 changes: 12 additions & 2 deletions src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ pub struct DetailedTomlDependency {
default_features: Option<bool>,
#[serde(rename = "default_features")]
default_features2: Option<bool>,
registry: Option<String>,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -435,6 +436,7 @@ pub struct TomlProject {
#[serde(rename = "license-file")]
license_file: Option<String>,
repository: Option<String>,
registry: Option<String>,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -810,7 +812,8 @@ impl TomlManifest {
profiles,
publish,
replace,
workspace_config);
workspace_config,
project.registry.clone());
if project.license_file.is_some() && project.license.is_some() {
manifest.add_warning("only one of `license` or \
`license-file` is necessary".to_string());
Expand Down Expand Up @@ -1046,7 +1049,14 @@ impl TomlDependency {
cx.source_id.clone()
}
},
(None, None) => SourceId::crates_io(cx.config)?,
(None, None) => {
if let Some(registry) = details.registry {
let url = registry.to_url()?;
SourceId::for_registry(&url)
} else {
SourceId::crates_io(cx.config)?
}
},
};

let version = details.version.as_ref().map(|v| &v[..]);
Expand Down
5 changes: 5 additions & 0 deletions src/doc/manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ license = "..."
# (similar to the readme key).
license-file = "..."

# This optional URL points to a registry index. The package will then be
# published to the registry specified rather than the default. When publishing,
# this index must be given as the host.
registry = "..."

# Optional specification of badges to be displayed on crates.io. The badges
# currently available are Travis CI, Appveyor, and GitLab latest build status,
# specified using the following parameters:
Expand Down
11 changes: 11 additions & 0 deletions src/doc/specifying-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ Here are some examples of inequality requirements:
Multiple version requirements can also be separated with a comma, e.g. `>= 1.2,
< 1.5`.

# Specifying dependencies from a custom registry

To use a custom registry (rather than crates.io) for a dependency, specify the
url of the index:

```toml
[dependencies.rand]
version = "0.1.11"
registry = "https://github.com/my_awesome_fork/crates.io-index"
```

# Specifying dependencies from `git` repositories

To depend on a library located in a `git` repository, the minimum information
Expand Down
12 changes: 12 additions & 0 deletions tests/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn cargo_metadata_simple() {
"license": null,
"license_file": null,
"description": null,
"registry": null,
"targets": [
{
"kind": [
Expand Down Expand Up @@ -94,6 +95,7 @@ crate-type = ["lib", "staticlib"]
"license": null,
"license_file": null,
"description": null,
"registry": null,
"targets": [
{
"kind": [
Expand Down Expand Up @@ -164,6 +166,7 @@ fn cargo_metadata_with_deps_and_version() {
"license": null,
"license_file": null,
"description": null,
"registry": null,
"targets": [
{
"kind": [
Expand Down Expand Up @@ -199,6 +202,7 @@ fn cargo_metadata_with_deps_and_version() {
"license": null,
"license_file": null,
"description": null,
"registry": null,
"targets": [
{
"kind": [
Expand Down Expand Up @@ -234,6 +238,7 @@ fn cargo_metadata_with_deps_and_version() {
"license": "MIT",
"license_file": null,
"description": "foo",
"registry": null,
"targets": [
{
"kind": [
Expand Down Expand Up @@ -302,6 +307,7 @@ name = "ex"
"description": null,
"source": null,
"dependencies": [],
"registry": null,
"targets": [
{
"kind": [ "lib" ],
Expand Down Expand Up @@ -364,6 +370,7 @@ crate-type = ["rlib", "dylib"]
"description": null,
"source": null,
"dependencies": [],
"registry": null,
"targets": [
{
"kind": [ "lib" ],
Expand Down Expand Up @@ -423,6 +430,7 @@ fn workspace_metadata() {
"license": null,
"license_file": null,
"description": null,
"registry": null,
"targets": [
{
"kind": [ "lib" ],
Expand All @@ -443,6 +451,7 @@ fn workspace_metadata() {
"license": null,
"license_file": null,
"description": null,
"registry": null,
"targets": [
{
"kind": [ "lib" ],
Expand Down Expand Up @@ -498,6 +507,7 @@ fn workspace_metadata_no_deps() {
"license": null,
"license_file": null,
"description": null,
"registry": null,
"targets": [
{
"kind": [ "lib" ],
Expand All @@ -518,6 +528,7 @@ fn workspace_metadata_no_deps() {
"license": null,
"license_file": null,
"description": null,
"registry": null,
"targets": [
{
"kind": [ "lib" ],
Expand Down Expand Up @@ -562,6 +573,7 @@ const MANIFEST_OUTPUT: &'static str=
"license": null,
"license_file": null,
"description": null,
"registry": null,
"targets":[{
"kind":["bin"],
"crate_types":["bin"],
Expand Down
1 change: 1 addition & 0 deletions tests/read-manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static MANIFEST_OUTPUT: &'static str = r#"
"description": null,
"source":null,
"dependencies":[],
"registry": null,
"targets":[{
"kind":["bin"],
"crate_types":["bin"],
Expand Down