buildtools icon indicating copy to clipboard operation
buildtools copied to clipboard

cannot invoke buildifier via the Bazel rule

Open nrvnrvn opened this issue 5 years ago • 9 comments

$ bazel run //:buildifier
ERROR: /private/var/tmp/_bazel_lol/b8c9941566ac81876f0d17a46a5ee553/external/io_bazel_rules_go/proto/BUILD.bazel:13:1: every rule of type go_proto_compiler implicitly depends upon the target '@com_google_protobuf//:protoc', but this target could not be found because of: no such package '@com_google_protobuf//': The repository '@com_google_protobuf' could not be resolved
ERROR: Analysis of target '//:buildifier' failed; build aborted: no such package '@com_google_protobuf//': The repository '@com_google_protobuf' could not be resolved
INFO: Elapsed time: 1.728s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 2 targets configured)
FAILED: Build did NOT complete successfully (1 packages loaded, 2 targets configured)

WORKSPACE:

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

# fetch and load rules_go
http_archive(
    name = "io_bazel_rules_go",
    sha256 = "9fb16af4d4836c8222142e54c9efa0bb5fc562ffc893ce2abeac3e25daead144",
    urls = [
        "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/0.19.0/rules_go-0.19.0.tar.gz",
        "https://github.com/bazelbuild/rules_go/releases/download/0.19.0/rules_go-0.19.0.tar.gz",
    ],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains()

http_archive(
    name = "com_github_bazelbuild_buildtools",
    sha256 = "d8440da64ac15eca922ca0e8c6772bbb04eaaf3d2f4de387e5bfdb87cecbe9d2",
    strip_prefix = "buildtools-0.28.0",
    url = "https://github.com/bazelbuild/buildtools/archive/0.28.0.zip",
)

load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_dependencies")

buildifier_dependencies()

root BUILD.bazel:

load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")

buildifier(
    name = "buildifier",
)

nrvnrvn avatar Jul 19 '19 11:07 nrvnrvn

Please try following the instructions that have just been updated: https://github.com/bazelbuild/buildtools/tree/master/buildifier#setup-and-usage-via-bazel

Note the newly added lines about protobuf_deps.

vladmos avatar Jul 22 '19 10:07 vladmos

Thanks a lot, now it is working!

but this time it's taking a while to compile protocol buffers. I wonder if it would be possible to disable proto files generation and just use *gen.pb.go files instead, i.e. something like # gazelle:proto disable_global but for bazel dependencies.

I would be happy to use bazel //:buildifier just to keep bazel tools and dependencies under control of bazel itself but it seems more reasonable to fetch buildifier as a binary atm...

nrvnrvn avatar Jul 22 '19 13:07 nrvnrvn

Please try following the instructions that have just been updated: https://github.com/bazelbuild/buildtools/tree/master/buildifier#setup-and-usage-via-bazel

Note the newly added lines about protobuf_deps.

Using the instructions you provided is giving me:

ERROR: error loading package '': Unable to load file '@com_google_protobuf//:protobuf_deps.bzl': file doesn't exist

Any help is appreciated!

changusmc avatar Oct 31 '19 15:10 changusmc

Please try following the instructions that have just been updated: https://github.com/bazelbuild/buildtools/tree/master/buildifier#setup-and-usage-via-bazel Note the newly added lines about protobuf_deps.

Using the instructions you provided is giving me:

ERROR: error loading package '': Unable to load file '@com_google_protobuf//:protobuf_deps.bzl': file doesn't exist

Any help is appreciated!

Never mind, I was loading google_common_workspace_rules which already defined protobuf

changusmc avatar Oct 31 '19 16:10 changusmc

Yeah, I have to agree that it's pretty annoying to have to wait for all 220+ protoc targets to build whenever the "sandbox goes stale" (I don't have metrics on this, but it seems to be every 5-10 minutes); I've never seen this message except when running //:buildifier just to format my Starlark files.

I want to be idiomatic and use Bazel to build buildifier, but I have to agree with @nrvnrvn; it seems much more reasonable to grab a binary (via go get or http_file()) than to wait the couple minutes (yes, minutes) every time I want to format.

Consider this; where bazel run //:buildifier is set up to execute in a pre-commit hook if WORKSPACE, BUILD, or BUILD.bazel files are detected:

➜ git commit
Formatting Bazel files...
INFO: Analyzed target //:buildifier (27 packages loaded, 595 targets configured).
INFO: Found 1 target...
Target //:buildifier up-to-date:
  bazel-bin/buildifier.bash
INFO: Elapsed time: 76.502s, Critical Path: 21.27s
INFO: 195 processes: 195 linux-sandbox.
INFO: Build completed successfully, 200 total actions
INFO: Build completed successfully, 200 total actions
[master 78c4c39] initial commit
...

cc @vladmos @laurentlb @pmbethe09

sudoforge avatar Nov 20 '19 08:11 sudoforge

Seems like this issue is back, current example in the README results in

ERROR: /private/var/tmp/_bazel_simon.sawert/0bff2b8bcf2abcbc64e735aa9bd2155f/external/com_github_bazelbuild_buildtools/api_proto/BUILD.bazel:11:14: 
every rule of type proto_library implicitly depends upon the target '@com_google_protobuf//:protoc', 
but this target could not be found because of: 
error loading package '@com_google_protobuf//': 
Unable to load file '@bazel_skylib//rules:common_settings.bzl': file doesn't exist

bombsimon avatar Aug 28 '20 09:08 bombsimon

Seems like this issue is back, current example in the README results in

ERROR: /private/var/tmp/_bazel_simon.sawert/0bff2b8bcf2abcbc64e735aa9bd2155f/external/com_github_bazelbuild_buildtools/api_proto/BUILD.bazel:11:14: 
every rule of type proto_library implicitly depends upon the target '@com_google_protobuf//:protoc', 
but this target could not be found because of: 
error loading package '@com_google_protobuf//': 
Unable to load file '@bazel_skylib//rules:common_settings.bzl': file doesn't exist

That's a different issue. You need to update your @bazel_skylib.

sudoforge avatar Aug 28 '20 10:08 sudoforge

That's a different issue. You need to update your @bazel_skylib.

Thanks! My bad, I was under the impression that the example would work as is by copy pasting, I don't have anything else in my WORKSPACE or BUILD.bazel. Will look into how I update @bazel_skylib!

bombsimon avatar Aug 28 '20 10:08 bombsimon

Ah -- if you don't have bazel_skylib defined in your workspace then the version defined in the buildifier_deps macro is erroneous and a bug should be opened for this.

I'm in the Pacific time zone and finally going to bed, but will follow up with this in the morning

sudoforge avatar Aug 28 '20 11:08 sudoforge