buildtools icon indicating copy to clipboard operation
buildtools copied to clipboard

Buildifier usage via Bazel does not work on Windows

Open filipesilva opened this issue 7 years ago • 9 comments

When following https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md#setup-and-usage-via-bazel on windows, the following output is observed:

$ bazel run //:buildifier
INFO: Analysed target //:buildifier (1 packages loaded).
INFO: Found 1 target...
Target //:buildifier up-to-date:
  C:/users/kamik/_bazel_kamik/gq7cl633/execroot/angular_devkit/bazel-out/x64_windows-fastbuild/bin/buildifier.bash
INFO: Elapsed time: 2.531s, Critical Path: 0.01s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
C:/users/kamik/_bazel_kamik/gq7cl633/execroot/angular_devkit/bazel-out/x64_windows-fastbuild/bin/buildifier.bash: line 8: : command not found

(Note: --direct_run is not used because it shows a WARNING: Option 'direct_run' is deprecated message).

The buildifier.bash file is such:

#!/usr/bin/env bash

BUILDIFIER_SHORT_PATH='../com_github_bazelbuild_buildtools/buildifier/windows_amd64_stripped/buildifier.exe'
ARGS=('-mode=fix' '-v=false' '-showlog=false')

buildifier_short_path=$(readlink "$BUILDIFIER_SHORT_PATH")
cd "$BUILD_WORKSPACE_DIRECTORY"
"$buildifier_short_path" "${ARGS[@]}" $(find . -type f  \( -name '*.bzl' -or -name 'BUILD.bazel' -or -name 'BUILD' -or -name 'WORKSPACE' \))

Debugging this generated file, I find two things:

  • $BUILDIFIER_SHORT_PATH evaluates to ../com_github_bazelbuild_buildtools/buildifier/windows_amd64_stripped/buildifier.exe
  • $buildifier_short_path evaluates to an empty string.

Indeed the path listed by $BUILDIFIER_SHORT_PATH does not actually exist. That executable can be found at ../external/com_github_bazelbuild_buildtools/buildifier/windows_amd64_stripped/buildifier.exe instead though. This mapping in windows is expressed in the buildifier.bash.runfiles_manifest file.

Another problem is that the path listed is not a symlink on windows, as using the --verbose flag on readlink highlights:

readlink "./external/com_github_bazelbuild_buildtools/buildifier/windows_amd64_stripped/buildifier.exe" --verbose
readlink: ./external/com_github_bazelbuild_buildtools/buildifier/windows_amd64_stripped/buildifier.exe: Invalid argument

Using realpath instead of readlink helps here, but I am unsure of the effect on non-windows platforms.

I hacked a bit on a local copy by hardcoding the right path and using realpath and buildifier seemed to work on Windows then.

Related to https://github.com/bazelbuild/buildtools/issues/170

/cc @alexeagle @gregmagolan

filipesilva avatar Aug 02 '18 10:08 filipesilva

I thought maybe the official runfiles lookup library for bazel binaries could be used, but buildifier is actually a Go binary instead. It doesn't look like Go has such helpers.

filipesilva avatar Aug 20 '18 16:08 filipesilva

FWIW there is no realpath command on macOS (with command line tools installed). Getting command not found: realpath. But there is readlink.

ash2k avatar Sep 10 '18 14:09 ash2k

I can confirm this issue exists on macOS 10.14.1 as well. This is what I tried today:

My WORKSPACE:

workspace(name = "my_workspace")

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

# abseil
http_archive(
    name = "absl",
    strip_prefix = "abseil-cpp-master",
    urls = ["https://github.com/abseil/abseil-cpp/archive/master.zip"],
)

# Google Test
http_archive(
    name = "gtest",
    strip_prefix = "googletest-master",
    urls = ["https://github.com/google/googletest/archive/master.zip"],
)

# Go support (e.g. buildifier)
http_archive(
    name = "io_bazel_rules_go",
    sha256 = "7be7dc01f1e0afdba6c8eb2b43d2fa01c743be1b9273ab1eaf6c233df078d705",
    urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.16.5/rules_go-0.16.5.tar.gz"],
)

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

http_archive(
    name = "bazel_gazelle",
    sha256 = "7949fc6cc17b5b191103e97481cf8889217263acf52e00b560683413af204fcb",
    urls = ["https://github.com/bazelbuild/bazel-gazelle/releases/download/0.16.0/bazel-gazelle-0.16.0.tar.gz"],
)

load("@io_bazel_rules_go//go:def.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_dependencies")
load("@io_bazel_rules_go//go:def.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains()

buildifier_dependencies()

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

gazelle_dependencies()

$ bazel run //:buildifier ...snip no failures...

$ bash -x bazel-bin/buildifier.bash
+ BUILDIFIER_SHORT_PATH=../com_github_bazelbuild_buildtools/buildifier/darwin_amd64_stripped/buildifier
+ ARGS=('-mode=fix' '-v=false' '-showlog=false')
++ readlink ../com_github_bazelbuild_buildtools/buildifier/darwin_amd64_stripped/buildifier
+ buildifier_short_path=
+ cd ''
++ find . -type f '(' -name '*.bzl' -or -name BUILD.bazel -or -name BUILD -or -name '*.BUILD' -or -name WORKSPACE ')'
+ '' -mode=fix -v=false -showlog=false ./WORKSPACE ./BUILD.bazel
bazel-bin/buildifier.bash: line 8: : command not found

BooneJS avatar Jan 06 '19 03:01 BooneJS

What is the status of this issue? It has been open for 2 years and I just tried with the latest master and the issue seems to persist. Does it mean that the integration of buildifier as stated in the documentation is only supported in linux? Is there any plans to support it in Windows?

limdor avatar Aug 09 '20 09:08 limdor

Regarding the status of Windows: The development is done on Linux and most of our users are on Linux. We run tests on Windows as part of our CI, so we make sure that we don't break the Windows integration.

Contributions are welcome.

laurentlb avatar Aug 09 '20 15:08 laurentlb

@laurentlb do you mean that there is a test that checks the integration of buildifier on windows on the CI? I am just trying to understand if the issue that I am facing when integrating buildifier is because of the setup on the windows machine or because it is a known issue. Thanks

limdor avatar Aug 18 '20 15:08 limdor

The CI runs bazel test //:tests on Windows: https://buildkite.com/bazel/buildtools/builds/1963#93f704b8-6a3c-43a0-a440-981034418abb

laurentlb avatar Aug 18 '20 15:08 laurentlb

Great thanks, I can try to run it on the appvoyer setup that I have and see if these tests are working there. I am also planning to setup a CI in buildkite and see if the issue is reproducible.

limdor avatar Aug 18 '20 15:08 limdor