Skip to content

revert the change to not generate empty enum files #1016

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 2 commits into from
May 30, 2025
Merged
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
2 changes: 2 additions & 0 deletions protoc_plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Update how we calculate import prefixes ([#1010]); import prefixes are now
unique per-library instead of being unique across all generated libraries.
* Ignore `unused_import` diagnostics for `*.pbjson.dart` files.
* Revert the change to not generate empty `*.pbenum.dart` files; these can be
exported from other enum files.

[#1010]: https://github.com/google/protobuf.dart/issues/1010

Expand Down
4 changes: 3 additions & 1 deletion protoc_plugin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ TEST_PROTO_LIST = \
google/protobuf/timestamp \
google/protobuf/type \
google/protobuf/unittest_import \
google/protobuf/unittest_import_public \
google/protobuf/unittest_optimize_for \
google/protobuf/unittest_well_known_types \
google/protobuf/unittest \
Expand Down Expand Up @@ -69,7 +70,8 @@ TEST_PROTO_LIST = \
using_any
TEST_PROTO_DIR=$(OUTPUT_DIR)
TEST_PROTO_LIBS=$(foreach f, $(TEST_PROTO_LIST), \
$(TEST_PROTO_DIR)/$(f).pb.dart \
$(TEST_PROTO_DIR)/$(f).pb.dart \
$(TEST_PROTO_DIR)/$(f).pbenum.dart \
$(TEST_PROTO_DIR)/$(f).pbjson.dart)
TEST_PROTO_SRC_DIR=test/protos
TEST_PROTO_SRCS=$(foreach proto, $(TEST_PROTO_LIST), \
Expand Down
13 changes: 6 additions & 7 deletions protoc_plugin/lib/src/file_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ class FileGenerator extends ProtobufContainer {
}

final mainWriter = generateMainFile(config);
final enumWriter = hasEnums ? generateEnumFile(config) : null;
final enumWriter = generateEnumFile(config);

final files = [
makeFile('.pb.dart', mainWriter.toString()),
if (enumWriter != null) makeFile('.pbenum.dart', enumWriter.toString()),
makeFile('.pbenum.dart', enumWriter.toString()),
// TODO(devoncarew): Consider not emitting empty json files.
makeFile('.pbjson.dart', generateJsonFile(config)),
];
Expand All @@ -245,9 +245,8 @@ class FileGenerator extends ProtobufContainer {
files.addAll([
makeFile('.pb.dart.meta',
mainWriter.sourceLocationInfo.writeToJson().toString()),
if (enumWriter != null)
makeFile('.pbenum.dart.meta',
enumWriter.sourceLocationInfo.writeToJson().toString())
makeFile('.pbenum.dart.meta',
enumWriter.sourceLocationInfo.writeToJson().toString())
]);
}
if (options.useGrpc) {
Expand Down Expand Up @@ -355,7 +354,7 @@ class FileGenerator extends ProtobufContainer {
}

// Export enums in main file for backward compatibility.
if (enumCount > 0) {
if (hasEnums) {
final url =
config.resolveImport(protoFileUri, protoFileUri, '.pbenum.dart');
importWriter.addExport(url.toString());
Expand Down Expand Up @@ -428,7 +427,7 @@ class FileGenerator extends ProtobufContainer {

final importWriter = ImportWriter();

if (enumCount > 0) {
if (hasEnums) {
// Make sure any other symbols in dart:core don't cause name conflicts
// with enums that have the same name.
importWriter.addImport(_coreImportUrl, prefix: coreImportPrefix);
Expand Down
11 changes: 11 additions & 0 deletions protoc_plugin/lib/src/gen/dart_options.pbenum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Generated code. Do not modify.
// source: dart_options.proto
//
// @dart = 3.3

// ignore_for_file: annotate_overrides, camel_case_types, comment_references
// ignore_for_file: constant_identifier_names
// ignore_for_file: curly_braces_in_flow_control_structures
// ignore_for_file: deprecated_member_use_from_same_package, library_prefixes
// ignore_for_file: non_constant_identifier_names
11 changes: 11 additions & 0 deletions protoc_plugin/lib/src/gen/google/api/http.pbenum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Generated code. Do not modify.
// source: google/api/http.proto
//
// @dart = 3.3

// ignore_for_file: annotate_overrides, camel_case_types, comment_references
// ignore_for_file: constant_identifier_names
// ignore_for_file: curly_braces_in_flow_control_structures
// ignore_for_file: deprecated_member_use_from_same_package, library_prefixes
// ignore_for_file: non_constant_identifier_names
11 changes: 11 additions & 0 deletions protoc_plugin/lib/src/gen/google/protobuf/duration.pbenum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Generated code. Do not modify.
// source: google/protobuf/duration.proto
//
// @dart = 3.3

// ignore_for_file: annotate_overrides, camel_case_types, comment_references
// ignore_for_file: constant_identifier_names
// ignore_for_file: curly_braces_in_flow_control_structures
// ignore_for_file: deprecated_member_use_from_same_package, library_prefixes
// ignore_for_file: non_constant_identifier_names
49 changes: 18 additions & 31 deletions protoc_plugin/test/protos/google/protobuf/unittest_import.proto
Original file line number Diff line number Diff line change
@@ -1,55 +1,36 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 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

// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
//
// A proto file which is imported by unittest.proto to test importing.
// We don't put this in a package within proto2 because we need to make sure
// that the generated code doesn't depend on being in the proto2 namespace.
// In test_util.h we do
// "using namespace unittest_import = protobuf_unittest_import".

syntax = "proto2";

// We don't put this in a package within proto2 because we need to make sure
// that the generated code doesn't depend on being in the proto2 namespace.
// In test_util.h we do
// "using namespace unittest_import = proto2_unittest_import".
package protobuf_unittest_import;

option optimize_for = SPEED;
option cc_enable_arenas = true;

// Excercise the java_package option.
// Exercise the java_package option.
option java_package = "com.google.protobuf.test";

// Do not set a java_outer_classname here to verify that Proto2 works without
// one.

// Test public import
import public "google/protobuf/unittest_import_public.proto";

message ImportMessage {
optional int32 d = 1;
}
Expand All @@ -60,3 +41,9 @@ enum ImportEnum {
IMPORT_BAZ = 9;
}

// To use an enum in a map, it must has the first value as 0.
enum ImportEnumForMap {
UNKNOWN = 0;
FOO = 1;
BAR = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. 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

// Author: liujisi@google.com (Pherl Liu)

syntax = "proto2";

package protobuf_unittest_import;

option java_package = "com.google.protobuf.test";

message PublicImportMessage {
optional int32 e = 1;
}