bazelbuild / 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.18k stars 374 forks source link

Gazelle Generates The Wrong Bazel Target #1272

Open lbhdc opened 2 years ago

lbhdc commented 2 years ago

What version of gazelle are you using?

0.25.0

What version of rules_go are you using?

0.33.0

What version of Bazel are you using?

5.1.1

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

Yes

What operating system and processor architecture are you using?

Linux x86_64

What did you do?

I am trying to incorporate wasmtime into my project. However, it generates the incorrect bazel path when generating build files.

Simple example main.go

package main

import _ "github.com/bytecodealliance/wasmtime-go"

This is how I am configuring my gazelle commands

gazelle(name = "gazelle")

gazelle(
    name = "gazelle-update-repos",
    args = [
        "-from_file=go.mod",
        "-to_macro=deps.bzl%go_dependencies",
        "-prune",
    ],
    command = "update-repos",
)

Then update dependencies, and generate build files

go mod tidy
bazel run //:gazelle-update-repos
bazel run //:gazelle

The wasmtime library calls out a go_repository rule in their docs. gazelle generates the identical rule.

go_repository(
    name = "com_github_bytecodealliance_wasmtime_go",
    importpath = "github.com/bytecodealliance/wasmtime-go",
    sum = "h1:B6thr7RMM9xQmouBtUqm1RpkJjuLS37m6nxX+iwsQSc=",
    version = "v0.36.0",
)

What did you expect to see?

This isn't what I expected, but this is correct.

go_library(
    # truncated fields
    srcs = ["main.go"],
    deps = [
        "@com_github_bytecodealliance_wasmtime_go//:go_default_library",
    ],
)

What did you see instead?

This is the path that gazelle generates.

"@com_github_bytecodealliance_wasmtime_go//:wasmtime-go"

This fails to build, bazel complains it can't find that path, and exploring what targets are available confirm that :wasmtime-go doesn't exist.

I wonder if it has something to do with the cc target?

> bazel query 'kind(rule, @com_github_bytecodealliance_wasmtime_go//...)' --output label_kind
go_library rule @com_github_bytecodealliance_wasmtime_go//:go_default_library
go_test rule @com_github_bytecodealliance_wasmtime_go//:go_default_test
cc_library rule @com_github_bytecodealliance_wasmtime_go//:wasmtime

Is there a way to prevent this?

tuananhlai commented 2 years ago

I'm having the same problem. Have you managed to find a solution?

lbhdc commented 2 years ago

@tuananhlai I have a workaround, but not a great one.

I have a wrapper around gazelle that runs go mod tidy, updates repos, then runs gazelle. I added a call to sed to do a find and replace for on the specific build file that I am having this problem in.

Here is what it looks like

#! /usr/bin/env bash

go mod tidy
bazel run //:gazelle-update-repos
bazel run //:gazelle
sed -i 's,@com_github_bytecodealliance_wasmtime_go//:wasmtime-go,@com_github_bytecodealliance_wasmtime_go//:go_default_library,g' path/to/BUILD

This is less than ideal though. If the wrapper isn't used then you run right back into this edge case.

achew22 commented 2 years ago

Sounds to me like there is something wrong with the way your gazelle:go_naming_convention is getting inferred. The value you see (:wasmtime-go) is what I'd expect from the import mode Do you have anything that is configuring that setting?

lbhdc commented 2 years ago

@achew22 that is what I would expect too based on the import.

In my root BUILD i have a few magic comment directives, but nothing (that I am aware of) that would change the naming convention.

# gazelle:prefix my-project
# gazelle:build_file_name BUILD
# gazelle:exclude bazel-*
# gazelle:exclude cmake-*
# gazelle:exclude third_party
# gazelle:exclude tools
gazelle(name = "gazelle")

Perhaps one complication is that this libraries package name is different from its github path? For example it uses package wasmtime instead of package wasmtime-go. This package name is the same as the cc_library target in that package. I wonder if that is causing part of the problem?