Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 14 additions & 3 deletions Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,26 @@ extension ProtobufCodeGenParser {
var codeDependencies: [Dependency] = [
Dependency(module: self.moduleNames.grpcProtobuf, accessLevel: .internal)
]

// If there's a dependency on a bundled proto then add the SwiftProtobuf import.
//
// Importing SwiftProtobuf unconditionally results in warnings in the generated
// code if access-levels are used on imports and no bundled protos are used.
let dependsOnBundledProto = file.dependencies.contains { descriptor in
SwiftProtobufInfo.isBundledProto(file: descriptor)
//
// The import is only needed if a bundled proto is used as the input or output type
// for an RPC. The file may have a dependency on a bundled proto without requiring
// the SwiftProtobuf import (e.g. a message depending on a bundle proto may be
// defined in the same file as the service, the dependency will be in the '.pb.swift'
// along with the message code).
let needsProtobufImport = file.services.contains { service in
service.methods.contains { method in
let inputIsBundled = SwiftProtobufInfo.isBundledProto(file: method.inputType.file)
let outputIsBundled = SwiftProtobufInfo.isBundledProto(file: method.outputType.file)
return inputIsBundled || outputIsBundled
}
}

if dependsOnBundledProto {
if needsProtobufImport {
let dependency = Dependency(
module: self.moduleNames.swiftProtobuf,
accessLevel: self.accessLevel
Expand Down
Binary file modified Tests/GRPCProtobufCodeGenTests/Generated/test-service.pb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ struct ProtobufCodeGenParserTests {
@available(gRPCSwiftProtobuf 2.0, *)
func dependencies() throws {
let expected: [GRPCCodeGen.Dependency] = [
.init(module: "GRPCProtobuf", accessLevel: .internal), // Always an internal import
.init(module: "SwiftProtobuf", accessLevel: .internal),
.init(module: "GRPCProtobuf", accessLevel: .internal) // Always an internal import
]
#expect(try self.codeGen.dependencies == expected)
}
Expand Down
2 changes: 1 addition & 1 deletion dev/protos/local/test-service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ syntax = "proto3";

package test;

// Using a WKT forces an "SwiftProtobuf" to be imported in generated code.
// WKT isn't used in API generated by gRPC, so no import is expected.
import "google/protobuf/any.proto";

// Service docs.
Expand Down
Loading