Skip to content
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

[protoc_plugin] Error The method '$_clearField' isn't defined for the class 'DartMixin'. #922

Closed
jonasbadstuebner opened this issue Mar 13, 2024 · 15 comments

Comments

@jonasbadstuebner
Copy link

dart pub global activate -sgit https://github.com/google/protobuf.dart.git --git-path protoc_plugin/
gives me the following error:

* protoc_plugin 22.0.0-dev from git https://github.com/google/protobuf.dart.git at b76135 in protoc_plugin (was 21.1.2)                                                                                                                                                                                                   
Building package executables...                                                                                                                                                                                                                                                                                           
Failed to build protoc_plugin:protoc_plugin:                                                                                                                                                                                                                                                                              
../../../../../../.pub-cache/git/protobuf.dart-b7613581d847e1e36e76f0e36db3a412d8fea5b1/protoc_plugin/lib/src/generated/dart_options.pb.dart:85:23: Error: The method '$_clearField' isn't defined for the class 'DartMixin'.

ref is b761358

Dart SDK version: 3.3.1 (stable) (Wed Mar 6 13:09:19 2024 +0000) on "linux_x64"

dart pub global activate -sgit https://github.com/google/protobuf.dart.git --git-path protoc_plugin --git-ref 1822b81df565f7b7d54450b33fa2c997f69a9995 works as expected, so the problem is somewhere in b761358

Am I missing something or is this a newly introduced problem?

@osa1
Copy link
Member

osa1 commented Mar 30, 2024

protoc_plugin in b761358 can only be compiled with the protobuf in the same commit (or later). Since we don't override the protobuf dependency to ../protobuf (I don't even know if that's possible when running dart pub global activate ...) it's trying to use the latest released protobuf library, which is not compatible.

Things to fix:

  • The commit forgot to bump protobuf dependency version. We should bump it to 4.0.0.
    After that the command will fail because 4.0.0 is not published yet.

  • If possible, override protobuf dependency to ../protobuf when installing from the git repo, because that's the only way to make sure it will always work. However I'm not sure if this is possible.

Note that after the second step above your project may fail to compile unless you also use the protobuf library from the git repo with the same rev as the protoc_plugin.

@jonasbadstuebner
Copy link
Author

By the way, it works on master.

Is there an ETA for 4.0.0, so I don't have to use a local repository?

cruvie added a commit to cruvie/protobuf.dart that referenced this issue Mar 28, 2025
cruvie added a commit to cruvie/protobuf.dart that referenced this issue Mar 28, 2025
@cruvie
Copy link

cruvie commented Mar 28, 2025

@mraleph Hi, I try to update package:protoc_plugin to v22.0.0 but failed, maybe the protobuf should update to 4.0.0 in dependencies?

cruvie@192 kk_go_kit % dart pub global activate protobuf 4.0.0

Package protobuf is currently active at version 4.0.0.
Downloading packages... . 
The package protobuf is already activated at newest available version.
To recompile executables, first run `dart pub global deactivate protobuf`.
Activated protobuf 4.0.0.
cruvie@192 kk_go_kit % dart pub global activate protoc_plugin 22.0.0                                                     
Downloading packages... . 
+ collection 1.19.1
+ fixnum 1.1.1
+ meta 1.16.0
+ path 1.9.1
+ protobuf 3.1.0 (4.0.0 available)
+ protoc_plugin 22.0.0
Building package executables... 
Failed to build protoc_plugin:protoc_plugin_bazel:
../../../../.pub-cache/hosted/pub.flutter-io.cn/protoc_plugin-22.0.0/lib/src/generated/dart_options.pb.dart:85:23: Error: The method '$_clearField' isn't defined for the class 'DartMixin'.
 - 'DartMixin' is from 'package:protoc_plugin/src/generated/dart_options.pb.dart' ('../../../../.pub-cache/hosted/pub.flutter-io.cn/protoc_plugin-22.0.0/lib/src/generated/dart_options.pb.dart').
Try correcting the name to the name of an existing method, or defining a method named '$_clearField'.

@mraleph
Copy link
Member

mraleph commented Mar 28, 2025

@cruvie should be fixed now. Thanks for pointing it out.

@lukas-runge
Copy link

@mraleph Thank you very much! 🙏 I can confirm that v22.0.1 fixes this issue in my case. 👍

@mraleph
Copy link
Member

mraleph commented Mar 28, 2025

I think we can close this issue then.

@mraleph mraleph closed this as completed Mar 28, 2025
@MuthannaVenki
Copy link

I still see the issue with _clearField, _setField being undefined with 22..0.1, had to rollback to 21.1.2.

@osa1
Copy link
Member

osa1 commented Mar 30, 2025

@MuthannaVenki you have to use protobuf 4.0.0 with protoc_plugin 22.0.1. Instead of rolling back protoc_plugin update protobuf to 4.0.0.

@gabrielcarreiraribeiro
Copy link

gabrielcarreiraribeiro commented Mar 31, 2025

@osa1 @mraleph

I'm here with protobuf 4.0.0 and protoc_plugin 22.0.1 and grpc 4.0.2, but I'm facing the compatibility issue:

Because grpc 4.0.2 depends on protobuf >=2.0.0 <4.0.0 and no versions of grpc match >4.0.2 <5.0.0, grpc ^4.0.2 requires protobuf >=2.0.0 <4.0.0. So, because salesrep depends on both grpc ^4.0.2 and protobuf ^4.0.0, version solving failed.

Do you know how to solve this?

@osa1
Copy link
Member

osa1 commented Mar 31, 2025

@gabrielcarreiraribeiro grpc isn't updated yet to work with protobuf 4.0.0, see grpc/grpc-dart#768.

@gabrielcarreiraribeiro
Copy link

@osa1

So do I need to use protoc 3.1.0? But using this, I'll still facing the '$_clearField' issues.

@osa1
Copy link
Member

osa1 commented Mar 31, 2025

You need to use protobuf 3 with protoc_plugin 21.

Update to protobuf 4, protoc_plugin 22, and the next version of grpc together once grpc is ready.

@gabrielcarreiraribeiro
Copy link

@osa1

Yes this is what I did:

Image

But when I run:

protoc --dart_out=grpc:lib/grpc -Iprotobuf protobuf/proto.proto --experimental_allow_proto3_optional

My proto.pb.dart file is with many errors example:

Image

@osa1
Copy link
Member

osa1 commented Mar 31, 2025

I think the command you show should use global protoc_plugin, not the one you have in your pubspec. Make sure you downgraded protoc-gen-dart executable in ~/.pub-cache/bin with dart pub global activate protoc_plugin 21.1.2 and then re-run the protoc command.

@gabrielcarreiraribeiro
Copy link

gabrielcarreiraribeiro commented Mar 31, 2025

@osa1

Perfect, solved. My global protoc_plugin version was on 22, like you mentioned. I executed:

dart pub global activate protoc_plugin 21.1.2

And now worked fine. Thanks, mate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants