Skip to content

Commit 339d9f9

Browse files
committed
Auto merge of #6319 - alexcrichton:beta-next, r=alexcrichton
[beta] Stabilize the `rename-dependency` feature This is a backport of #6311
2 parents 569507e + 5d6504d commit 339d9f9

File tree

4 files changed

+66
-150
lines changed

4 files changed

+66
-150
lines changed

src/cargo/core/features.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ features! {
177177
[stable] edition: bool,
178178

179179
// Renaming a package in the manifest via the `package` key
180-
[unstable] rename_dependency: bool,
180+
[stable] rename_dependency: bool,
181181

182182
// Whether a lock file is published with this crate
183183
[unstable] publish_lockfile: bool,

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

+59
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,62 @@ features = ["secure-password", "civet"]
534534

535535
More information about features can be found in the
536536
[manifest documentation](reference/manifest.html#the-features-section).
537+
538+
### Renaming dependencies in `Cargo.toml`
539+
540+
When writing a `[dependencies]` section in `Cargo.toml` the key you write for a
541+
dependency typically matches up to the name of the crate you import from in the
542+
code. For some projects, though, you may wish to reference the crate with a
543+
different name in the code regardless of how it's published on crates.io. For
544+
example you may wish to:
545+
546+
* Avoid the need to `use foo as bar` in Rust source.
547+
* Depend on multiple versions of a crate.
548+
* Depend on crates with the same name from different registries.
549+
550+
To support this Cargo supports a `package` key in the `[dependencies]` section
551+
of which package should be depended on:
552+
553+
```toml
554+
[package]
555+
name = "mypackage"
556+
version = "0.0.1"
557+
558+
[dependencies]
559+
foo = "0.1"
560+
bar = { git = "https://github.com/example/project", package = "foo" }
561+
baz = { version = "0.1", registry = "custom", package = "foo" }
562+
```
563+
564+
In this example, three crates are now available in your Rust code:
565+
566+
```rust
567+
extern crate foo; // crates.io
568+
extern crate bar; // git repository
569+
extern crate baz; // registry `custom`
570+
```
571+
572+
All three of these crates have the package name of `foo` in their own
573+
`Cargo.toml`, so we're explicitly using the `package` key to inform Cargo that
574+
we want the `foo` package even though we're calling it something else locally.
575+
The `package` key, if not specified, defaults to the name of the dependency
576+
being requested.
577+
578+
Note that if you have an optional dependency like:
579+
580+
```toml
581+
[dependencies]
582+
foo = { version = "0.1", package = 'bar', optional = true }
583+
```
584+
585+
you're depending on the crate `bar` from crates.io, but your crate has a `foo`
586+
feature instead of a `bar` feature. That is, names of features take after the
587+
name of the dependency, not the package name, when renamed.
588+
589+
Enabling transitive dependencies works similarly, for example we could add the
590+
following to the above manifest:
591+
592+
```toml
593+
[features]
594+
log-debug = ['foo/log-debug'] # using 'bar/log-debug' would be an error!
595+
```

src/doc/src/reference/unstable.md

-55
Original file line numberDiff line numberDiff line change
@@ -63,61 +63,6 @@ publish = ["my-registry"]
6363
```
6464

6565

66-
### rename-dependency
67-
* Original Issue: [#1311](https://github.com/rust-lang/cargo/issues/1311)
68-
* PR: [#4953](https://github.com/rust-lang/cargo/pull/4953)
69-
* Tracking Issue: [#5653](https://github.com/rust-lang/cargo/issues/5653)
70-
71-
The rename-dependency feature allows you to import a dependency
72-
with a different name from the source. This can be useful in a few scenarios:
73-
74-
* Depending on crates with the same name from different registries.
75-
* Depending on multiple versions of a crate.
76-
* Avoid needing `extern crate foo as bar` in Rust source.
77-
78-
Just include the `package` key to specify the actual name of the dependency.
79-
You must include `cargo-features` at the top of your `Cargo.toml`.
80-
81-
```toml
82-
cargo-features = ["rename-dependency"]
83-
84-
[package]
85-
name = "mypackage"
86-
version = "0.0.1"
87-
88-
[dependencies]
89-
foo = "0.1"
90-
bar = { version = "0.1", registry = "custom", package = "foo" }
91-
baz = { git = "https://github.com/example/project", package = "foo" }
92-
```
93-
94-
In this example, three crates are now available in your Rust code:
95-
96-
```rust
97-
extern crate foo; // crates.io
98-
extern crate bar; // registry `custom`
99-
extern crate baz; // git repository
100-
```
101-
102-
Note that if you have an optional dependency like:
103-
104-
```toml
105-
[dependencies]
106-
foo = { version = "0.1", package = 'bar', optional = true }
107-
```
108-
109-
you're depending on the crate `bar` from crates.io, but your crate has a `foo`
110-
feature instead of a `bar` feature. That is, names of features take after the
111-
name of the dependency, not the package name, when renamed.
112-
113-
Enabling transitive dependencies works similarly, for example we could add the
114-
following to the above manifest:
115-
116-
```toml
117-
[features]
118-
log-debug = ['foo/log-debug'] # using 'bar/log-debug' would be an error!
119-
```
120-
12166
### publish-lockfile
12267
* Original Issue: [#2263](https://github.com/rust-lang/cargo/issues/2263)
12368
* PR: [#5093](https://github.com/rust-lang/cargo/pull/5093)

tests/testsuite/rename_deps.rs

+6-94
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,6 @@ use support::paths;
33
use support::registry::Package;
44
use support::{basic_manifest, project};
55

6-
#[test]
7-
fn gated() {
8-
let p = project()
9-
.file(
10-
"Cargo.toml",
11-
r#"
12-
[project]
13-
name = "foo"
14-
version = "0.0.1"
15-
authors = []
16-
17-
[dependencies]
18-
bar = { package = "foo", version = "0.1" }
19-
"#,
20-
).file("src/lib.rs", "")
21-
.build();
22-
23-
p.cargo("build")
24-
.masquerade_as_nightly_cargo()
25-
.with_status(101)
26-
.with_stderr(
27-
"\
28-
error: failed to parse manifest at `[..]`
29-
30-
Caused by:
31-
feature `rename-dependency` is required
32-
33-
consider adding `cargo-features = [\"rename-dependency\"]` to the manifest
34-
",
35-
).run();
36-
37-
let p = project()
38-
.at("bar")
39-
.file(
40-
"Cargo.toml",
41-
r#"
42-
[project]
43-
name = "foo"
44-
version = "0.0.1"
45-
authors = []
46-
47-
[dependencies]
48-
bar = { version = "0.1", package = "baz" }
49-
"#,
50-
).file("src/lib.rs", "")
51-
.build();
52-
53-
p.cargo("build")
54-
.masquerade_as_nightly_cargo()
55-
.with_status(101)
56-
.with_stderr(
57-
"\
58-
error: failed to parse manifest at `[..]`
59-
60-
Caused by:
61-
feature `rename-dependency` is required
62-
63-
consider adding `cargo-features = [\"rename-dependency\"]` to the manifest
64-
",
65-
).run();
66-
}
67-
686
#[test]
697
fn rename_dependency() {
708
Package::new("bar", "0.1.0").publish();
@@ -74,8 +12,6 @@ fn rename_dependency() {
7412
.file(
7513
"Cargo.toml",
7614
r#"
77-
cargo-features = ["rename-dependency"]
78-
7915
[project]
8016
name = "foo"
8117
version = "0.0.1"
@@ -88,7 +24,7 @@ fn rename_dependency() {
8824
).file("src/lib.rs", "extern crate bar; extern crate baz;")
8925
.build();
9026

91-
p.cargo("build").masquerade_as_nightly_cargo().run();
27+
p.cargo("build").run();
9228
}
9329

9430
#[test]
@@ -97,8 +33,6 @@ fn rename_with_different_names() {
9733
.file(
9834
"Cargo.toml",
9935
r#"
100-
cargo-features = ["rename-dependency"]
101-
10236
[project]
10337
name = "foo"
10438
version = "0.0.1"
@@ -122,7 +56,7 @@ fn rename_with_different_names() {
12256
).file("bar/src/lib.rs", "")
12357
.build();
12458

125-
p.cargo("build").masquerade_as_nightly_cargo().run();
59+
p.cargo("build").run();
12660
}
12761

12862
#[test]
@@ -148,8 +82,7 @@ fn lots_of_names() {
14882
"Cargo.toml",
14983
&format!(
15084
r#"
151-
cargo-features = ["alternative-registries", "rename-dependency"]
152-
85+
cargo-features = ["alternative-registries"]
15386
[package]
15487
name = "test"
15588
version = "0.1.0"
@@ -196,8 +129,6 @@ fn rename_and_patch() {
196129
.file(
197130
"Cargo.toml",
198131
r#"
199-
cargo-features = ["rename-dependency"]
200-
201132
[package]
202133
name = "test"
203134
version = "0.1.0"
@@ -216,7 +147,7 @@ fn rename_and_patch() {
216147
.file("foo/src/lib.rs", "pub fn foo() {}")
217148
.build();
218149

219-
p.cargo("build -v").masquerade_as_nightly_cargo().run();
150+
p.cargo("build -v").run();
220151
}
221152

222153
#[test]
@@ -227,8 +158,6 @@ fn rename_twice() {
227158
.file(
228159
"Cargo.toml",
229160
r#"
230-
cargo-features = ["rename-dependency"]
231-
232161
[package]
233162
name = "test"
234163
version = "0.1.0"
@@ -243,7 +172,6 @@ fn rename_twice() {
243172
.build();
244173

245174
p.cargo("build -v")
246-
.masquerade_as_nightly_cargo()
247175
.with_status(101)
248176
.with_stderr(
249177
"\
@@ -264,8 +192,6 @@ fn rename_affects_fingerprint() {
264192
.file(
265193
"Cargo.toml",
266194
r#"
267-
cargo-features = ["rename-dependency"]
268-
269195
[package]
270196
name = "test"
271197
version = "0.1.0"
@@ -277,13 +203,11 @@ fn rename_affects_fingerprint() {
277203
).file("src/lib.rs", "extern crate foo;")
278204
.build();
279205

280-
p.cargo("build -v").masquerade_as_nightly_cargo().run();
206+
p.cargo("build -v").run();
281207

282208
p.change_file(
283209
"Cargo.toml",
284210
r#"
285-
cargo-features = ["rename-dependency"]
286-
287211
[package]
288212
name = "test"
289213
version = "0.1.0"
@@ -295,7 +219,6 @@ fn rename_affects_fingerprint() {
295219
);
296220

297221
p.cargo("build -v")
298-
.masquerade_as_nightly_cargo()
299222
.with_status(101)
300223
.run();
301224
}
@@ -309,8 +232,6 @@ fn can_run_doc_tests() {
309232
.file(
310233
"Cargo.toml",
311234
r#"
312-
cargo-features = ["rename-dependency"]
313-
314235
[project]
315236
name = "foo"
316237
version = "0.0.1"
@@ -328,7 +249,6 @@ fn can_run_doc_tests() {
328249
).build();
329250

330251
foo.cargo("test -v")
331-
.masquerade_as_nightly_cargo()
332252
.with_stderr_contains(
333253
"\
334254
[DOCTEST] foo
@@ -363,8 +283,6 @@ fn features_still_work() {
363283
.file(
364284
"a/Cargo.toml",
365285
r#"
366-
cargo-features = ["rename-dependency"]
367-
368286
[package]
369287
name = "p1"
370288
version = "0.1.0"
@@ -377,8 +295,6 @@ fn features_still_work() {
377295
.file(
378296
"b/Cargo.toml",
379297
r#"
380-
cargo-features = ["rename-dependency"]
381-
382298
[package]
383299
name = "p2"
384300
version = "0.1.0"
@@ -393,7 +309,7 @@ fn features_still_work() {
393309
).file("b/src/lib.rs", "extern crate b;")
394310
.build();
395311

396-
p.cargo("build -v").masquerade_as_nightly_cargo().run();
312+
p.cargo("build -v").run();
397313
}
398314

399315
#[test]
@@ -405,7 +321,6 @@ fn features_not_working() {
405321
.file(
406322
"Cargo.toml",
407323
r#"
408-
cargo-features = ["rename-dependency"]
409324
[package]
410325
name = "test"
411326
version = "0.1.0"
@@ -422,7 +337,6 @@ fn features_not_working() {
422337
.build();
423338

424339
p.cargo("build -v")
425-
.masquerade_as_nightly_cargo()
426340
.with_status(101)
427341
.with_stderr(
428342
"\
@@ -440,7 +354,6 @@ fn rename_with_dash() {
440354
.file(
441355
"Cargo.toml",
442356
r#"
443-
cargo-features = ["rename-dependency"]
444357
[package]
445358
name = "qwerty"
446359
version = "0.1.0"
@@ -455,6 +368,5 @@ fn rename_with_dash() {
455368
.build();
456369

457370
p.cargo("build")
458-
.masquerade_as_nightly_cargo()
459371
.run();
460372
}

0 commit comments

Comments
 (0)