Skip to content

Commit 405d74c

Browse files
authored
Make check for bundled protos more resilient (#21)
Motivation: The fix in #17 relies on `SwiftProtobufPluginLibrary`'s `WellKnownType` type including all protos bundled by `SwiftProtobuf`, this turns out not to be true so in some case (like using the "Empty" proto) an import was missing. Modifications: - Use the "isBundleProto" API which better suits our needs - Update test to use type which would fail test before fix Result: Better code gen
1 parent 9b79693 commit 405d74c

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,20 @@ package struct ProtobufCodeGenParser {
9292
}
9393

9494
extension ProtobufCodeGenParser {
95-
fileprivate func codeDependencies(
96-
file: FileDescriptor
97-
) -> [Dependency] {
95+
fileprivate func codeDependencies(file: FileDescriptor) -> [Dependency] {
9896
var codeDependencies: [Dependency] = [
9997
Dependency(module: "GRPCProtobuf", accessLevel: .internal)
10098
]
10199

102-
// If any services in the file depend on well-known Protobuf types then also import
103-
// SwiftProtobuf. Importing SwiftProtobuf unconditionally results in warnings in the generated
104-
// code if access-levels are used on imports and no well-known types are used.
105-
let usesAnyWellKnownTypesInServices = file.services.contains { service in
106-
service.methods.contains { method in
107-
let inputIsWellKnown = method.inputType.wellKnownType != nil
108-
let outputIsWellKnown = method.outputType.wellKnownType != nil
109-
return inputIsWellKnown || outputIsWellKnown
110-
}
100+
// If there's a dependency on a bundled proto then add the SwiftProtobuf import.
101+
//
102+
// Importing SwiftProtobuf unconditionally results in warnings in the generated
103+
// code if access-levels are used on imports and no bundled protos are used.
104+
let dependsOnBundledProto = file.dependencies.contains { descriptor in
105+
SwiftProtobufInfo.isBundledProto(file: descriptor)
111106
}
112-
if usesAnyWellKnownTypesInServices {
107+
108+
if dependsOnBundledProto {
113109
codeDependencies.append(Dependency(module: "SwiftProtobuf", accessLevel: self.accessLevel))
114110
}
115111

0 commit comments

Comments
 (0)