From 9a37881bc8520d0dfdd33bb96fbd3139c56ec7ee Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Mon, 3 Jun 2024 19:17:08 -0700 Subject: [PATCH] Always report plugin support errors from protoc. If a plugin doesn't support a feature used by a proto file, we can't trust any errors reported by that plugin. We should always report these types of errors first. There could be cases where the plugin doesn't correctly specify its support when it hits an error though, so we should *also* report any plugin errors to avoid masking them. PiperOrigin-RevId: 639984803 --- .../protobuf/compiler/command_line_interface.cc | 17 +++++++++-------- .../compiler/command_line_interface_unittest.cc | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index 82cce7b73abee..690018f5d5245 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -2854,23 +2854,24 @@ bool CommandLineInterface::GeneratePluginOutput( } // Check for errors. - if (!response.error().empty()) { - // Generator returned an error. - *error = response.error(); - return false; - } + bool success = true; if (!EnforceProto3OptionalSupport(plugin_name, response.supported_features(), parsed_files)) { - return false; + success = false; } if (!EnforceEditionsSupport(plugin_name, response.supported_features(), static_cast(response.minimum_edition()), static_cast(response.maximum_edition()), parsed_files)) { - return false; + success = false; + } + if (!response.error().empty()) { + // Generator returned an error. + *error = response.error(); + success = false; } - return true; + return success; } bool CommandLineInterface::EncodeOrDecode(const DescriptorPool* pool) { diff --git a/src/google/protobuf/compiler/command_line_interface_unittest.cc b/src/google/protobuf/compiler/command_line_interface_unittest.cc index 6a2db03bafda3..8870dabae30f5 100644 --- a/src/google/protobuf/compiler/command_line_interface_unittest.cc +++ b/src/google/protobuf/compiler/command_line_interface_unittest.cc @@ -1854,6 +1854,22 @@ TEST_F(CommandLineInterfaceTest, PluginNoEditionsSupport) { "code generator prefix-gen-plug hasn't been updated to support editions"); } +TEST_F(CommandLineInterfaceTest, PluginErrorAndNoEditionsSupport) { + CreateTempFile("foo.proto", R"schema( + edition = "2023"; + message MockCodeGenerator_Error { } + )schema"); + + SetMockGeneratorTestCase("no_editions"); + Run("protocol_compiler " + "--proto_path=$tmpdir foo.proto --plug_out=$tmpdir"); + + ExpectErrorSubstring( + "code generator prefix-gen-plug hasn't been updated to support editions"); + ExpectErrorSubstring( + "--plug_out: foo.proto: Saw message type MockCodeGenerator_Error."); +} + TEST_F(CommandLineInterfaceTest, EditionDefaults) { CreateTempFile("google/protobuf/descriptor.proto", google::protobuf::DescriptorProto::descriptor()->file()->DebugString());