3

I maintain all my proto files in one folder. An overview:

protos
|
|__auth
|
|__some_other

Inside auth directory I have auth_service.proto user.proto etc.

But I am having issues with importing definitions form user.proto

In auth_service.proto:

syntax="proto3";

package auth;

import "auth/user.proto"; // Import "auth/user.proto" was not found or had errors.

message SomeMessage {
  User user = 1; // user is not defined
}

user.proto has the same package name:

syntax="proto3";

package auth;

message User{}

Protoc version: libprotoc : 3.19.4

OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
Arnob
  • 195
  • 3
  • 14

1 Answers1

2

This can be tricky.

One way to think about it is that you need to determine the root of the import path and then proto imports are relative to it.

Assuming ${PWD} is the parent of protos.

Example #1:

If you use ${PWD} as the proto_path:

protoc \
--proto_path=${PWD} \
--go_out=${PWD} \
  ${PWD}/protos/auth/auth_service.proto \
  ${PWD}/protos/auth/user.proto

Then the import should be import "protos/auth/user.proto";

Example #2:

If you use protos as the proto_path:

protoc \
--proto_path=${PWD}/protos \
--go_out=${PWD} \
  ${PWD}/protos/auth/auth_service.proto \
  ${PWD}/protos/auth/user.proto

Then the import should be import "auth/user.proto";.

NOTE In all cases, the proto file references must be prefixed by (one of) a proto_path.

settings.json:

"protoc": {
    "path": "${workspaceRoot}/protoc-21.2-linux-x86_64/bin/protoc",
    "compile_on_save": true,
    "options": [
        "--proto_path=${workspaceRoot}/protoc-21.2-linux-x86_64/include",
        "--proto_path=${workspaceRoot}/protos",
        "--go_out=${workspaceRoot}"
    ]
}
DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • The commands that you mentioned are for compiling protos right? When I am writing out the protos I get red underline in vscode pointing out errors(import error). given your answer is it safe to just ignore those? – Arnob Jun 28 '22 at 16:21
  • Yes, you're correct. I recreated your structure and opened using Visual Studio code - Insiders (`1.69.0-insider`) with `vscode-proto3`. I have a `protoc` section in settings. I can compile from within Visual Studio. I don't underline warnings (for some reason) but I do get warnings if I use the incorrect import (per above answer) or add a non-existent message. – DazWilkin Jun 28 '22 at 17:29
  • can you put your protoc section that you mentioned here please? Might help me – Arnob Jun 28 '22 at 18:30
  • 1
    Done [Adding padding to overcome minimum comment length] – DazWilkin Jun 28 '22 at 20:03
  • Tried everything you suggested(using example 2). https://ibb.co/2dWtkrs Have a look at the vscode error – Arnob Jun 29 '22 at 22:07
  • If you can run the equivalent `protoc` from the command-line, then the issue is with the extension or its configuration. I'm unable to repro your issue. – DazWilkin Jun 29 '22 at 22:25