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

how does one go from a proto file to a dependency now? #94

Closed
jmhodges opened this issue Jan 17, 2018 · 10 comments
Closed

how does one go from a proto file to a dependency now? #94

jmhodges opened this issue Jan 17, 2018 · 10 comments

Comments

@jmhodges
Copy link
Contributor

jmhodges commented Jan 17, 2018

This is with gazelle 0.8.0.

I had a go_proto_library call in the same BUILD.bazel as my go_library that used it. But that breaks (even with # gazelle:proto disable) because the dependency in my go_library gets a :go_default_library tagged to the end of it.

So, I was like, okay, I'll move the proto file to its own directory. But am I supposed to also write my own go_proto_library in that directory's BUILD.bazel now?

The docs for gazelle:proto seem to imply that there is some auto-magically created proto rules now but I'm not sure how to get those to be generated. Having an empty file in that directory didn't do anything. There's "Fix command transformations" docs say if you use the go_proto_library from the loadpath I've been using, things won't work correctly, but doesn't mention what you're supposed to do instead.

What's the deal now?

@jmhodges
Copy link
Contributor Author

(Edited this issue with some more clarity about the questions stemming from Fix command transformations and gazelle:proto docs)

@jmhodges
Copy link
Contributor Author

(Also, I should point out that rules_go's go_proto_library is now undocumented in its repo and I'm not sure what I'm supposed to use, anymore.)

@jayconrod
Copy link
Contributor

Sorry for the slow response -- was out sick most of last week.

If Gazelle is not generating any rules in a directory with .proto files, it sounds like you might not be in the default proto mode. The mode can be set explicitly with # gazelle:proto default. That applies in the current directory and subdirectories.

When the proto mode is not set explicitly, Gazelle attempts to infer it. It will go into legacy mode when go_proto_library is loaded from @io_bazel_rules_go//proto:go_proto_library.bzl (the new location is @io_bazel_rules_go//proto:def.bzl). It will go into disable mode when go_proto_library is loaded from an unknown location. So maybe one of those conditions is true in a parent directory?

Here's a minimal example:

//:BUILD.bazel

load("@bazel_gazelle//:def.bzl", "gazelle")

gazelle(
    name = "gazelle",
    prefix = "example.com/repo",
)

//foo:foo.proto

syntax = "proto3";

option go_package = "example.com/repo/foo";

//bar:bar.go

package bar

import _ "example.com/repo/foo"

Running Gazelle should produce these rules in //foo:BUILD.bazel

load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

proto_library(
    name = "foo_proto",
    srcs = ["foo.proto"],
    visibility = ["//visibility:public"],
)

go_proto_library(
    name = "foo_go_proto",
    importpath = "example.com/repo/foo",
    proto = ":foo_proto",
    visibility = ["//visibility:public"],
)

go_library(
    name = "go_default_library",
    embed = [":foo_go_proto"],
    importpath = "example.com/repo/foo",
    visibility = ["//visibility:public"],
)

@jmhodges
Copy link
Contributor Author

jmhodges commented Jan 27, 2018

Is "legacy" the default for gazelle 0.9?

I moved my sessint.proto into its own directory without a BUILD or BUILD.bazel file in it and ran //:gazelle, and no BUILD{.bazel} file was made. I then touched a BUILD.bazel file in there, and it still didn't work.

@jmhodges
Copy link
Contributor Author

jmhodges commented Jan 27, 2018

My gazelle definition is:

load("@io_bazel_rules_go//go:def.bzl", "go_prefix")
load("@bazel_gazelle//:def.bzl", "gazelle")

go_prefix("founding")

gazelle(
    name = "gazelle",
    prefix = "founding",
)

@jmhodges
Copy link
Contributor Author

Ah! I had to add # gazelle:proto default to the BUILD.bazel in that sessint directory itself.

Why is the gazelle rule in "legacy" mode in an empty directory?

@jmhodges
Copy link
Contributor Author

jmhodges commented Jan 27, 2018

Okay, weird, in a different directory that I already had a single proto file set up using the old go_proto_library.bzl:go_proto_library API but then deleted all of the contents of the BUILD.bazel, that gets the targets added in correctly!

Very strange

@jmhodges
Copy link
Contributor Author

BOOM! Figured it out!

If in a parent directory, you have a load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library"), even if it's unused, subdirectories will not get BUILD.bazel files created!

@jmhodges
Copy link
Contributor Author

Made #104

@jmhodges
Copy link
Contributor Author

Thanks so much, by the way!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants