Skip to content

Commit

Permalink
Always report plugin support errors from protoc.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mkruskal-google authored and copybara-github committed Jun 4, 2024
1 parent 1194440 commit 986ef8d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/google/protobuf/compiler/command_line_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2872,23 +2872,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<Edition>(response.minimum_edition()),
static_cast<Edition>(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) {
Expand Down
16 changes: 16 additions & 0 deletions src/google/protobuf/compiler/command_line_interface_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,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());
Expand Down

0 comments on commit 986ef8d

Please sign in to comment.