buildtools icon indicating copy to clipboard operation
buildtools copied to clipboard

bazel-bin/buildifier.bash commands

Open anandwana001 opened this issue 4 years ago • 3 comments

I am currently on macOS, and not able to run the Buildifier.bash file.

WORKSPACE

# buildifier is written in Go and hence needs rules_go to be built.
# See https://github.com/bazelbuild/rules_go for the up to date setup instructions.
http_archive(
    name = "io_bazel_rules_go",
    sha256 = "d1ffd055969c8f8d431e2d439813e42326961d0942bdf734d2c95dc30c369566",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.24.5/rules_go-v0.24.5.tar.gz",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.24.5/rules_go-v0.24.5.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 = "bazel_gazelle",
    sha256 = "b85f48fa105c4403326e9525ad2b2cc437babaa6e15a3fc0b1dbab0ab064bc7c",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.2/bazel-gazelle-v0.22.2.tar.gz",
        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.2/bazel-gazelle-v0.22.2.tar.gz",
    ],
)

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies()

Root level Build.bazel

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

buildifier(
    name = "buildifier",
)

Target Run successfully

bazel run //:buildifier
Screenshot 2021-02-17 at 12 08 09
bash bazel-bin/buildifier.bash -version  -> xargs: : No such file or directory
bash bazel-bin/buildifier.bash --version  -> xargs: : No such file or directory
sh bazel-bin/buildifier.bash -version  -> xargs: : No such file or directory
sh bazel-bin/buildifier.bash --version  -> xargs: : No such file or directory
bash bazel-bin/buildifier.bash -version  -> xargs: : No such file or directory
bazel-bin/buildifier.bash  -> xargs: : No such file or directory
  1. Am I using commands incorrectly?

Without rules, I tried brew install buildifier and it works smoothly.

buildifier --lint=warn --mode=check --warnings=-native-android,+out-of-order-load,+unsorted-dict-items -r app data 
domain model testing utility third_party tools BUILD.bazel WORKSPACE oppia_android_test.bzl

Tried 1 more option:

  1. Downloaded the binary file from here - https://github.com/bazelbuild/buildtools/releases/download/$BUILDIFIER/buildifier where BUILDIFIER="3.4.0"
  2. Make it executable chmod a+x buildifier
  3. Error - cannot execute binary file

anandwana001 avatar Feb 17 '21 06:02 anandwana001

If you want to build a binary and then run it independently, use bazel build instead of bazel run, the latter creates a special bash script that's meant to be used by bazel run.

In your case, if you want to just build buildifier, you don't need to create a new repository, just check out this one, run bazel build //buildifier there and use the generated binary (you'll see its path in the console). It's a self-contained binary, you can copy it anywhere.

Alternatively, just download it from GitHub as you've described above, but you need to fetch a binary for MacOS, before 4.0.0 it's called buildifier.mac, starting with 4.0.0 on it's called buildifier-darwin-amd64. buildifier is just a copy of buildifier-linux-amd64, it's being uploaded there for compatibility reasons and will not be included in the future versions.

vladmos avatar Feb 17 '21 11:02 vladmos

I'm having good luck with the following in my root BUILD.bazel:

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

buildifier(
    name = "buildifier-fix",
    lint_mode = "fix",
    mode = "fix",
)

Then ran via:

$ bazelisk run //:buildifier-fix

This properly updates the script (bazel-bin/buildifier-fix.bash) that Vladimir mentions to have the arguments passed from the build file:

$ cat bazel-bin/buildifier-fix.bash
#! /usr/bin/env bash

BUILDIFIER_SHORT_PATH='../com_github_bazelbuild_buildtools/buildifier/buildifier_/buildifier'
ARGS=('-mode=fix' '-v=false' '-lint=fix')

...

mattrobmattrob avatar Sep 28 '21 03:09 mattrobmattrob

I have the same issue as Akshay. I ran bazel build //:buildifier and bazel run //:buildifier. The same problem. Can someone help?

Edit: I gave up on building it myself. It seems the executable already available, buildifier-linux-amd64 works.

AStefan93 avatar Feb 02 '22 22:02 AStefan93