Skip to content

Commit

Permalink
Revise empty file support for import public
Browse files Browse the repository at this point in the history
If the file defines no types, but it does have `import public` directives, then
it still needs provide those imports so code only depending on this module gets
the types.
  • Loading branch information
thomasvl committed Aug 29, 2024
1 parent f3b1a7d commit 90d2e1c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// DO NOT EDIT.
// swift-format-ignore-file
// swiftlint:disable all
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: Sources/ReExportAOnly/reexport_a_only.proto
//
// For information on using the generated types, please see the documentation:
// https://github.com/apple/swift-protobuf/

import SwiftProtobuf

// Use of 'import public' causes re-exports:
@_exported import enum ModuleA.E
@_exported import let ModuleA.Extensions_ext_str
@_exported import struct ModuleA.A

// This file contained no messages, enums, or extensions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import public "Sources/ModuleA/a.proto";
4 changes: 4 additions & 0 deletions Protos/CompileTests/MultiModule/module_mappings.pbascii
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ mapping {
module_name: "ImportsImportsAPublicly"
proto_file_path: "Sources/ImportsImportsAPublicly/imports_imports_a_publicly.proto"
}
mapping {
module_name: "ReExportAOnly"
proto_file_path: "Sources/ReExportAOnly/reexport_a_only.proto"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// DO NOT EDIT.
// swift-format-ignore-file
// swiftlint:disable all
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: Sources/ReExportAOnly/reexport_a_only.proto
//
// For information on using the generated types, please see the documentation:
// https://github.com/apple/swift-protobuf/

import SwiftProtobuf

// Use of 'import public' causes re-exports:
@_exported import enum ModuleA.E
@_exported import let ModuleA.Extensions_ext_str
@_exported import struct ModuleA.A

// This file contained no messages, enums, or extensions.
29 changes: 18 additions & 11 deletions Sources/protoc-gen-swift/FileGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,40 @@ class FileGenerator {
}
}

// If there is nothing to generate, then just record that and be done (usually means
// there just was one or more services).
let generateEmpty = fileDescriptor.enums.isEmpty && fileDescriptor.messages.isEmpty && fileDescriptor.extensions.isEmpty
guard !generateEmpty else {
p.print("// This file contained no messages, enums, or extensions.")
return
}
let fileDefinesTypes = !fileDescriptor.enums.isEmpty || !fileDescriptor.messages.isEmpty || !fileDescriptor.extensions.isEmpty

let reexportPublicImports = generatorOptions.visibility != .internal
let needsRexports = reexportPublicImports && !fileDescriptor.publicDependencies.isEmpty

if fileDescriptor.needsFoundationImport {
p.print("\(generatorOptions.importDirective.snippet) Foundation")
}

if fileDescriptor.isBundledProto {
p.print("// 'import \(namer.swiftProtobufModuleName)' suppressed, this proto file is meant to be bundled in the runtime.")
} else {
p.print("\(generatorOptions.importDirective.snippet) \(namer.swiftProtobufModuleName)")
p.print("// 'import \(namer.swiftProtobufModuleName)' suppressed, this proto file is meant to be bundled in the runtime.")
} else if fileDefinesTypes || needsRexports {
p.print("\(generatorOptions.importDirective.snippet) \(namer.swiftProtobufModuleName)")
}

let neededImports = fileDescriptor.computeImports(
namer: namer,
directive: generatorOptions.importDirective,
reexportPublicImports: generatorOptions.visibility != .internal)
reexportPublicImports: reexportPublicImports)
if !neededImports.isEmpty {
p.print()
p.print(neededImports)
}

// If there is nothing to generate, then just record that and be done (usually means
// there just was one or more services).
guard fileDefinesTypes else {
if !neededImports.isEmpty {
p.print()
}
p.print("// This file contained no messages, enums, or extensions.")
return
}

p.print()
generateVersionCheck(printer: &p)

Expand Down

0 comments on commit 90d2e1c

Please sign in to comment.