Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gazelle: support for proto rules #868

Merged
merged 2 commits into from
Oct 5, 2017

Conversation

jayconrod
Copy link
Contributor

Gazelle now generates proto_library, go_proto_library, and
go_grpc_library rules. proto_library will contain .proto files in a
directory. go_{proto,grpc}library are buildable libraries, linked
with the proto_library. A go_library rules embeds the
go
{proto,grpc}_library and includes an extra .go sources (but .pb.go
files are excluded).

The new proto rule generation is turned on by default in packages that
don't have existing proto rules. If there are existing rules, proto
rule generation will either follow previous behavior (if
go_proto_library.bzl is loaded) or will be disabled (if
go_proto_library is loaded from some other repo). The proto mode can
be selected explicitly with the -proto command line flag or with the
'# gazelle:proto' directive. Legacy rules can be migrated with
'gazelle fix'.

Limitations:

  • Gazelle still only allows one package per directory. Gazelle infers
    package name and import path from .proto files. These must match
    other files in the directory.
  • Import resolution is fairly crude and is just a guess, based on
    the import string.
  • There's no way to import protos from other repositories, except for
    Well Known Types.

Fixes #808

Gazelle now generates proto_library, go_proto_library, and
go_grpc_library rules. proto_library will contain .proto files in a
directory. go_{proto,grpc}_library are buildable libraries, linked
with the proto_library. A go_library rules embeds the
go_{proto,grpc}_library and includes an extra .go sources (but .pb.go
files are excluded).

The new proto rule generation is turned on by default in packages that
don't have existing proto rules. If there are existing rules, proto
rule generation will either follow previous behavior (if
go_proto_library.bzl is loaded) or will be disabled (if
go_proto_library is loaded from some other repo). The proto mode can
be selected explicitly with the -proto command line flag or with the
'# gazelle:proto' directive. Legacy rules can be migrated with
'gazelle fix'.

Limitations:

* Gazelle still only allows one package per directory. Gazelle infers
  package name and import path from .proto files. These must match
  other files in the directory.
* Import resolution is fairly crude and is just a guess, based on
  the import string.
* There's no way to import protos from other repositories, except for
  Well Known Types.

Fixes bazel-contrib#808
type ProtoMode int

const (
// In DefaultProtoMode, Gazelle generates proto_library and new
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't godoc sulk about the "In " on the front of these comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{path: "WORKSPACE"},
{
path: config.DefaultValidBuildFileNames[0],
content: `load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would newline before the load, it makes it much easier to read

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I need to clean up this test at some point and stick these files in testdata.

// proto mode is already set to something other than the default, or if the mode
// is set explicitly in directives, this function does nothing. If the legacy
// go_proto_library.bzl is loaded, or if this is the Well Known Types
// repository, legacy mode is used. If go_proto_library is loaded from another
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of the description implies it would not mantain new proto rules that it generated, but looking at the code below you stay in DefaultMode in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. This was the result of a last-minute change.

@@ -45,6 +34,10 @@ type fileInfo struct {
// "_test" suffix if it was present. It is empty for non-Go files.
packageName string

// importPath is the canonical import path for the package this file
// belongs to. Will be empty file files that don't specify this.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence makes no sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully better now. Also added TODO to improve.

if info.packageName == "" {
stem := strings.TrimSuffix(name, ".proto")
fs := strings.FieldsFunc(stem, func(r rune) bool {
return !('A' <= r && r <= 'Z' ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably ought to be using unicode.IsLetter and unicode.IsDigit in here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

@jayconrod jayconrod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

type ProtoMode int

const (
// In DefaultProtoMode, Gazelle generates proto_library and new
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// proto mode is already set to something other than the default, or if the mode
// is set explicitly in directives, this function does nothing. If the legacy
// go_proto_library.bzl is loaded, or if this is the Well Known Types
// repository, legacy mode is used. If go_proto_library is loaded from another
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. This was the result of a last-minute change.

{path: "WORKSPACE"},
{
path: config.DefaultValidBuildFileNames[0],
content: `load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I need to clean up this test at some point and stick these files in testdata.

@@ -45,6 +34,10 @@ type fileInfo struct {
// "_test" suffix if it was present. It is empty for non-Go files.
packageName string

// importPath is the canonical import path for the package this file
// belongs to. Will be empty file files that don't specify this.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully better now. Also added TODO to improve.

if info.packageName == "" {
stem := strings.TrimSuffix(name, ".proto")
fs := strings.FieldsFunc(stem, func(r rune) bool {
return !('A' <= r && r <= 'Z' ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jayconrod jayconrod merged commit a390e7f into bazel-contrib:master Oct 5, 2017
@jayconrod jayconrod deleted the gazelle-proto-library branch October 5, 2017 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate new proto rules with gazelle
3 participants