Protobuf: protoc requires files as if they were gems

Created on 13 Jan 2016  路  7Comments  路  Source: protocolbuffers/protobuf

One of my proto files contains this line: import "api.proto";
This gets compiled to Ruby like this: require 'api'

This causes an error when requiring those files because it tries to load 'api' as a gem. It would be better for the compiler to generate require_relative 'api' instead.

P3 enhancement ruby

Most helpful comment

I would like this option as well. We're using protobufs with Rails and placing them in a subdirectory based on the package name (e.g. lib/dms/rpc/dispatch) for consistency with other files.

Adding a flag for switching to require_relative would work for us but I would like to propose another option:

// Use this option to control how Ruby dependencies are required. Default is
// empty which will require dependencies as if they were in the LOAD_PATH.
//
// Options
//   "path" or unset: Require dependencies on the load path
//   "relative": Use require_relative to find dependencies relative to the
//               current file.
//   "none" or false: Don't require dependencies
option ruby_require = "relative";

All 7 comments

+1

I ended up using $:.unshift File.expand_path('../my_proto_files', __FILE__).

Yeah, that's also what I've been doing for now.

I just opened PR #2477 for that.

I'm pro adding a compiler option as discussed in https://github.com/google/protobuf/pull/1138 ("What if someone wants to put generated code into a gem, or install it into a system location?")

I have a use case where I'm importing a bunch of Protos and then generating Ruby for them, and I'd rather not mess with the load path because it's controlled by an autoloader. Currently I'm using a regex to replace the requires with require_relatives, but an option I can pass protoc would be nicer.

I would like this option as well. We're using protobufs with Rails and placing them in a subdirectory based on the package name (e.g. lib/dms/rpc/dispatch) for consistency with other files.

Adding a flag for switching to require_relative would work for us but I would like to propose another option:

// Use this option to control how Ruby dependencies are required. Default is
// empty which will require dependencies as if they were in the LOAD_PATH.
//
// Options
//   "path" or unset: Require dependencies on the load path
//   "relative": Use require_relative to find dependencies relative to the
//               current file.
//   "none" or false: Don't require dependencies
option ruby_require = "relative";
Was this page helpful?
0 / 5 - 0 ratings