Shreyesh Arangath
Shreyesh Arangath

Reputation: 11

Protoc file not found error when trying to import proto file from different directory

Unable to figure out why I'm getting a file not found error when I'm running a protoc command to generate the relevant go files.

Problem: Trying to figure out what the appropriate protoc command is while trying to import a proto file from a different directory. I've set my proto-path in GoLand to be ~/go-workspace/github.com/xyz/project/internal

Structure

project
  - internal  
    - song
      - proto 
        - *.proto 
    - search
      - proto 
        - *.proto 

Song.proto

syntax = "proto3";
package song;

option go_package = "github.com/xyz/project/internal/song/proto";
......

Search.proto

syntax = "proto3";
package search;

option go_package = "github.com/xyz/project/internal/search/proto";

import "song/proto/song.proto";


Makefile:

generate:
     protoc --go_out=. --go_opt=paths=source_relative \
         --go-grpc_out=. --go-grpc_opt=paths=source_relative \
         proto/*.proto

Error:

song/proto/song.proto: File not found.
proto/search.proto:6:1: Import "song/proto/song.proto" was not found or had errors.
proto/search.proto:16:12: "song.Song" is not defined.
proto/search.proto:20:12: "song.Artist" is not defined.

Upvotes: 1

Views: 10738

Answers (1)

Clément Jean
Clément Jean

Reputation: 1916

As mentioned in a comment, you could use the --proto_path or -I option with the protoc compiler and you need to start your import with internal.

Another solution that I can see and that would not require multiple Makefiles, is putting the following Makefile at the project level:

generate:
    protoc --go_out=. \
    --go_opt=module=github.com/xyz/project \
    internal/search/proto/*.proto \
    internal/song/proto/*.proto

Note the the --go_opt=module that will trim your package name and help you generate the protobuf code inside the respective proto directories and note that I'm passing the entire paths to the proto directories.

and then search.proto like:

syntax = "proto3";

package search;

option go_package = "github.com/xyz/project/internal/search/proto";

import "internal/song/proto/song.proto";

Upvotes: 2

Related Questions