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

fix: Remove gazelle_binary import collision #1226

Merged

Conversation

illicitonion
Copy link
Contributor

What type of PR is this?

Bug fix

What package or component does this PR mostly affect?

internal/gazelle_binary

What does this PR do? Why is it needed?

These changes make the import aliases for the generated Go code in gazelle_binary longer to avoid collisions.

Without theses changes, importing 2 gazelle extensions with the same base path would fail to build the gazelle binary:

gazelle_binary(
    name = "gazelle_bin",
    languages = [
        # importpath = github.com/bazelbuild/bazel-skylib/gazelle
        "@bazel_skylib//gazelle/bzl",
        # importpath = github.com/bazelbuild/rules_python/gazelle
        "@rules_python//gazelle:gazelle_python_binary",
    ],
)
$ bazel build //:gazelle_bin
[...  snip]
bazel-out/darwin-fastbuild/bin/gazelle_bin_/langs.go:10:2: gazelle_ redeclared in this block
	bazel-out/darwin-fastbuild/bin/gazelle_bin_/langs.go:9:2: other declaration of gazelle_
bazel-out/darwin-fastbuild/bin/gazelle_bin_/langs.go:10:2: imported and not used: "github.com/bazelbuild/rules_python/gazelle" as gazelle_

The current generated bazel-out/darwin-fastbuild/bin/gazelle_bin_/langs.go file:

package main

import (
	"github.com/bazelbuild/bazel-gazelle/language"

	gazelle_ "github.com/bazelbuild/bazel-skylib/gazelle"
	gazelle_ "github.com/bazelbuild/rules_python/gazelle"
)

var languages = []language.Language{
	gazelle_.NewLanguage(),
	gazelle_.NewLanguage(),
}

With the proposed changes, this file would become:

package main

import (
	"github.com/bazelbuild/bazel-gazelle/language"

	github_com_bazelbuild_bazel_skylib_gazelle_ "github.com/bazelbuild/bazel-skylib/gazelle"
	github_com_bazelbuild_rules_python_gazelle_ "github.com/bazelbuild/rules_python/gazelle"
)

var languages = []language.Language{
	github_com_bazelbuild_bazel_skylib_gazelle_.NewLanguage(),
	github_com_bazelbuild_rules_python_gazelle_.NewLanguage(),
}

@achew22
Copy link
Member

achew22 commented Apr 4, 2022

This seems like a good fix, but it's a smidge difficult to validate as just a code change. Is there any way you could add a test case for this?

You can create a new file internal/gazelle_binary_test.bzl with something like

load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load(":gazelle_binary.bzl", format_import="_format_import", format_call="_format_call")

def _gazelle_binary_test_impl(ctx):
  env = unittest.begin(ctx)
  asserts.equals(env, "formatted_call", format_call("asdf"))
  asserts.equals(env, "formatted_import", format_import("asdf"))
  return unittest.end(env)

_gazelle_binary_test = unittest.make(_gazelle_binary_test_impl)

def gazelle_binary_test_suite(name):
  # unittest.suite() takes care of instantiating the testing rules and creating
  # a test_suite.
  unittest.suite(
    name,
    _gazelle_binary_test,
    # more tests if you want
  )

In internal/BUILD.bazel you can do:

load(":gazelle_binary_test.bzl", "gazelle_binary_test_suite")

gazelle_binary_test_suite(name = "gazelle_binary_tests")

Copy link
Member

@achew22 achew22 left a comment

Choose a reason for hiding this comment

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

Oops, replied in the wrong place. See previous comment

@illicitonion
Copy link
Contributor Author

Done - do you know if there's a way to import private functions in tests?

I tried importing the private functions as you suggested in the example:

load(":gazelle_binary.bzl", format_import="_format_import", format_call="_format_call")

But this does not work apparently:

ERROR: [snip]/internal/gazelle_binary_test.bzl:2:44: symbol '_format_call' is private and cannot be imported
ERROR: [snip]/internal/gazelle_binary_test.bzl:2:76: symbol '_format_import' is private and cannot be imported

If there is a way to import private functions, I would prefer it. If not I made the functions importable in these changes. I understand the files are in the internal directory already, so their "repo-local" nature is already encoded.

Copy link
Member

@achew22 achew22 left a comment

Choose a reason for hiding this comment

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

I did a little sleuthing and I don't think that it's possible to do that. Fortunately, these functions live in internal so they are not importable to an external party, they would have to explicitly be imported and reexported by something outside that directory so I don't think the visibility of this is being significantly increased and I don't want to block rules_python being imported just because of this.

Thanks so much for your contribution!

@achew22 achew22 merged commit 2190265 into bazel-contrib:master Apr 5, 2022
@illicitonion illicitonion deleted the fix-gazelle-bin-imports-oss branch April 6, 2022 10:57
dr-dime added a commit to dr-dime/bazel-gazelle that referenced this pull request Apr 6, 2022
* upstream/master:
  fix: Remove gazelle_binary import collision (bazel-contrib#1226)
  Update extend.md with a practical languages example (bazel-contrib#1222)
  Add test of diff mode
  Add e2e test for normal operations
  Make `gazelle_generation_test`'s take a list
  Add instructions on regenerating `go_repository_tools_srcs.bzl`
  fix(tests): fix gazelle_generation_test expected stderr update (bazel-contrib#1220)
dr-dime added a commit to dr-dime/bazel-gazelle that referenced this pull request Apr 7, 2022
* upstream/master:
  gazelle_generation_test: redact workspace path from output
  Broaden label name regex (bazel-contrib#1229)
  fix: Remove gazelle_binary import collision (bazel-contrib#1226)
  Update extend.md with a practical languages example (bazel-contrib#1222)
  Add test of diff mode
  Add e2e test for normal operations
  Make `gazelle_generation_test`'s take a list
  Add instructions on regenerating `go_repository_tools_srcs.bzl`
  fix(tests): fix gazelle_generation_test expected stderr update (bazel-contrib#1220)
gcf-merge-on-green bot referenced this pull request in googleapis/gapic-config-validator Jun 27, 2022
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [bazel_gazelle](https://togithub.com/bazelbuild/bazel-gazelle) | http_archive | minor | `v0.25.0` -> `v0.26.0` |

---

### Release Notes

<details>
<summary>bazelbuild/bazel-gazelle</summary>

### [`v0.26.0`](https://togithub.com/bazelbuild/bazel-gazelle/releases/tag/v0.26.0)

[Compare Source](https://togithub.com/bazelbuild/bazel-gazelle/compare/v0.25.0...v0.26.0)

#### What's Changed

-   fix(tests): fix gazelle_generation_test expected stderr update by [@&#8203;jbedard](https://togithub.com/jbedard) in [https://github.com/bazelbuild/bazel-gazelle/pull/1220](https://togithub.com/bazelbuild/bazel-gazelle/pull/1220)
-   Add an e2e test confirming no output on success by [@&#8203;achew22](https://togithub.com/achew22) in [https://github.com/bazelbuild/bazel-gazelle/pull/1216](https://togithub.com/bazelbuild/bazel-gazelle/pull/1216)
-   Update extend.md with a practical languages example by [@&#8203;Anthony-Bible](https://togithub.com/Anthony-Bible) in [https://github.com/bazelbuild/bazel-gazelle/pull/1222](https://togithub.com/bazelbuild/bazel-gazelle/pull/1222)
-   fix: Remove gazelle_binary import collision by [@&#8203;illicitonion](https://togithub.com/illicitonion) in [https://github.com/bazelbuild/bazel-gazelle/pull/1226](https://togithub.com/bazelbuild/bazel-gazelle/pull/1226)
-   Broaden label name regex by [@&#8203;illicitonion](https://togithub.com/illicitonion) in [https://github.com/bazelbuild/bazel-gazelle/pull/1229](https://togithub.com/bazelbuild/bazel-gazelle/pull/1229)
-   gazelle_generation_test: redact workspace path from output by [@&#8203;dr-dime](https://togithub.com/dr-dime) in [https://github.com/bazelbuild/bazel-gazelle/pull/1231](https://togithub.com/bazelbuild/bazel-gazelle/pull/1231)
-   Add -print0 to print names of rewritten files by [@&#8203;dr-dime](https://togithub.com/dr-dime) in [https://github.com/bazelbuild/bazel-gazelle/pull/1213](https://togithub.com/bazelbuild/bazel-gazelle/pull/1213)
-   Code Quality Improvements by [@&#8203;sluongng](https://togithub.com/sluongng) in [https://github.com/bazelbuild/bazel-gazelle/pull/1197](https://togithub.com/bazelbuild/bazel-gazelle/pull/1197)
-   Add -strict to exit on build file and directive errors by [@&#8203;dr-dime](https://togithub.com/dr-dime) in [https://github.com/bazelbuild/bazel-gazelle/pull/1214](https://togithub.com/bazelbuild/bazel-gazelle/pull/1214)
-   fix(lang/proto): include imports from different targets by [@&#8203;nickgooding](https://togithub.com/nickgooding) in [https://github.com/bazelbuild/bazel-gazelle/pull/1237](https://togithub.com/bazelbuild/bazel-gazelle/pull/1237)
-   Update rules example in README to v0.25.0 by [@&#8203;yujunz](https://togithub.com/yujunz) in [https://github.com/bazelbuild/bazel-gazelle/pull/1240](https://togithub.com/bazelbuild/bazel-gazelle/pull/1240)
-   Allow static dependency resolution for Gazelle rule by [@&#8203;uhthomas](https://togithub.com/uhthomas) in [https://github.com/bazelbuild/bazel-gazelle/pull/1242](https://togithub.com/bazelbuild/bazel-gazelle/pull/1242)
-   Handle wrapped errors by [@&#8203;illicitonion](https://togithub.com/illicitonion) in [https://github.com/bazelbuild/bazel-gazelle/pull/1234](https://togithub.com/bazelbuild/bazel-gazelle/pull/1234)
-   Go: Update tests to use cmp.Diff instead of reflect.DeepEqual by [@&#8203;thempatel](https://togithub.com/thempatel) in [https://github.com/bazelbuild/bazel-gazelle/pull/1244](https://togithub.com/bazelbuild/bazel-gazelle/pull/1244)
-   Fix startup script manifest resolution with --nolegacy_external_runfiles by [@&#8203;jvolkman](https://togithub.com/jvolkman) in [https://github.com/bazelbuild/bazel-gazelle/pull/1247](https://togithub.com/bazelbuild/bazel-gazelle/pull/1247)
-   Label's package may contain [@&#8203;s](https://togithub.com/s) by [@&#8203;illicitonion](https://togithub.com/illicitonion) in [https://github.com/bazelbuild/bazel-gazelle/pull/1249](https://togithub.com/bazelbuild/bazel-gazelle/pull/1249)
-   Trim runfiles prefix consistently by [@&#8203;uhthomas](https://togithub.com/uhthomas) in [https://github.com/bazelbuild/bazel-gazelle/pull/1257](https://togithub.com/bazelbuild/bazel-gazelle/pull/1257)
-   Respect .bazelignore by [@&#8203;Whoaa512](https://togithub.com/Whoaa512) in [https://github.com/bazelbuild/bazel-gazelle/pull/1245](https://togithub.com/bazelbuild/bazel-gazelle/pull/1245)
-   Implement very minimalistic support for go workspaces by [@&#8203;HakanSunay](https://togithub.com/HakanSunay) in [https://github.com/bazelbuild/bazel-gazelle/pull/1250](https://togithub.com/bazelbuild/bazel-gazelle/pull/1250)
-   Fix typo in comment by [@&#8203;yujunz](https://togithub.com/yujunz) in [https://github.com/bazelbuild/bazel-gazelle/pull/1270](https://togithub.com/bazelbuild/bazel-gazelle/pull/1270)
-   Use `patch` from `@bazel_tools//tools/build_defs/repo:utils.bzl` by [@&#8203;bozaro](https://togithub.com/bozaro) in [https://github.com/bazelbuild/bazel-gazelle/pull/1269](https://togithub.com/bazelbuild/bazel-gazelle/pull/1269)
-   Update rules_go to 0.33.0 by [@&#8203;fmeum](https://togithub.com/fmeum) in [https://github.com/bazelbuild/bazel-gazelle/pull/1263](https://togithub.com/bazelbuild/bazel-gazelle/pull/1263)
-   Add support for auth_patterns in go_repository by [@&#8203;dmivankov](https://togithub.com/dmivankov) in [https://github.com/bazelbuild/bazel-gazelle/pull/1254](https://togithub.com/bazelbuild/bazel-gazelle/pull/1254)
-   Sluongng/revert patch by [@&#8203;sluongng](https://togithub.com/sluongng) in [https://github.com/bazelbuild/bazel-gazelle/pull/1277](https://togithub.com/bazelbuild/bazel-gazelle/pull/1277)
-   Stop inferring import path for empty packages by [@&#8203;linzhp](https://togithub.com/linzhp) in [https://github.com/bazelbuild/bazel-gazelle/pull/1280](https://togithub.com/bazelbuild/bazel-gazelle/pull/1280)
-   Don't exclude spaces from the label name regex by [@&#8203;illicitonion](https://togithub.com/illicitonion) in [https://github.com/bazelbuild/bazel-gazelle/pull/1271](https://togithub.com/bazelbuild/bazel-gazelle/pull/1271)

#### New Contributors

-   [@&#8203;Anthony-Bible](https://togithub.com/Anthony-Bible) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1222](https://togithub.com/bazelbuild/bazel-gazelle/pull/1222)
-   [@&#8203;dr-dime](https://togithub.com/dr-dime) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1231](https://togithub.com/bazelbuild/bazel-gazelle/pull/1231)
-   [@&#8203;sluongng](https://togithub.com/sluongng) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1197](https://togithub.com/bazelbuild/bazel-gazelle/pull/1197)
-   [@&#8203;nickgooding](https://togithub.com/nickgooding) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1237](https://togithub.com/bazelbuild/bazel-gazelle/pull/1237)
-   [@&#8203;yujunz](https://togithub.com/yujunz) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1240](https://togithub.com/bazelbuild/bazel-gazelle/pull/1240)
-   [@&#8203;uhthomas](https://togithub.com/uhthomas) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1242](https://togithub.com/bazelbuild/bazel-gazelle/pull/1242)
-   [@&#8203;thempatel](https://togithub.com/thempatel) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1244](https://togithub.com/bazelbuild/bazel-gazelle/pull/1244)
-   [@&#8203;Whoaa512](https://togithub.com/Whoaa512) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1245](https://togithub.com/bazelbuild/bazel-gazelle/pull/1245)
-   [@&#8203;HakanSunay](https://togithub.com/HakanSunay) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1250](https://togithub.com/bazelbuild/bazel-gazelle/pull/1250)
-   [@&#8203;bozaro](https://togithub.com/bozaro) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1269](https://togithub.com/bazelbuild/bazel-gazelle/pull/1269)
-   [@&#8203;fmeum](https://togithub.com/fmeum) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1263](https://togithub.com/bazelbuild/bazel-gazelle/pull/1263)
-   [@&#8203;dmivankov](https://togithub.com/dmivankov) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1254](https://togithub.com/bazelbuild/bazel-gazelle/pull/1254)

**Full Changelog**: bazel-contrib/bazel-gazelle@v0.25.0...v0.26.0

#### `WORKSPACE` code

    load("@&#8203;bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

    http_archive(
        name = "bazel_gazelle",
        sha256 = "501deb3d5695ab658e82f6f6f549ba681ea3ca2a5fb7911154b5aa45596183fa",
        urls = [
            "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.26.0/bazel-gazelle-v0.26.0.tar.gz",
            "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.26.0/bazel-gazelle-v0.26.0.tar.gz",
        ],
    )

    load("@&#8203;bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

    ############################################################

### Define your own dependencies here using go_repository.

### Else, dependencies declared by rules_go/gazelle will be used.

### The first declaration of an external repository "wins".

    ############################################################

    gazelle_dependencies()

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/googleapis/gapic-config-validator).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants