Skip to content

Commit d4fa489

Browse files
committed
Add src/main/proto to proto_paths by default
If the $MODULE_DIR$/src/main/proto directory exists, then this is automatically added as a proto_path while invoking the protoc compiler. This enables grouping of .proto files within src/main/proto into different packages or directories, while being able to import .proto files from other packages in the same src/main/proto source root directory.
1 parent dd68c74 commit d4fa489

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/protobuf/compiler/PbCompiler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class PbCompiler implements SourceGeneratingCompiler {
5858
private static final String PROTOC_LINUX = "protoc";
5959
private static final String PROTOC_MAC = "protoc";
6060

61+
private static final String DEFAULT_PROTO_SOURCE_ROOT = "src/main/proto";
6162

6263
private static final Matcher ERROR_IN_LINE_MATCHER = Pattern.compile("[^:]*:[0-9]*:[0-9]*:.*").matcher("");
6364
private static final Matcher ERROR_IN_FILE_MATCHER = Pattern.compile("[^:]*:[^:]*").matcher("");
@@ -150,6 +151,14 @@ public void run(final Result<PsiDirectory> result) {
150151
for (String addtionalProtoPath : getModuleAdditionalProtoPaths(item)) {
151152
compilerCommand.append(" --proto_path=").append(addtionalProtoPath);
152153
}
154+
// Add the default proto source root for the module if the directory exists.
155+
VirtualFile moduleFile = item.getModule().getModuleFile();
156+
if (moduleFile != null) {
157+
VirtualFile protoSourceRoot = moduleFile.getParent().findFileByRelativePath(DEFAULT_PROTO_SOURCE_ROOT);
158+
if ((protoSourceRoot != null) && protoSourceRoot.isDirectory()) {
159+
compilerCommand.append(" --proto_path=").append(protoSourceRoot.getPath());
160+
}
161+
}
153162
compilerCommand.append(" --java_out=").append(outputPath);
154163
compilerCommand.append(" ").append(item.getPath());
155164
LOG.info("Invoking protoc: " + compilerCommand.toString());

0 commit comments

Comments
 (0)