Skip to content

Commit bc85124

Browse files
committed
Make bazel run //bazel/buildifier work on Windows
1 parent 2809b2f commit bc85124

File tree

6 files changed

+85
-7
lines changed

6 files changed

+85
-7
lines changed

.gitlab/bazel/build-deps.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,4 @@ bazel:build-deps:windows-amd64:
4747
ARCH: x64
4848
SCRIPT: |-
4949
echo "🟡 TODO(regis): compilation errors remain - limiting to a working subset for the time being"
50-
bazel build '@zlib//...' '@libffi'
51-
# bazel build @bzip2//...
50+
bazel run //bazel/buildifier

MODULE.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "ht
2323
http_archive(
2424
name = "com_github_bazelbuild_buildtools",
2525
patch_strip = 1,
26-
patches = ["//bazel/patches:buildifier.patch"],
26+
patches = [
27+
# pending https://github.com/bazelbuild/buildtools/pull/1398:
28+
"//bazel/patches:buildifier-build.patch",
29+
# pending https://github.com/bazelbuild/buildtools/pull/1399 & https://github.com/bazelbuild/buildtools/pull/1400:
30+
"//bazel/patches:buildifier-internal-factory.patch",
31+
"//bazel/patches:buildifier-runner-bat-template.patch",
32+
],
2733
sha256 = "53119397bbce1cd7e4c590e117dcda343c2086199de62932106c80733526c261",
2834
strip_prefix = "buildtools-8.2.1",
2935
urls = ["https://github.com/bazelbuild/buildtools/archive/refs/tags/v8.2.1.tar.gz"],

bazel/buildifier/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier", "buildifier_test")
22

33
exclude_patterns = [
4-
"./.bazelbsp/**",
5-
"./.cache/**",
4+
"./.*", # all "hidden" directories at the root of the workspace: ./.bazelbsp, ./.cache, ./.git, etc.
65
]
76

87
buildifier(

bazel/patches/buildifier.patch renamed to bazel/patches/buildifier-build.patch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ index 1f1cd67..2f2a5b1 100644
66
+INHIBITED_IN_FAVOR_OF_PREBUILT = """
77
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
88
load("@rules_shell//shell:sh_test.bzl", "sh_test")
9-
9+
1010
@@ -108,6 +109,7 @@ go_library(
1111
"//wspace",
1212
],
1313
)
1414
+"""
15-
15+
1616
exports_files(
1717
[
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
diff --git a/buildifier/internal/factory.bzl b/buildifier/internal/factory.bzl
2+
index 0ad56ff..c33f0b6 100644
3+
--- a/buildifier/internal/factory.bzl
4+
+++ b/buildifier/internal/factory.bzl
5+
@@ -159,8 +159,12 @@ def buildifier_impl_factory(ctx, test_rule = False):
6+
if ctx.attr.exclude_patterns:
7+
if test_rule and not ctx.attr.no_sandbox:
8+
fail("Cannot use 'exclude_patterns' in a test rule without 'no_sandbox'")
9+
- exclude_patterns = ["\\! -path %s" % shell.quote(pattern) for pattern in ctx.attr.exclude_patterns]
10+
- exclude_patterns_str = " ".join(exclude_patterns)
11+
+ if ctx.executable.buildifier.extension.lower() == "exe":
12+
+ exclude_patterns = [shell.quote(pattern.replace("/", "\\")) for pattern in ctx.attr.exclude_patterns]
13+
+ exclude_patterns_str = ", ".join(exclude_patterns)
14+
+ else:
15+
+ exclude_patterns = ["\\! -path %s" % shell.quote(pattern) for pattern in ctx.attr.exclude_patterns]
16+
+ exclude_patterns_str = " ".join(exclude_patterns)
17+
18+
workspace = ""
19+
if test_rule and ctx.attr.no_sandbox:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
diff --git a/buildifier/runner.bat.template b/buildifier/runner.bat.template
2+
index 13ca3fa..480b2bd 100644
3+
--- a/buildifier/runner.bat.template
4+
+++ b/buildifier/runner.bat.template
5+
@@ -9,14 +9,38 @@ rem Unquote the arguments
6+
set stripped_args=%stripped_args:'=%
7+
8+
rem Get the absolute path to the buildifier executable
9+
-for /f "tokens=2" %%i in ('findstr /r "\<buildifier\.exe\>" MANIFEST') do (set buildifier_abs_path=%%i)
10+
+if exist MANIFEST (
11+
+ set manifest_file=MANIFEST
12+
+) else if exist "%RUNFILES_MANIFEST_FILE%" (
13+
+ set "manifest_file=%RUNFILES_MANIFEST_FILE:/=\%"
14+
+) else (
15+
+ >&2 echo Error: manifest file not found
16+
+ exit /b 1
17+
+)
18+
+for /f "tokens=2" %%i in ('findstr /r "\<buildifier\.exe\>" "%manifest_file%"') do (set buildifier_abs_path=%%i)
19+
20+
powershell ^
21+
+function Should-Exclude($Path)^
22+
+{^
23+
+ $relPath = '.' + (Resolve-Path $Path).Path.Substring('%BUILD_WORKSPACE_DIRECTORY%'.Length);^
24+
+ foreach ($pattern in @(@@EXCLUDE_PATTERNS@@))^
25+
+ {^
26+
+ if ($relPath -clike $pattern)^
27+
+ {^
28+
+ return $true;^
29+
+ };^
30+
+ };^
31+
+ return $false;^
32+
+};^
33+
function Buildify($Root)^
34+
{^
35+
+ if (Should-Exclude $Root)^
36+
+ {^
37+
+ return;^
38+
+ };^
39+
$Folder = (New-Object -Com Scripting.FileSystemObject).GetFolder($Root);^
40+
$Files = $Folder.Files ^| Where-Object {^
41+
- $_.Name -eq 'BUILD.bazel' `^
42+
+ ($_.Name -eq 'BUILD.bazel' `^
43+
-or $_.Name -eq 'BUILD' `^
44+
-or $_.Name -eq 'WORKSPACE' `^
45+
-or $_.Name -eq 'WORKSPACE.bazel' `^
46+
@@ -27,7 +51,8 @@ function Buildify($Root)^
47+
-or $_.Name -clike 'BUILD.*.bazel' `^
48+
-or $_.Name -clike 'BUILD.*.oss' `^
49+
-or $_.Name -clike 'WORKSPACE.*.bazel' `^
50+
- -or $_.Name -clike 'WORKSPACE.*.oss'^
51+
+ -or $_.Name -clike 'WORKSPACE.*.oss') `^
52+
+ -and -not (Should-Exclude $_.Path)^
53+
};^
54+
foreach ($File in $Files)^
55+
{^

0 commit comments

Comments
 (0)