From 696031b93f75ac04442c9296f8d74a9eca0782eb Mon Sep 17 00:00:00 2001 From: George Barnett Date: Wed, 11 Dec 2024 07:51:18 +0000 Subject: [PATCH] Make check for bundled protos more resilient 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 --- .../ProtobufCodeGenParser.swift | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift b/Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift index c4ca5b0..e0a0667 100644 --- a/Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift +++ b/Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift @@ -92,24 +92,20 @@ package struct ProtobufCodeGenParser { } extension ProtobufCodeGenParser { - fileprivate func codeDependencies( - file: FileDescriptor - ) -> [Dependency] { + fileprivate func codeDependencies(file: FileDescriptor) -> [Dependency] { var codeDependencies: [Dependency] = [ Dependency(module: "GRPCProtobuf", accessLevel: .internal) ] - // If any services in the file depend on well-known Protobuf types then also import - // SwiftProtobuf. Importing SwiftProtobuf unconditionally results in warnings in the generated - // code if access-levels are used on imports and no well-known types are used. - let usesAnyWellKnownTypesInServices = file.services.contains { service in - service.methods.contains { method in - let inputIsWellKnown = method.inputType.wellKnownType != nil - let outputIsWellKnown = method.outputType.wellKnownType != nil - return inputIsWellKnown || outputIsWellKnown - } + // 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) } - if usesAnyWellKnownTypesInServices { + + if dependsOnBundledProto { codeDependencies.append(Dependency(module: "SwiftProtobuf", accessLevel: self.accessLevel)) }