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.19k stars 378 forks source link

build_file_generation doesn't generate clean build files #1435

Open uhthomas opened 1 year ago

uhthomas 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?

6.0.0

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

Yeah.

What operating system and processor architecture are you using?

macOS aarch64

What did you do?

We're building https://github.com/sourcegraph/sourcegraph with Bazel via the Go toolchain. The dependency is loaded through the go_repository rule:

go_repository(
    name = "com_github_sourcegraph_sourcegraph",
    build_file_generation = "on",
    build_file_proto_mode = "disable",
    build_tags = ["dist"],  # keep
    importpath = "github.com/sourcegraph/sourcegraph",
    patch_args = ["-p1"],
    patches = ["//third_party:com_github_sourcegraph_sourcegraph-gazelle.patch"],
    sum = "h1:YhaCnQpy1SIo46dICVFk/gQEDZblMoegLfEtWlu8tbY=",
    version = "v0.0.0-20230201132943-764250ee35e4",
)

They are in the process of migrating to Bazel, and so now there are build files in the repo. The migration isn't complete, so we want to force Gazelle to generate new build files as if it were a "normal" Go module. This doesn't seem to work as expected.

What did you expect to see?

Gazelle should generate clean build files, as if it were a normal Go module.

What did you see instead?

❯ bazel build //...
(14:30:02) INFO: Current date is 2023-02-01
(14:30:02) ERROR: /private/var/tmp/_bazel_thomas/d95eb470615df18f91feffc144a9a015/external/com_github_sourcegraph_sourcegraph/internal/extsvc/BUILD.bazel:3:11: error loading package '@com_github_sourcegraph_sourcegraph//schema': Unable to find package for @aspect_rules_js//js:defs.bzl: The repository '@aspect_rules_js' could not be resolved: Repository '@aspect_rules_js' is not defined. and referenced by '@com_github_sourcegraph_sourcegraph//internal/extsvc:extsvc'
(14:30:02) ERROR: Analysis of target '//<redacted>' failed; build aborted: 
(14:30:02) INFO: Elapsed time: 0.913s
(14:30:02) INFO: 0 processes.
(14:30:02) FAILED: Build did NOT complete successfully (5 packages loaded, 0 targets configured)
    currently loading: @com_github_sourcegraph_sourcegraph//schema
    Fetching repository @io_opentelemetry_go_otel; starting
    Fetching repository @com_github_gofrs_uuid; starting
    Fetching repository @com_github_keegancsmith_sqlf; starting
    Fetching repository @com_github_go_stack_stack; starting
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@aspect_rules_js//js:defs.bzl", "js_library")

package(default_visibility = ["//client/shared:__pkg__"])

js_library(
    name = "json-schema-draft-07",
    srcs = [
        "json-schema-draft-07.schema.json",
    ],
)

js_library(
    name = "settings",
    srcs = [
        "settings.schema.json",
    ],
)

js_library(
    name = "site",
    srcs = [
        "site.schema.json",
    ],
)

js_library(
    name = "batch_spec",
    srcs = [
        "batch_spec.schema.json",
    ],
)

go_library(
    name = "schema",
    srcs = [
        "bitbucket_server_util.go",
        "bitbucketcloud_util.go",
        "extension_schema.go",
        "gen.go",
        "github_util.go",
        "gitlab_util.go",
        "schema.go",
        "stringdata.go",
    ],
    embedsrcs = [
        "aws_codecommit.schema.json",
        "batch_spec.schema.json",
        "bitbucket_cloud.schema.json",
        "bitbucket_server.schema.json",
        "changeset_spec.schema.json",
        "gerrit.schema.json",
        "github.schema.json",
        "gitlab.schema.json",
        "gitolite.schema.json",
        "go-modules.schema.json",
        "jvm-packages.schema.json",
        "npm-packages.schema.json",
        "other_external_service.schema.json",
        "pagure.schema.json",
        "perforce.schema.json",
        "phabricator.schema.json",
        "python-packages.schema.json",
        "ruby-packages.schema.json",
        "rust-packages.schema.json",
        "settings.schema.json",
        "site.schema.json",
    ],
    importpath = "github.com/sourcegraph/sourcegraph/schema",
    deps = ["@com_github_sourcegraph_go_jsonschema//jsonschema"],
)

alias(
    name = "go_default_library",
    actual = ":schema",
    visibility = ["//visibility:public"],
)

Almost https://github.com/sourcegraph/sourcegraph/blob/ef114ac72b847cd6bca67a9adb9b2f32dc2cbcad/schema/BUILD.bazel, but not quite.


I would have expected Gazelle to wipe out the original build file first, but this doesn't seem to happen.

uhthomas commented 1 year ago

This is even more confusing: build_file_generation with off or auto just has very few build files at all...

Screenshot 2023-02-01 at 14 43 53

I can understand off, but it seems that auto disables build file generation for the entire repository?