Skip to content

RUST-2217 Optionally support bson crate 3.0 #1380

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

Merged
merged 8 commits into from
May 28, 2025

Conversation

abr-egn
Copy link
Contributor

@abr-egn abr-egn commented May 22, 2025

RUST-2217

This adds the bson-2 and bson-3 features, allowing users to select which version of the bson crate they'd like the driver crate to work with. bson-2 remains the default for compatibility reasons, we can't change that until driver 4.0.

There are actually quite a few user scenarios here and I think we managed to cover them nicely:

  • User does nothing: Nothing changes and there are no surprises; bson 2.x continues to be used.
  • User had disabled default features: They'll have been required to enable the compat-3-0-0 feature (compile error otherwise), which at the time did nothing; now that feature enables bson-2, so this user will also continue to get bson 2.x.
  • User adds the bson-3 feature flag: The driver API will switch over to using bson 3.x, nothing else will change. Under the hood, the bson 2.x crate will continue to be pulled in as a dependency; this is a consequence of Rust features needing to be additive. If the user cares about the compile time/binary size, they can go with:
  • User disables default features, selects the bson-3 feature flag: They'll get just bson 3.x, no 2.x pulled in.

@@ -29,7 +29,10 @@ exclude = [

[features]
default = ["compat-3-0-0", "rustls-tls", "dns-resolver"]
compat-3-0-0 = []
compat-3-0-0 = ["compat-3-3-0", "bson-2"]
compat-3-3-0 = []
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keeping a rolling compat-VERSION feature allows us to do this again for other things if we need to, e.g. if we need to go back to supporting multiple async runtimes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice. That seems like a good precaution in case there is a future need to make a required feature optional.

@abr-egn
Copy link
Contributor Author

abr-egn commented May 22, 2025

cargo-deny and msrv failures are both unrelated; I've filed RUST-2222 and RUST-2223 for them.

@abr-egn abr-egn marked this pull request as ready for review May 22, 2025 19:11
@abr-egn abr-egn requested a review from a team as a code owner May 22, 2025 19:12
kevinAlbs
kevinAlbs previously approved these changes May 23, 2025
Copy link
Contributor

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

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

LGTM! Testing scenarios locally match the PR description.

@abr-egn abr-egn force-pushed the RUST-2217/optional-bson-3.0 branch from cc7ba19 to 4358d38 Compare May 27, 2025 15:59
@abr-egn
Copy link
Contributor Author

abr-egn commented May 27, 2025

Update: the msrv and cargo-deny failures were because I needed to rebase. cargo-deny is now happy but there's a new msrv failure that's actually directly related and needs fixing.

@@ -17,7 +17,7 @@ license = "Apache-2.0"
readme = "README.md"
name = "mongodb"
version = "3.2.3"
rust-version = "1.74"
rust-version = "1.81"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This needed to change because bson's msrv at head is 1.81; when doing an msrv compile against 2.x it would work because the crates.io published version had an msrv of 1.48, but there's no published version for 3.x so it needs to use the one from git.

@@ -9,9 +9,6 @@ source ./.evergreen/env.sh
if [ "$RUST_VERSION" != "" ]; then
rustup toolchain install $RUST_VERSION
TOOLCHAIN="+${RUST_VERSION}"
# Remove the local git dependencies for bson and mongocrypt, which don't work properly with the MSRV resolver.
sed -i "s/bson =.*/bson = \"2\"/" Cargo.toml
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I accidentally broke these by changing the formatting of Cargo.toml to no longer match, but AFAICT the MSRV resolver is working fine with the git dependencies now.

I'm not sure if this would have been a viable solution as we progress with bson 3.0 since there's no published version yet to fall back on, so hopefully I'm not missing a failure mode here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@isabelatkinson I'd like your input here - I can't find any brokenness but I'm deeply wary of things that work when I don't expect them to :)

Copy link
Contributor

Choose a reason for hiding this comment

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

This originally had to be fixed when the driver's MSRV (1.74) was less than bson's (1.81) - the MSRV resolver couldn't look back for an older MSRV-compatible version with a git dependency instead of a published version dependency. That problem goes away with the driver's MSRV being >= bson's MSRV (which is done in this PR) since an older version doesn't need to be selected, but it will come up again if we bump bson without bumping the driver. I think we can just figure that out if/when it comes up again, though - we should be able to do something similar once 3.0 is actually published.

git = "https://github.com/mongodb/bson-rust"
branch = "main"
package = "bson"
version = "3.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly out of curiosity: will this break if we try to release the driver before bson 3.0 is released?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cargo publish will fail since there's no non-git version, yeah. If we do need to do a driver release before 3.0 we can either push a patch to the release branch that strips out the bson-3 feature entirely or we could push an alpha release of the bson crate to cargo.

@@ -9,9 +9,6 @@ source ./.evergreen/env.sh
if [ "$RUST_VERSION" != "" ]; then
rustup toolchain install $RUST_VERSION
TOOLCHAIN="+${RUST_VERSION}"
# Remove the local git dependencies for bson and mongocrypt, which don't work properly with the MSRV resolver.
sed -i "s/bson =.*/bson = \"2\"/" Cargo.toml
Copy link
Contributor

Choose a reason for hiding this comment

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

This originally had to be fixed when the driver's MSRV (1.74) was less than bson's (1.81) - the MSRV resolver couldn't look back for an older MSRV-compatible version with a git dependency instead of a published version dependency. That problem goes away with the driver's MSRV being >= bson's MSRV (which is done in this PR) since an older version doesn't need to be selected, but it will come up again if we bump bson without bumping the driver. I think we can just figure that out if/when it comes up again, though - we should be able to do something similar once 3.0 is actually published.

@abr-egn abr-egn merged commit a82265d into mongodb:main May 28, 2025
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants