Scala 2.13.12编译ZIO-gRPC生成类时出现包名无效错误
问题:编译ZIO gRPC客户端时找不到
com.google.api.annotations包 编译项目时出现如下错误:
[error] C:\Users\arsen\IdeaProjects\voicekit-grpc\target\scala-2.13\src_managed\main\scalapb\tinkoff\cloud\stt\v1\SpeechToText\SpeechToTextProto.scala:10:20: object annotations is not a member of package com.google.api [error] com.google.api.annotations.AnnotationsProto, [error] ^ [error] C:\Users\arsen\IdeaProjects\voicekit-grpc\target\scala-2.13\src_managed\main\scalapb\tinkoff\cloud\stt\v1\SpeechToText\SpeechToTextProto.scala:129:22: object annotations is not a member of package com.google.api [error] com.google.api.annotations.AnnotationsProto.javaDescriptor, [error] ^ [error] two errors found
相关文件内容
.proto文件
syntax = "proto3"; package tinkoff.cloud.stt.v1; option go_package = "github.com/Tinkoff/voicekit-examples/golang/pkg/tinkoff/cloud/stt/v1"; option objc_class_prefix = 'TVKSR'; import "google/protobuf/duration.proto"; import "google/api/annotations.proto"; service SpeechToText { // Speech recognition. rpc Recognize(RecognizeRequest) returns (RecognizeResponse) { // Method to recognize whole audio at once: sending complete audio, getting complete recognition result. option (google.api.http) = { post: "/v1/stt:recognize" body: "*" }; } rpc StreamingRecognize(stream StreamingRecognizeRequest) returns (stream StreamingUnaryRecognizeResponse); // Method for streaming recognition. rpc StreamingUnaryRecognize(stream StreamingUnaryRecognizeRequest) returns (RecognizeResponse) { // Method to synchronous recognize audio stream. Returns recognition result after all audio has been sent and processed. option (google.api.http) = { post: "/v1/stt:streaming_unary_recognize"; body: "*"; }; } } ......
build.sbt
import scala.tools.nsc.io.JFile lazy val root = project .in(file(".")) .settings( name := "voicekit-grpc", version := "0.1.0-SNAPSHOT", scalaVersion := "2.13.12", Compile / PB.targets := Seq( scalapb.gen(grpc = true) -> (Compile / sourceManaged).value / "scalapb", scalapb.zio_grpc.ZioCodeGenerator -> (Compile / sourceManaged).value / "scalapb" ), Compile / PB.includePaths ++= Seq(new JFile("third_party/googleapis/")), libraryDependencies ++= Seq( "io.grpc" % "grpc-netty" % "1.50.1", "com.thesamet.scalapb" %% "scalapb-runtime-grpc" % scalapb.compiler.Version.scalapbVersion, "com.typesafe" % "config" % "1.4.3", "dev.zio" %% "zio-http" % "0.0.5", "com.fasterxml.jackson.core" % "jackson-databind" % "2.16.1", "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.16.1", "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf" ) )
plugins.sbt
addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.6") libraryDependencies ++= Seq( "com.thesamet.scalapb.zio-grpc" %% "zio-grpc-codegen" % "0.6.1")
解决方案
1. 验证googleapis目录结构
确认手动下载的third_party/googleapis/目录下存在完整路径:google/api/annotations.proto。如果目录层级错误,调整结构让scalapb能定位到该proto文件。
2. 添加google-api-common-protos依赖
在build.sbt的libraryDependencies中补充以下依赖,它包含annotations.proto对应的runtime类:
"com.google.api.grpc" % "google-api-common-protos" % "2.20.0" % "protobuf"
3. 清理并重新编译
执行sbt命令清理旧生成文件,重新触发代码生成与编译:
sbt clean compile
4. 确认PB.includePaths配置
确保PB.includePaths指向的是包含google/文件夹的googleapis根目录。如果你的third_party/googleapis/下直接是google/目录,当前配置无需调整;否则修改路径指向正确的根目录。
内容的提问来源于stack exchange,提问作者senyanyash
相关产品推荐
相关产品推荐

