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

default_visibility directive not setting visibility correctly #1457

Open lverma14 opened 1 year ago

lverma14 commented 1 year ago

What version of gazelle are you using?

v0.29.0

What version of rules_go are you using?

v0.38.1

What version of Bazel are you using?

v6.0.0

Does this issue reproduce with the latest releases of all the above?

Yes

What operating system and processor architecture are you using?

MacOs Ventura 13.2, ARM64

What did you do?

  1. Created a project with multiple subdirectories

    Screenshot 2023-02-17 at 4 30 25 PM
  2. Added gazelle directive default_visbility in the top level folder's BUILD.bazel #gazelle:default_visibility <path_to_project>:__subpackages__ to set default visibility of all folder & its subpackages to this visibility

  3. Ran gazelle

    What did you expect to see?

    All the subpackages having the following visibility: package(default_visibility = ["//<path_to_project>:__subpackages__"])

What did you see instead?

In the subfolder instead of the above got the visibility attribute set to public

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

go_library(
    name = "go_default_library",
    srcs = ["<file_name>.go"],
    importpath = "",
    visibility = ["//visibility:public"],
    deps = ["//src/code.uber.internal/go/:go_default_library"],
)

Running gazelle a 2nd time adds package(default_visibility = ["//<path_to_project>:__subpackages__"]) However, the first run already set the visibility attribute and according to rules-target-visibility takes precedence over default_visibility. Hence, we default_visibility does not apply.

dnathe4th commented 1 year ago

Thanks for this report. Does the BUILD.bazel file in your subdirectory by any chance include a exports_files function? I have been trying to sort out an issue related to this function.