Skip to content

Commit d84f2de

Browse files
feat(manifest): allow git dependency alongside alternative registry
1 parent 1be51ed commit d84f2de

5 files changed

Lines changed: 78 additions & 36 deletions

File tree

src/cargo/util/toml/mod.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,21 +2362,17 @@ fn to_dependency_source_id<P: ResolveToPath + Clone>(
23622362
orig.registry.as_deref(),
23632363
orig.registry_index.as_ref(),
23642364
) {
2365-
(Some(_git), _, Some(_registry), _) | (Some(_git), _, _, Some(_registry)) => bail!(
2366-
"dependency ({name_in_toml}) specification is ambiguous. \
2367-
Only one of `git` or `registry` is allowed.",
2368-
),
2369-
(_, _, Some(_registry), Some(_registry_index)) => bail!(
2370-
"dependency ({name_in_toml}) specification is ambiguous. \
2371-
Only one of `registry` or `registry-index` is allowed.",
2372-
),
2373-
(Some(_git), Some(_path), None, None) => {
2365+
(Some(_git), Some(_path), _, _) => {
23742366
bail!(
23752367
"dependency ({name_in_toml}) specification is ambiguous. \
23762368
Only one of `git` or `path` is allowed.",
23772369
);
23782370
}
2379-
(Some(git), None, None, None) => {
2371+
(_, _, Some(_registry), Some(_registry_index)) => bail!(
2372+
"dependency ({name_in_toml}) specification is ambiguous. \
2373+
Only one of `registry` or `registry-index` is allowed.",
2374+
),
2375+
(Some(git), None, _, _) => {
23802376
let n_details = [&orig.branch, &orig.tag, &orig.rev]
23812377
.iter()
23822378
.filter(|d| d.is_some())

src/doc/src/reference/specifying-dependencies.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,11 @@ The use of `path` and `version` keys together is explained in the [Multiple loca
389389
390390
## Multiple locations
391391

392-
It is possible to specify both a registry version and a `git` or `path`
393-
location. The `git` or `path` dependency will be used locally (in which case
394-
the `version` is checked against the local copy), and when published to a
395-
registry like [crates.io], it will use the registry version. Other
396-
combinations are not allowed. Examples:
392+
It is possible to specify both a `git` or `path` location and either a
393+
registry version or an alternative registry. The `git` or `path` dependency
394+
will be used locally, and when published to a registry it will use the
395+
registry version. If a `version` is specified, it is checked against the
396+
local copy. Other combinations are not allowed. Examples:
397397

398398
```toml
399399
[dependencies]
@@ -405,6 +405,11 @@ bitflags = { path = "my-bitflags", version = "1.0" }
405405
# version 1.0 from crates.io when published.
406406
smallvec = { git = "https://github.com/servo/rust-smallvec.git", version = "1.0" }
407407

408+
# Uses the given git repo when used locally, and uses
409+
# version 1.0 from `my-registry` when published.
410+
# See [the alternate registry chapter](registries.md#using-an-alternate-registry) for how to use an alternate registry.
411+
smallvec = { git = "https://github.com/servo/rust-smallvec.git", version = "1.0", registry = "my-registry" }
412+
408413
# Note: if a version doesn't match, Cargo will fail to compile!
409414
```
410415

tests/testsuite/alt_registry.rs

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,12 @@ fn registry_and_git_dep_works() {
274274
Package::new("bar", "0.0.1").alternative(true).publish();
275275

276276
p.cargo("check")
277-
.with_status(101)
278277
.with_stderr_data(str![[r#"
279-
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
280-
281-
Caused by:
282-
dependency (bar) specification is ambiguous. Only one of `git` or `registry` is allowed.
278+
[UPDATING] git repository `[ROOTURL]/bar`
279+
[LOCKING] 1 package to latest compatible version
280+
[CHECKING] bar v0.0.1 ([ROOTURL]/bar#[..])
281+
[CHECKING] foo v0.0.1 ([ROOT]/foo)
282+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
283283
284284
"#]])
285285
.run();
@@ -720,15 +720,62 @@ fn publish_with_git_and_registry_dep() {
720720
Package::new("bar", "0.0.1").alternative(true).publish();
721721

722722
p.cargo("publish --registry alternative")
723-
.with_status(101)
724723
.with_stderr_data(str![[r#"
725-
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
726-
727-
Caused by:
728-
dependency (bar) specification is ambiguous. Only one of `git` or `registry` is allowed.
724+
[UPDATING] `alternative` index
725+
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
726+
[UPDATING] `alternative` index
727+
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
728+
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
729+
[DOWNLOADING] crates ...
730+
[DOWNLOADED] bar v0.0.1 (registry `alternative`)
731+
[COMPILING] bar v0.0.1 (registry `alternative`)
732+
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
733+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
734+
[UPLOADING] foo v0.0.1 ([ROOT]/foo)
735+
[UPLOADED] foo v0.0.1 to registry `alternative`
736+
[NOTE] waiting for foo v0.0.1 to be available at registry `alternative`
737+
[HELP] you may press ctrl-c to skip waiting; the crate should be available shortly
738+
[PUBLISHED] foo v0.0.1 at registry `alternative`
729739
730740
"#]])
731741
.run();
742+
743+
validate_alt_upload(
744+
r#"{
745+
"authors": [],
746+
"badges": {},
747+
"categories": [],
748+
"deps": [
749+
{
750+
"default_features": true,
751+
"features": [],
752+
"kind": "normal",
753+
"name": "bar",
754+
"optional": false,
755+
"target": null,
756+
"version_req": "^0.0.1"
757+
}
758+
],
759+
"description": null,
760+
"documentation": null,
761+
"features": {},
762+
"homepage": null,
763+
"keywords": [],
764+
"license": null,
765+
"license_file": null,
766+
"links": null,
767+
"name": "foo",
768+
"readme": null,
769+
"readme_file": null,
770+
"repository": null,
771+
"homepage": null,
772+
"documentation": null,
773+
"rust_version": null,
774+
"vers": "0.0.1"
775+
}"#,
776+
"foo-0.0.1.crate",
777+
&["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"],
778+
);
732779
}
733780

734781
#[cargo_test]

tests/testsuite/cargo_add/git_registry/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ fn case() {
4646
])
4747
.current_dir(cwd)
4848
.assert()
49-
.failure()
5049
.stdout_eq(str![""])
5150
.stderr_eq(file!["stderr.term.svg"]);
5251

tests/testsuite/cargo_add/git_registry/stderr.term.svg

Lines changed: 5 additions & 10 deletions
Loading

0 commit comments

Comments
 (0)