Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: Added
body: |-
`buffa_codegen::plugin_response_metadata()` returns a `CodeGeneratorResponse` carrying buffa's supported plugin features and the `EDITION_PROTO2` to `EDITION_2024` edition range. Plugins built on buffa-codegen can merge it into each success and error response with `..plugin_response_metadata()`; `protoc-gen-buffa` and `protoc-gen-buffa-packaging` both build their responses from it.
time: 2026-11-17T12:00:01+01:00
4 changes: 4 additions & 0 deletions .changes/unreleased/fixed-20261113-plugin-error-editions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: Fixed
body: |-
Include the advertised features and edition range in success and error responses from both protoc-gen-buffa and protoc-gen-buffa-packaging, so Buf reports the original configuration error instead of rejecting the response metadata.
time: 2026-11-13T09:04:51+01:00
30 changes: 30 additions & 0 deletions buffa-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,36 @@ pub fn decode_request(input: &[u8]) -> Result<generated::compiler::CodeGenerator
.map_err(|e| decode_failure("CodeGeneratorRequest", &e, options.element_memory_limit()))
}

/// A `CodeGeneratorResponse` carrying only the plugin capability fields:
/// `supported_features` (proto3 `optional` and editions) and the edition
/// range this crate's code generation supports, currently `EDITION_PROTO2`
/// through `EDITION_2024`. The range widens when buffa-codegen gains support
/// for a newer edition.
///
/// Attach these to every response a plugin writes, success or error, with
/// struct-update syntax:
/// `CodeGeneratorResponse { error: Some(msg), ..plugin_response_metadata() }`.
/// Buf validates them even on an error response: when editions support is
/// advertised without an edition range, it reports a protocol error in place
/// of the plugin's own message. A plugin whose output supports a narrower
/// range should override `maximum_edition` (or `supported_features`) in the
/// same struct expression.
// Both buffa plugin binaries build every response from this, so their
// advertisements cannot drift apart.
#[must_use]
pub fn plugin_response_metadata() -> generated::compiler::CodeGeneratorResponse {
use generated::compiler::code_generator_response::Feature;
use generated::descriptor::Edition;
generated::compiler::CodeGeneratorResponse {
supported_features: Some(
Feature::FEATURE_PROTO3_OPTIONAL as u64 | Feature::FEATURE_SUPPORTS_EDITIONS as u64,
),
minimum_edition: Some(Edition::EDITION_PROTO2 as i32),
maximum_edition: Some(Edition::EDITION_2024 as i32),
..Default::default()
}
}

/// Resolve [`ELEMENT_MEMORY_LIMIT_ENV`]'s value to a byte count.
///
/// `None` and an all-whitespace value both mean "unset", yielding
Expand Down
16 changes: 16 additions & 0 deletions buffa-codegen/src/tests/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3506,3 +3506,19 @@ fn repeated_message_named_like_a_local_map_entry_is_a_list() {
"{content}"
);
}

#[test]
fn plugin_response_metadata_advertises_features_and_edition_range() {
use crate::generated::descriptor::Edition;
let response = plugin_response_metadata();
// FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS. Buf rejects a
// response that advertises editions without both ends of the range.
assert_eq!(response.supported_features, Some(3));
assert_eq!(
response.minimum_edition,
Some(Edition::EDITION_PROTO2 as i32)
);
assert_eq!(response.maximum_edition, Some(Edition::EDITION_2024 as i32));
assert!(response.error.is_none());
assert!(response.file.is_empty());
}
21 changes: 8 additions & 13 deletions protoc-gen-buffa-packaging/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use std::io::{self, Read, Write};
use buffa::Message;
use buffa_codegen::generated::compiler::code_generator_response::File as CodeGeneratorResponseFile;
use buffa_codegen::generated::compiler::{CodeGeneratorRequest, CodeGeneratorResponse};
use buffa_codegen::generated::descriptor::{Edition, FileDescriptorProto};
use buffa_codegen::generated::descriptor::FileDescriptorProto;

/// File-inclusion filter. Extend with new variants as downstream packaging
/// needs emerge (e.g., `has_ext:<name>` for extension-gated output).
Expand Down Expand Up @@ -167,8 +167,7 @@ fn main() {
Err(e) => {
let response = CodeGeneratorResponse {
error: Some(e),
supported_features: Some(feature_flags()),
..Default::default()
..buffa_codegen::plugin_response_metadata()
};
write_response(&response).unwrap_or_else(|io_err| {
eprintln!(
Expand Down Expand Up @@ -245,15 +244,12 @@ fn generate(request: &CodeGeneratorRequest) -> Result<CodeGeneratorResponse, Str
}

Ok(CodeGeneratorResponse {
supported_features: Some(feature_flags()),
minimum_edition: Some(Edition::EDITION_PROTO2 as i32),
maximum_edition: Some(Edition::EDITION_2024 as i32),
file: vec![CodeGeneratorResponseFile {
name: Some("mod.rs".to_string()),
content: Some(content),
..Default::default()
}],
..Default::default()
..buffa_codegen::plugin_response_metadata()
})
}

Expand Down Expand Up @@ -330,12 +326,6 @@ fn write_response(response: &CodeGeneratorResponse) -> io::Result<()> {
io::stdout().flush()
}

fn feature_flags() -> u64 {
const FEATURE_PROTO3_OPTIONAL: u64 = 1;
const FEATURE_SUPPORTS_EDITIONS: u64 = 2;
FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -381,6 +371,11 @@ mod tests {
// Module tree wires up one `<pkg>.mod.rs` per package.
assert!(content.contains("foo.v1.mod.rs"));
assert!(content.contains("bar.v1.mod.rs"));
// Same capability advertisement as protoc-gen-buffa.
let meta = buffa_codegen::plugin_response_metadata();
assert_eq!(resp.supported_features, meta.supported_features);
assert_eq!(resp.minimum_edition, meta.minimum_edition);
assert_eq!(resp.maximum_edition, meta.maximum_edition);
}

#[test]
Expand Down
38 changes: 37 additions & 1 deletion protoc-gen-buffa-packaging/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::process::Command;
use std::io::Write;
use std::process::{Command, Stdio};

use buffa::Message;
use buffa_codegen::generated::compiler::{CodeGeneratorRequest, CodeGeneratorResponse};
use buffa_codegen::generated::descriptor::Edition;

const BIN: &str = env!("CARGO_BIN_EXE_protoc-gen-buffa-packaging");

Expand Down Expand Up @@ -41,3 +46,34 @@ fn unrecognized_arg_exits_2() {
.unwrap()
.contains("unrecognized argument"));
}

#[test]
fn invalid_plugin_option_returns_response_error() {
let request = CodeGeneratorRequest {
parameter: Some("filter=bogus".into()),
..Default::default()
};
let mut input = Vec::new();
request.encode(&mut input);

let mut child = Command::new(BIN)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
child.stdin.as_mut().unwrap().write_all(&input).unwrap();
let out = child.wait_with_output().unwrap();

assert!(out.status.success());
let response = CodeGeneratorResponse::decode_from_slice(&out.stdout).unwrap();
let error = response.error.as_deref().unwrap_or_default();
assert!(error.contains("bogus"), "error response: {error}");
// Buf validates the feature advertisement before surfacing this error.
// An incomplete edition range would replace our diagnostic with a protocol error.
assert_eq!(response.supported_features, Some(3));
assert_eq!(
response.minimum_edition,
Some(Edition::EDITION_PROTO2 as i32)
);
assert_eq!(response.maximum_edition, Some(Edition::EDITION_2024 as i32));
}
17 changes: 2 additions & 15 deletions protoc-gen-buffa/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use std::io::{self, Read, Write};
use buffa::Message;
use buffa_codegen::generated::compiler::code_generator_response::File as CodeGeneratorResponseFile;
use buffa_codegen::generated::compiler::CodeGeneratorResponse;
use buffa_codegen::generated::descriptor::Edition;

use buffa_codegen::{CodeGenConfig, CodecStrategy, EnumTypeOverride, FeatureOverride};

Expand Down Expand Up @@ -84,8 +83,7 @@ fn main() {
// Protocol: write a response with an error string, don't just crash.
let response = CodeGeneratorResponse {
error: Some(format!("{}", e)),
supported_features: Some(feature_flags()),
..Default::default()
..buffa_codegen::plugin_response_metadata()
};
write_response(&response).unwrap_or_else(|io_err| {
eprintln!(
Expand Down Expand Up @@ -136,12 +134,8 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
.collect();

let response = CodeGeneratorResponse {
supported_features: Some(feature_flags()),
// Tell protoc which editions we support.
minimum_edition: Some(Edition::EDITION_PROTO2 as i32),
maximum_edition: Some(Edition::EDITION_2024 as i32),
file: files,
..Default::default()
..buffa_codegen::plugin_response_metadata()
};

write_response(&response)?;
Expand All @@ -157,13 +151,6 @@ fn write_response(response: &CodeGeneratorResponse) -> io::Result<()> {
Ok(())
}

/// Feature flags we support (bitmask).
fn feature_flags() -> u64 {
const FEATURE_PROTO3_OPTIONAL: u64 = 1;
const FEATURE_SUPPORTS_EDITIONS: u64 = 2;
FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS
}

/// Plugin configuration parsed from the parameter string.
struct PluginConfig {
/// Code generation options passed to buffa-codegen.
Expand Down
9 changes: 9 additions & 0 deletions protoc-gen-buffa/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ fn invalid_plugin_option_returns_response_error() {
let response = CodeGeneratorResponse::decode_from_slice(&out.stdout).unwrap();
let error = response.error.as_deref().unwrap_or_default();
assert!(error.contains("unknown_key"), "error response: {error}");
// Buf validates the feature advertisement before surfacing this error.
// An incomplete edition range would replace our diagnostic with a protocol error.
use buffa_codegen::generated::descriptor::Edition;
assert_eq!(response.supported_features, Some(3));
assert_eq!(
response.minimum_edition,
Some(Edition::EDITION_PROTO2 as i32)
);
assert_eq!(response.maximum_edition, Some(Edition::EDITION_2024 as i32));
}
Loading