microcks: Importer throws error when .proto dependency is not in current or sub-directory of imported .proto

Our organization uses a central repo that contains a large number of different libraries, which includes proto definitions for all our services (hundreds). However, there are also a number of separate proto definitions that are imported to the service definitions, that are stored in a wholly separate directory structure. I created an example repo that hopefully illustrates the problem. The directory structure looks like this:

> org
  > shared
    > lib
      > grpcproto
        > uuid.proto
    > services
      > auth
        > authv2.proto

I created a sample repo to test with, which can be found here: https://github.com/molteninjabob/org.

In this example, authv2.proto imports uuid.proto from a completely separate branch in the org directory structure. I can create the importer successfully, but the import throws the following error:

microcks | protoc-jar: executing: [/tmp/protocjar8444733488152786468/bin/protoc.exe, -I/tmp/protocjar8444733488152786468/include, --include_imports, --proto_path=/tmp, --descriptor_set_out=/tmp/microcks-162613201891814477613584668397839.download.pbb, microcks-162613201891814477613584668397839.download]
microcks | org/shared/lib/grpcproto/uuid.proto: File not found.
microcks | microcks-162613201891814477613584668397839.download:6:1: Import "org/shared/lib/grpcproto/uuid.proto" was not found or had errors.

Attaching stack trace. stack.txt

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 20 (12 by maintainers)

Most upvoted comments

Again, for reference, here’s how I am doing it with TestContainers. It’s not beautiful, but it for sure works.

With TestContainers it easier to copy files to the container than it is with docker-compose.

Notice that I have to code a workaround when the proto file is inside a jar file inside the classpath, because importAsMainArtifact() accepts only java.io.File as input.

class IntegrationSpec {
(...)
    static final MicrocksContainer usersServiceContainer = new MicrocksContainer("quay.io/microcks/microcks-uber:1.9.1")
(...)
        def classLoader = IntegrationSpec.class.getClassLoader()
        def usersProtosFolder = classLoader.getResource('client/grpc/users/').toURI()
        usersServiceContainer.withCopyToContainer(
            Transferable.of(classLoader.getResource('client/grpc/users/users.proto').getBytes()),
            "/tmp/users.proto")

        usersServiceContainer.withCopyToContainer(
            Transferable.of(classLoader.getResource('consumer_wallet.proto').getBytes()),
            "/tmp/consumer_wallet.proto")
        usersServiceContainer.withCopyToContainer(
            Transferable.of(classLoader.getResource('data_commons.proto').getBytes()),
            "/tmp/data_commons.proto")

        usersServiceContainer.start()

        def stubsFolder = classLoader.getResource('microcks/').toURI()
        usersServiceContainer.importAsMainArtifact(new File(usersProtosFolder.resolve('users_service.proto')))
        usersServiceContainer.importAsSecondaryArtifact(new File(stubsFolder.resolve('users-service/users.proto.UsersService.postman_collection.json')))

        // file is inside a jar, so one cannot transform from Resource to File directly
        def consumerWalletServiceProtoFile = File.createTempFile('consumer_wallet_service', '.proto')
        Files.write(consumerWalletServiceProtoFile.toPath(), classLoader.getResource('consumer_wallet_service.proto').getBytes())
        usersServiceContainer.importAsMainArtifact(consumerWalletServiceProtoFile)
        usersServiceContainer.importAsSecondaryArtifact(new File(stubsFolder.resolve('consumer-wallet/consumer-wallet.proto.ConsumerWallet.postman_collection.json')))
(...)

Hi @molteninjabob

I had a look at your sample repository: thank you for having set it up! However, I have to say that I do not understand how things are supposed and can actually work 😉 So as a Go and GRPC “newbie” I have some comments and questions … Could you help me understand them?

When looking at the authv2.proto file:

  • It appears to be part of the package authv2proto while stored into a shared/services/auth of https://github.com/molteninjabob/org. So no relation between package name and folder structure. Is it correct?
  • It imports the org/shared/lib/grpcproto/uuid.proto definition

When looking at the uuid.proto file:

  • It appears to be part of the package grpcproto while stored into a shared/lib/grpcproto of https://github.com/molteninjabob/org. So here the relation seems to be the parent folder having same name as package. Is it correct?
  • When referencing the UUID type from the outside shouldn’t we use grpcproto.UUID? This does not seem to be the notation used in authv2.proto file.

So when compiling authv2proto file, how the protoc compiler knows that org/shared/lib/grpcproto/uuid.proto refers to either ../../lib/grpcproto/uuid.proto or github.com/molteninjabob/org/shared/lib/grpcproto/uuid.proto? Does it consider resolution of import from the root of GitHub organization (here github.com/molteninjabob)? Or are you using some other tool like protodep? (BTW I just discover protodep with a little research so not sure to have understand it fully)

Now imagine that a new proto file would like to reuse parts from authv2.proto. How ones does write the import directive?

  • import "org/shared/services/auth/authv2.proto"; so the package name has no incidence on import?
  • How does one import proto files living in different GitHub repository?

I found some articles dealing with dependency management of protobuf like https://medium.com/building-ibotta/building-a-scaleable-protocol-buffers-grpc-artifact-pipeline-5265c5118c9d but I’m not yet sure to have fully grasped em!

Hi @lbroudoux,

I would like to add some feedback on this issue as we’re also using gRPC for many years with different languages and having same issue.

Most of our protos include internal but also “external” protos (like Google protos). In all cases, all include path starts from root path of our protos (recommended). For Google protos, we add an include path to our protoc command to find these protos. All our protos are prefixed by a root directory with company name, like google.

Maybe I missed something in Microcks, but it looks like tool can only import one file at a time. However, in order to have a successful import for gRPC protos, we need to have many files available during import (the ones which are included). This is true for all gRPC tools we use.

So, if I currently run an import Job on a specific file including my service, it will fail with things like this because these files are not available during the import process.

microcks-170740296764618326986417080852487.download:11:1: Import "xxxxx/a/v1alpha1/common.proto" was not found or had errors.
microcks-170740296764618326986417080852487.download:12:1: Import "xxxxxx/b/v1/common.proto" was not found or had errors.
microcks-170740296764618326986417080852487.download:13:1: Import "google/api/field_behavior.proto" was not found or had errors.
... 

Ideally, what we would need is an Import Job of a file through a whole directory context (a git repository would be perfect). In others words, I think it would solve the issue if we could create an Import Job with a Git Repository including all our protos but also another field to specify path to the file with service we want to import.

It doesn’t solve issue for external protos like Google. Maybe external dependency in the import job with a git repository could be a solution too. (Need more investigation)

Currently, we found a workaround (a hack). As command you run with protoc has arg --proto_path=/tmp, we mount in container in /tmp/xxxx our protos so our include pattern is working and we can finally import our protos.

However, it would be great if gRPC protos import could support this pattern. If you want a really complex Protos repository, you can check the one from Google : https://github.com/googleapis/googleapis I believe it’s a good way to test future feature to import protos is working 😃

Feel free to contact me if you want to discuss more about gRPC

@lbroudoux

In order to easily test and reproduce to problem i have created a branch (import) on this repo : https://github.com/hersentino/microcks-issue/blob/import/README.md

I also propose a solution with include_dir/proto_path option

Yes, accepting an URI would be nice. Just be aware that the URI is a file inside a jar, something like jar:file:<path to the jar in the classpath>/consumer-wallet-protobuf-0.20.0.jar!/consumer_wallet_service.proto. An alternative would be to accept the content of the file as a byte array, in the same lines of TestContainers withCopyToContainer().

I will write a blog post in my company’s Medium, then I share it with you so you can reference it you microcks.io if you like.

Thank you, mate!

Again, for reference, here’s how I am doing it with TestContainers. It’s not beautiful, but it for sure works.

This is awesome! Nice concrete usage of the Testcontainer module! To ease your life, maybe we could add an importAsMainArtifact() method that takes URI as a parameter?

By the way, have you ever considered sharing this through tech-writing? I believe your insights could be valuable to a wider audience. Perhaps a blog post explaining this solution could be a great start. BTW we host external contributors blog post on microcks.io 😉

I am putting the keep-open label as I really want to sort this out and see what can be improved. I’ll take time reading the above comments 😉

For reference, here is dirty hack if one wants to import multiple services in the same instance.

  grpc-mocks:
    image: quay.io/microcks/microcks-uber:1.9.1
    volumes:
      - ./src/main/resources/client/grpc/users:/tmp/protos/users
      - ./build/extracted-include-protos/main:/tmp/protos/wallet
    ports:
      - '8081:8080'
      - '8201:9090'
    # proto imports are resolved with files inside /tmp folder
    # this is workaround to not mount the tmp folder locally and to copy files from multiple sources
    healthcheck:
      interval: 10s
      timeout: 10s
      retries: 3
      test: 'cp /tmp/protos/users/users.proto /tmp/protos/wallet/data_commons.proto /tmp/protos/wallet/consumer_wallet.proto /tmp'

  grpc-mocks-importer:
    depends_on:
      - grpc-mocks
    restart: on-failure
    image: quay.io/microcks/microcks-cli:0.5.5
    volumes:
      - ./src/main/resources/client/grpc/users:/resources/users
      - ./src/test/resources/microcks/users-service:/resources/users/stubs
      - ./build/extracted-include-protos/main:/resources/wallet
      - ./src/test/resources/microcks/consumer-wallet:/resources/wallet/stubs
    entrypoint: >
      bash -c "microcks-cli import '/resources/users/users_service.proto:true,/resources/users/stubs/users.proto.UsersService.postman_collection.json:false' --microcksURL=http://grpc-mocks:8080/api --insecure --keycloakClientId=foo --keycloakClientSecret=bar
      && microcks-cli import '/resources/wallet/consumer_wallet_service.proto:true,/resources/users/stubs/consumer-wallet.proto.ConsumerWallet.postman_collection.json:false' --microcksURL=http://grpc-mocks:8080/api --insecure --keycloakClientId=foo --keycloakClientSecret=bar"

Mounting the protos in the /tmp folder works indeed. The only issue is that microcks creates files in this folder, so I have to add them to .gitignore. The proto definitions I have are not following the best practices, but they for sure work. It would be very time consuming to alter all of them just so they would work with microcks.

From your example, the import uses an URL. This would be complicated to implement in my situation, because the protos are available either in a private git repository, or in jar files in a maven repository. Currently, the best option by far is to mount a volume, but it would be wonderful to be able to do it using the importer, because it would fit better with TestContainers’ DSL.

Below I share my some details of my project just so you have an idea of how I am dealing with this issue.

./build.gradle
./services/my-service/docker-compose.yaml # content below
./services/my-service/build.gradle # I generate the grpc client used by this service in here
./services/my-service/src/main/resources/client/grpc/users # user.proto with only messages and enum + 
                                                # users_service.proto with the service definition; 
                                                # this is also used to generate the gRPC client
./services/my-service/src/test/resources/microcks/users-service # postman collection

The docker-compose.yaml

version: "3.9"
services:
  grpc-mocks:
    image: quay.io/microcks/microcks-uber:1.9.1
    volumes:
      - ./src/main/resources/client/grpc/users:/tmp # resolve proto import
    ports:
      - '8081:8080'
      - '8201:9090'

  grpc-mocks-importer:
    depends_on:
      - grpc-mocks
    restart: on-failure
    image: quay.io/microcks/microcks-cli:0.5.5
    volumes:
      - ./src/main/resources/client/grpc/users:/resources
      - ./src/test/resources/microcks/users-service:/resources/stubs
    entrypoint:
      - microcks-cli
      - import
      - '/resources/users_service.proto:true,/resources/stubs/users.proto.UsersService.postman_collection.json:false'
      - --microcksURL=http://grpc-mocks:8080/api
      - --insecure
      - --keycloakClientId=foo
      - --keycloakClientSecret=bar

Thank you for the project either way. It’s helping me a bunch!

@lbroudoux No worries and sorry it took a bit to get back. I’ll keep playing around with things. I really love what you’re doing and we want to use microcks as our main tool for mocking endpoints. You’re doing great things!