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

go workspace seems not work on Golang official tutorial #1857

Open xuzhenglun opened 2 months ago

xuzhenglun commented 2 months ago

What version of gazelle are you using?

0.38.0

What version of rules_go are you using?

0.49.0

What version of Bazel are you using?

7.2.1

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

yes

What operating system and processor architecture are you using?

Darwin ReficuldeMBP.reficul.lab 23.0.0 Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:34 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8103 arm64

What did you do?

I was following the official tutorial of go workspace with bzlmod. I created a demo project looks like below:

.
├── BUILD.bazel
├── MODULE.bazel
├── MODULE.bazel.lock
├── WORKSPACE
├── example
│   └── hello
│       ├── BUILD.bazel
│       ├── go.mod
│       └── reverse
│           ├── BUILD.bazel
│           ├── example_test.go
│           ├── int.go
│           ├── reverse.go
│           └── reverse_test.go
├── go.mod
├── go.sum
├── go.work
└── main.go

go.work looks like:

go 1.22.5

use (
        .
        ./example/hello
)

MODULE.bazel looks like:

module(
    name = "demo",
    version = "0.1.0",
)

bazel_dep(name = "rules_go", version = "0.49.0")
bazel_dep(name = "gazelle", version = "0.38.0")

go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")

go_sdk.download(version = "1.22.3")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")

go_deps.from_file(
    go_work = "//:go.work",
)

use_repo(
    go_deps,
    "org_golang_x_example_hello",
)

In this demo, main.go imports example/hello/reverse by using go.work, and a new added function Int is called. This Int method is only existed in this local package. so if go.work works, building should success. if go.work doest work, then building should fail.

This demo works great by using go build, but fails by using bazel build //:demo. did anything I do wrong?

The demo project could be found in attachment: go-work.tar.gz

What did you expect to see?

building without any error.

What did you see instead?

ERROR: /Users/reficul/projects/go-work/BUILD.bazel:14:10: GoCompilePkg demo.a failed: (Exit 1): builder failed: error executing GoCompilePkg command (from target //:demo) bazel-out/darwin_arm64-opt-exec-ST-d57f47055a04/bin/external/rules_go~~go_sdk~demo__download_0/builder_reset/builder compilepkg -sdk external/rules_go~~go_sdk~demo__download_0 -goroot ... (remaining 29 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
main.go:10:47: undefined: reverse.Int
compilepkg: error running subcommand external/rules_go~~go_sdk~demo__download_0/pkg/tool/darwin_arm64/compile: exit status 2
Target //:demo failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.488s, Critical Path: 0.11s
INFO: 2 processes: 2 internal.
ERROR: Build did NOT complete successfully
xuzhenglun commented 2 months ago

friendly ping @stefanpenner

stefanpenner commented 1 month ago

I haven't had much time for bazel stuff recently, but that might be changing. I'll attempt to carv out some time to investigate.

FWIW the go work demo was how I originally made this work, and I believe it inspired the tests within gazelle itself, so it is surprising to see a failure here.