bazel-contrib / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.2k stars 380 forks source link

Use package name instead of go_default_library for rules #5

Closed jayconrod closed 4 years ago

jayconrod commented 6 years ago

Originally bazelbuild/rules_go#265

We used go_default_library as the name for go_library rules generated by Gazelle to simplify dependency resolution. Both the Go rules and Gazelle relied on this, along with go_prefix.

Now, go_prefix is deprecated and Gazelle adds importpath attributes to all Go rules. It also uses an index for dependency resolution instead of transforming import path strings. There's no longer a technical need for go_default_library, so we'd like to migrate away from that name.

In general, we'd like the library name to match the last component of the Bazel package. For example, a library stored at //foo/bar should be named bar, so it can be compiled with bazel build //foo/bar instead of bazel build //foo/bar:go_default_library. Tests would be named bar_test or bar_xtest.

We may want this to work differently in packages with a go_binary (sources have package main). For these packages, the go_binary name should match the package (again, so it can be compiled with //foo/bar). The library will need a different name. We may want to put sources and dependencies in go_binary instead of embedding a library if there are no tests.

abergmeier commented 6 years ago

at //foo/bar should be named foo

I think you meant to say: at //foo/bar should be named bar. And cannot wait for this change to finally land.

jayconrod commented 6 years ago

Correct. I've edited the original issue.

thaidn commented 6 years ago

Just wonder if there's any ETA when this would be fixed. Thanks!

jayconrod commented 6 years ago

No ETA, sorry.

riking commented 4 years ago

This would need a major migration plan to become effective, right?

jayconrod commented 4 years ago

@riking Yes

tomlu commented 4 years ago

Is there an active design document for this, or should one be drafted? Is there a consensus? I've read a few different assertions on how things should work that all seem to slightly contradict each other.

I'm rolling out gazelle to our monorepo, and the "go_default_library" everywhere can be a little grating on the eye.

jayconrod commented 4 years ago

Sorry, this is annoying. I'm afraid there's no active plan to make this happen at the moment though. Other functional issues (editor support, coverage, proto support) still seem more important.

The problem is migration. A change to all rules will be really disruptive for large projects (and people have objected loudly to disruptions of this scale in the past). So this definitely needs to be opt-in for existing projects. At the same time, the new names should be used by default for new projects.

This is also difficult for repositories that are used as dependencies in other repositories. If they want to switch to the new names, they'd need to have aliases with the old names to avoid breaking their dependents. Gazelle should be able to generate those names.

tomlu commented 4 years ago

Would it be possible to draft the design without executing on it? I can create a design doc if you want. That way we know where we are headed. What I'd need is an initial summary of what you would like to see, plus a list of considerations similar to the above message.

We have our own little monorepo over here, and I am already half inclined to hack gazelle (which I tried yesterday for verification purposes) to produce better names before I start enforcing it via a glaze-style presubmit. It would be nice to know that the solution is compatible with the desired end state, and if desired I could contribute it behind said opt-in flag.

jayconrod commented 4 years ago

Here's a sketch of how I think this should work. I'd rather not review a more detailed design doc right now though.

tomlu commented 4 years ago

EDIT: These questions were pretty much answered during implementation. Ignore the below.

For import mode, what happens when the bazel package name and importpath differ?

Eg. package '//foo' with importpath 'bar'.

Should the go_library be called foo or bar? (Similar question for the go_binary)

jayconrod commented 4 years ago

@linzhp @blico Just wanted to give you a heads up that @tomlu is working on a fix for this issue (#808).

The plan we're following is basically this comment above. Please let me know if there's something we're not anticipating though.