From 3df019d491d3abeb561f011a1f4ab1a0d60b3501 Mon Sep 17 00:00:00 2001 From: Jason Lunn Date: Tue, 25 Aug 2026 09:05:30 -0700 Subject: [PATCH] Forward-port Rust crate swap (#29397) Cherrypick crate fixes from 36.x to main Closes #29397 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/29397 from protocolbuffers:forward_port_crate_swap 2b5bb9fe65cf28b139bac1aee07f67c7863ce823 PiperOrigin-RevId: 970589358 --- rust/internal.rs | 26 +++++-------- .../google_protobuf/BUILD.bazel | 10 ++++- .../google_protobuf/Cargo-template.toml | 27 ++++++++++++- rust/release_crates/google_protobuf/README.md | 39 +++++++++++++++++++ .../{protobuf => google_protobuf}/build.rs | 0 .../release_crates/google_protobuf/src/lib.rs | 1 - rust/release_crates/protobuf/BUILD | 10 +---- .../protobuf/Cargo-template.toml | 27 +------------ rust/release_crates/protobuf/README.md | 3 ++ rust/release_crates/protobuf/src/lib.rs | 1 + src/google/protobuf/compiler/versions.h | 2 +- 11 files changed, 90 insertions(+), 56 deletions(-) create mode 100644 rust/release_crates/google_protobuf/README.md rename rust/release_crates/{protobuf => google_protobuf}/build.rs (100%) delete mode 100644 rust/release_crates/google_protobuf/src/lib.rs create mode 100644 rust/release_crates/protobuf/src/lib.rs diff --git a/rust/internal.rs b/rust/internal.rs index 6ba21c5f7c..047684a4c3 100644 --- a/rust/internal.rs +++ b/rust/internal.rs @@ -100,17 +100,12 @@ const fn split_version(version: &str) -> (u32, u32, u32, u32) { // Indicates whether the given gencode and runtime versions are compatible with // each other. Generally they are compatible if the gencode is no newer than the -// runtime, but the exception is that we consider gencode at 4.33 and older to -// be incompatible no matter what. +// runtime. #[cfg(not(bzl))] const fn are_versions_compatible(gencode: &str, runtime: &str) -> bool { let gencode = split_version(gencode); let runtime = split_version(runtime); - if gencode.0 < 4 || (gencode.0 == 4 && gencode.1 <= 33) { - return false; - } - if gencode.0 != runtime.0 { return gencode.0 < runtime.0; } @@ -134,18 +129,19 @@ const fn are_versions_compatible(gencode: &str, runtime: &str) -> bool { /// the current runtime version. We require that the generated code cannot be /// newer than the runtime version. /// -/// 4.34 is the first stable release, so any gencode older than that is not -/// compatible going forward. -/// /// If you are seeing this fail, it means that your generated code was built -/// with a protoc version newer than the runtime crate version (or you have -/// pre-4.34 gencode). +/// with a protoc version newer than the runtime crate version. #[cfg(not(bzl))] pub const fn assert_compatible_gencode_version(gencode_version: &'static str) { let runtime_version = env!("CARGO_PKG_VERSION"); assert!( are_versions_compatible(gencode_version, runtime_version), - "Gencode version is not compatible with runtime version", + concat!( + "Gencode version (see callsite) is not compatible with runtime ", + "version (runtime is ", + env!("CARGO_PKG_VERSION"), + ")" + ), ) } @@ -170,11 +166,7 @@ mod tests { #[gtest] fn test_are_versions_compatible() { - // Pre-4.34 gencode is never considered compatible. - expect_false!(are_versions_compatible("4.33.0", "4.33.1")); - expect_false!(are_versions_compatible("3.33.0", "3.33.0")); - - // Otherwise, exact matches are always fine. + // Exact matches are always fine. expect_true!(are_versions_compatible("4.34.0-rc.1", "4.34.0-rc.1")); expect_true!(are_versions_compatible("4.34.1", "4.34.1")); diff --git a/rust/release_crates/google_protobuf/BUILD.bazel b/rust/release_crates/google_protobuf/BUILD.bazel index 3acea54b28..b630b379d0 100644 --- a/rust/release_crates/google_protobuf/BUILD.bazel +++ b/rust/release_crates/google_protobuf/BUILD.bazel @@ -5,6 +5,8 @@ pkg_filegroup( name = "google_protobuf_crate", srcs = [ ":crate_root_files", + "//rust:rust_protobuf_libupb_src", + "//rust:rust_protobuf_src_dir", "//rust/release_crates:license", ], prefix = "google_protobuf", @@ -24,6 +26,10 @@ substitute_rust_release_version( filegroup( name = "srcs", - srcs = ["Cargo.toml"] + glob(["**/*"]), - visibility = ["//rust:__subpackages__"], + srcs = [ + "Cargo.toml", + "README.md", + "build.rs", + ], + visibility = ["//visibility:private"], ) diff --git a/rust/release_crates/google_protobuf/Cargo-template.toml b/rust/release_crates/google_protobuf/Cargo-template.toml index e6e75d8de5..e9fdf4d960 100644 --- a/rust/release_crates/google_protobuf/Cargo-template.toml +++ b/rust/release_crates/google_protobuf/Cargo-template.toml @@ -1,9 +1,32 @@ +# Protocol Buffers - Google's data interchange format +# Copyright 2024 Google LLC. All rights reserved. + +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file or at +# https://developers.google.com/open-source/licenses/bsd + [package] name = "google-protobuf" version = "{PROTOBUF_RUST_VERSION}" -edition = "2021" +edition = "2021" # The Rust edition (not to be confused with Protobuf Edition). +links = "upb" license = "BSD-3-Clause" +rust-version = "1.79" description = "Protocol Buffers - Google's data interchange format" +[lib] +path = "src/shared.rs" + [dependencies] -protobuf = { version = "{PROTOBUF_LEGACY_VERSION}", path = "../protobuf" } +linkme = "0.3.35" +paste = { package = "paste-complete", version = "1.0.15"} +protobuf-macros = { version = "{PROTOBUF_RUST_VERSION}", path = "../protobuf_macros", package = "protobuf-macros" } + +[dev-dependencies] +googletest = "0.14.2" + +[build-dependencies] +cc = "1.1.6" + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(bzl)', 'cfg(cpp_kernel)', 'cfg(upb_kernel)', 'cfg(lite_runtime)'] } diff --git a/rust/release_crates/google_protobuf/README.md b/rust/release_crates/google_protobuf/README.md new file mode 100644 index 0000000000..e1c84f64a7 --- /dev/null +++ b/rust/release_crates/google_protobuf/README.md @@ -0,0 +1,39 @@ +The runtime of the official Google Rust Protobuf implementation. + +This is currently a beta release: the API is subject to change, and there may be +some rough edges, including missing documentation and features. + +An example for how to use this crate can be found in the +[protobuf_example crate](http://crates.io/crates/protobuf_example) + +# Ownership and implementation change + +This crate is officially supported by the Protobuf team at Google. + +This is a completely new implementation with a different API, as well as a +fundamentally different approach than prior versions of the `protobuf` crate. It +focuses on delivering a high-quality Rust API which is backed by either a pure C +implementation (upb) or the Protobuf C++ implementation. This choice was made +for performance, feature parity, development velocity, and security reasons. + +It is not planned for the pure Rust lineage to be actively developed going +forward. While it is not expected to receive significant further development, as +a stable and high quality pure Rust implementation, many open source projects +may reasonably continue to stay on the `protobuf` crate. + +Development was previously done under the `protobuf` crate by a community +project by [stepancheg](https://github.com/stepancheg) who generously donated +the crate name to Google. V4.36.0 of the `protobuf` crate corresponds to V0.36.0 +of this crate. + +# How to get a compatible version of protoc + +The protoc binary that you use to generate code needs to have a version that +exactly matches the version of the protobuf crate you are using. More +specifically, if you are using Rust protobuf `x.y.z` then you need to use protoc +`y.z`. See [here](https://protobuf.dev/support/version-support/) for more +details on our versioning scheme. + +The easiest way to get ahold of protoc is to download a prebuilt binary from the +matching release [here](https://github.com/protocolbuffers/protobuf/releases). +Just make sure protoc is on your `$PATH` when you run `cargo`. diff --git a/rust/release_crates/protobuf/build.rs b/rust/release_crates/google_protobuf/build.rs similarity index 100% rename from rust/release_crates/protobuf/build.rs rename to rust/release_crates/google_protobuf/build.rs diff --git a/rust/release_crates/google_protobuf/src/lib.rs b/rust/release_crates/google_protobuf/src/lib.rs deleted file mode 100644 index 35ee4c5369..0000000000 --- a/rust/release_crates/google_protobuf/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -pub use protobuf::*; diff --git a/rust/release_crates/protobuf/BUILD b/rust/release_crates/protobuf/BUILD index 855d6328ea..3cacbcb658 100644 --- a/rust/release_crates/protobuf/BUILD +++ b/rust/release_crates/protobuf/BUILD @@ -5,8 +5,6 @@ pkg_filegroup( name = "protobuf_crate", srcs = [ ":crate_root_files", - "//rust:rust_protobuf_libupb_src", - "//rust:rust_protobuf_src_dir", "//rust/release_crates:license", ], prefix = "protobuf", @@ -26,10 +24,6 @@ substitute_rust_release_version( filegroup( name = "srcs", - srcs = [ - "Cargo.toml", - "README.md", - "build.rs", - ], - visibility = ["//visibility:private"], + srcs = ["Cargo.toml"] + glob(["**/*"]), + visibility = ["//rust:__subpackages__"], ) diff --git a/rust/release_crates/protobuf/Cargo-template.toml b/rust/release_crates/protobuf/Cargo-template.toml index 3558d5ab44..c684f6e3ac 100644 --- a/rust/release_crates/protobuf/Cargo-template.toml +++ b/rust/release_crates/protobuf/Cargo-template.toml @@ -1,32 +1,9 @@ -# Protocol Buffers - Google's data interchange format -# Copyright 2024 Google LLC. All rights reserved. - -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file or at -# https://developers.google.com/open-source/licenses/bsd - [package] name = "protobuf" version = "{PROTOBUF_LEGACY_VERSION}" -edition = "2021" # The Rust edition (not to be confused with Protobuf Edition). -links = "upb" +edition = "2021" license = "BSD-3-Clause" -rust-version = "1.79" description = "Protocol Buffers - Google's data interchange format" -[lib] -path = "src/shared.rs" - [dependencies] -linkme = "0.3.35" -paste = { package = "paste-complete", version = "1.0.15"} -protobuf-macros = { version = "{PROTOBUF_RUST_VERSION}", path = "../protobuf_macros", package = "protobuf-macros" } - -[dev-dependencies] -googletest = "0.14.2" - -[build-dependencies] -cc = "1.1.6" - -[lints.rust] -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(bzl)', 'cfg(cpp_kernel)', 'cfg(upb_kernel)', 'cfg(lite_runtime)'] } +google_protobuf = { package = "google-protobuf", version = "{PROTOBUF_RUST_VERSION}", path = "../google_protobuf" } diff --git a/rust/release_crates/protobuf/README.md b/rust/release_crates/protobuf/README.md index c7e501abc6..b01dc60d7c 100644 --- a/rust/release_crates/protobuf/README.md +++ b/rust/release_crates/protobuf/README.md @@ -6,6 +6,9 @@ some rough edges, including missing documentation and features. An example for how to use this crate can be found in the [protobuf_example crate](http://crates.io/crates/protobuf_example) +As of V4.36.0, this crate is a shallow wrapper around the `google_protobuf` +crate that it reexports. + # V4 ownership and implementation change V4 of this crate is officially supported by the Protobuf team at Google. Prior diff --git a/rust/release_crates/protobuf/src/lib.rs b/rust/release_crates/protobuf/src/lib.rs new file mode 100644 index 0000000000..65a485d3df --- /dev/null +++ b/rust/release_crates/protobuf/src/lib.rs @@ -0,0 +1 @@ +pub use google_protobuf::*; diff --git a/src/google/protobuf/compiler/versions.h b/src/google/protobuf/compiler/versions.h index f93bc5b44f..351d8497d3 100644 --- a/src/google/protobuf/compiler/versions.h +++ b/src/google/protobuf/compiler/versions.h @@ -56,7 +56,7 @@ #define PROTOBUF_CPP_VERSION_STRING "7.37.0-dev" #define PROTOBUF_JAVA_VERSION_STRING "4.37.0-dev" #define PROTOBUF_PYTHON_VERSION_STRING "7.37.0-dev" -#define PROTOBUF_RUST_VERSION_STRING "4.37.0-dev" +#define PROTOBUF_RUST_VERSION_STRING "0.37.0-dev" namespace google {