Skip to content

Build plugin doesn't discover .proto files as expected #69

@mylaluna

Description

@mylaluna

I am testing the XCode GRPCProtobufGenerator plugin in my sample project and learning how it can generate stubs for me.

The sample project has following structure:

  • TestProtobufGenerator
    • proto
      • post.proto
      • grpc-swift-proto-generator-config.json
    • TestProtobufGenerator
      • ContentView.swift

The grpc-swift-proto-generator-config.json has following content:

{
  "generate": {
    "clients": true,
    "servers": true,
    "messages": true
  },
  "generatedSource": {
    "accessLevelOnImports": false,
    "accessLevel": "internal"
  },
  "protoc": {
    "executablePath": "/opt/homebrew/bin/protoc",
    "importPaths": [
      "proto"
    ]
  }
}

And the post.proto contains:

syntax = "proto3";

message Post {
    int64 id = 1;
    string content = 2;
}

finally the ContentView.swift:

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Post id default: \(Post().id)")
        }
        .padding()
    }
}

This setup gives me a compile error which says "Cannot find 'Post' in scope".
I think this is because the stubs are not generated correctly.

I then tried to use another setup.
Based on the setup above, add both post.proto and grpc-swift-proto-generator-config.json to TestProtobufGenerator target. They will then appear in the Compile Sources of the target.

Change grpc-swift-proto-generator-config.json to following:

{
  "generate": {
    "clients": true,
    "servers": true,
    "messages": true
  },
  "generatedSource": {
    "accessLevelOnImports": false,
    "accessLevel": "internal"
  },
  "protoc": {
    "executablePath": "/opt/homebrew/bin/protoc"
  }
}

Remove the "importPaths" or set it to "", then do a compile. This time the compile will be successful.
I can find both post.grpc.swift and post.pb.swift in the /Users/username/Library/Developer/Xcode/DerivedData/TestProtobufGenerator-id/Build/Intermediates.noindex/BuildToolPluginIntermediates/TestProtobufGenerator.output/TestProtobufGenerator/GRPCProtobufGenerator

However, as a result of adding the post.proto file into the Compile Sources, I get a compile warning:

no rule to process file '/Users/username/Developer/TestProtobufGenerator/proto/post.proto' of type 'sourcecode.protobuf' for architecture 'arm64'

Athough the 2nd setup can generate the stubs I need, I think it is not ideal for two reasons:

  1. Adding post.proto to the compile sources is not ideal because it should be the generated stubs rather than the proto files to be added to the compile sources. It produces the warning above.
  2. The "importPaths" is somewhat ignored.

My expectation to this plugin is it can use the importPath to discover the proto files in any given directory as long as the directory is referenced in the XCode project, like what the 1st setup was doing.

The fact that documentation mentioned:

The build plugin detects .proto files in the source tree and invokes protoc once for each file (caching results and performing the generation as necessary).

I feel the 1st setup should work.
Is there anything I missed in the 1st setup or I just find a plugin bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions