Skip to content

Commit e4de526

Browse files
committed
Auto merge of #12687 - ehuss:semver-remove-opt-dep, r=epage
SemVer: Update documentation about removing optional dependencies This updates the documentation cautioning against removing optional dependencies with more up-to-date information about using the `dep:` syntax in the features table. This documentation was written before `dep:`, and I just forgot to update these docs when it was stabilized.
2 parents 819fa73 + 8fe995f commit e4de526

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/doc/src/reference/semver.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2135,9 +2135,16 @@ std = []
21352135

21362136
#### Possibly-breaking: removing an optional dependency {#cargo-remove-opt-dep}
21372137

2138-
Removing an optional dependency can break a project using your library because
2138+
Removing an [optional dependency][opt-dep] can break a project using your library because
21392139
another project may be enabling that dependency via [Cargo features].
21402140

2141+
When there is an optional dependency, cargo implicitly defines a feature of
2142+
the same name to provide a mechanism to enable the dependency and to check
2143+
when it is enabled. This problem can be avoided by using the `dep:` syntax in
2144+
the `[features]` table, which disables this implicit feature. Using `dep:`
2145+
makes it possible to hide the existence of optional dependencies under more
2146+
semantically-relevant names which can be more safely modified.
2147+
21412148
```toml
21422149
# Breaking change example
21432150

@@ -2152,7 +2159,33 @@ curl = { version = "0.4.31", optional = true }
21522159
# ..curl removed
21532160
```
21542161

2162+
```toml
2163+
# MINOR CHANGE
2164+
#
2165+
# This example shows how to avoid breaking changes with optional dependencies.
2166+
2167+
###########################################################
2168+
# Before
2169+
[dependencies]
2170+
curl = { version = "0.4.31", optional = true }
2171+
2172+
[features]
2173+
networking = ["dep:curl"]
2174+
2175+
###########################################################
2176+
# After
2177+
[dependencies]
2178+
# Here, one optional dependency was replaced with another.
2179+
hyper = { version = "0.14.27", optional = true }
2180+
2181+
[features]
2182+
networking = ["dep:hyper"]
2183+
```
2184+
21552185
Mitigation strategies:
2186+
* Use the `dep:` syntax in the `[features]` table to avoid exposing optional
2187+
dependencies in the first place. See [optional dependencies][opt-dep] for
2188+
more information.
21562189
* Clearly document your features. If the optional dependency is not included
21572190
in the documented list of features, then you may decide to consider it safe
21582191
to change undocumented entries.
@@ -2166,6 +2199,8 @@ Mitigation strategies:
21662199
optional dependencies necessary to implement "networking". Then document the
21672200
"networking" feature.
21682201

2202+
[opt-dep]: features.md#optional-dependencies
2203+
21692204
#### Minor: changing dependency features {#cargo-change-dep-feature}
21702205

21712206
It is usually safe to change the features on a dependency, as long as the

0 commit comments

Comments
 (0)