From 9d45db3fe2240901036be95f15e2a6a6f5f1a5d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Desgroppes?= Date: Wed, 24 Sep 2025 11:25:11 +0200 Subject: [PATCH] Make `bazel run //bazel/buildifier` work on Windows --- MODULE.bazel | 6 ++- bazel/buildifier/BUILD.bazel | 3 +- ...uildifier.patch => buildifier-build.patch} | 0 .../patches/buildifier-internal-factory.patch | 15 ++++++ .../buildifier-runner-bat-template.patch | 48 +++++++++++++++++++ 5 files changed, 69 insertions(+), 3 deletions(-) rename bazel/patches/{buildifier.patch => buildifier-build.patch} (100%) create mode 100644 bazel/patches/buildifier-internal-factory.patch create mode 100644 bazel/patches/buildifier-runner-bat-template.patch diff --git a/MODULE.bazel b/MODULE.bazel index cb63d7f4a40653..a1e0b21b455227 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -23,7 +23,11 @@ http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "ht http_archive( name = "com_github_bazelbuild_buildtools", patch_strip = 1, - patches = ["//bazel/patches:buildifier.patch"], + patches = [ + "//bazel/patches:buildifier-build.patch", # https://github.com/bazelbuild/buildtools/pull/1398 + "//bazel/patches:buildifier-internal-factory.patch", # https://github.com/bazelbuild/buildtools/pull/1399 + "//bazel/patches:buildifier-runner-bat-template.patch", # ^ https://github.com/bazelbuild/buildtools/pull/1400 + ], sha256 = "53119397bbce1cd7e4c590e117dcda343c2086199de62932106c80733526c261", strip_prefix = "buildtools-8.2.1", urls = ["https://github.com/bazelbuild/buildtools/archive/refs/tags/v8.2.1.tar.gz"], diff --git a/bazel/buildifier/BUILD.bazel b/bazel/buildifier/BUILD.bazel index 63f266f0ee4e41..4b6c4df06d8a61 100644 --- a/bazel/buildifier/BUILD.bazel +++ b/bazel/buildifier/BUILD.bazel @@ -1,8 +1,7 @@ load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier", "buildifier_test") exclude_patterns = [ - "./.bazelbsp/**", - "./.cache/**", + "./.*", # all "hidden" directories at the root of the workspace: ./.bazelbsp, ./.cache, ./.git, etc. ] buildifier( diff --git a/bazel/patches/buildifier.patch b/bazel/patches/buildifier-build.patch similarity index 100% rename from bazel/patches/buildifier.patch rename to bazel/patches/buildifier-build.patch diff --git a/bazel/patches/buildifier-internal-factory.patch b/bazel/patches/buildifier-internal-factory.patch new file mode 100644 index 00000000000000..834f66c35f97fa --- /dev/null +++ b/bazel/patches/buildifier-internal-factory.patch @@ -0,0 +1,15 @@ +diff --git a/buildifier/internal/factory.bzl b/buildifier/internal/factory.bzl +index 0ad56ff..c33f0b6 100644 +--- a/buildifier/internal/factory.bzl ++++ b/buildifier/internal/factory.bzl +@@ -161,4 +161,8 @@ def buildifier_impl_factory(ctx, test_rule = False): + fail("Cannot use 'exclude_patterns' in a test rule without 'no_sandbox'") +- exclude_patterns = ["\\! -path %s" % shell.quote(pattern) for pattern in ctx.attr.exclude_patterns] +- exclude_patterns_str = " ".join(exclude_patterns) ++ if ctx.executable.buildifier.extension.lower() == "exe": ++ exclude_patterns = [shell.quote(pattern.replace("/", "\\")) for pattern in ctx.attr.exclude_patterns] ++ exclude_patterns_str = ", ".join(exclude_patterns) ++ else: ++ exclude_patterns = ["\\! -path %s" % shell.quote(pattern) for pattern in ctx.attr.exclude_patterns] ++ exclude_patterns_str = " ".join(exclude_patterns) + diff --git a/bazel/patches/buildifier-runner-bat-template.patch b/bazel/patches/buildifier-runner-bat-template.patch new file mode 100644 index 00000000000000..555441199d54d7 --- /dev/null +++ b/bazel/patches/buildifier-runner-bat-template.patch @@ -0,0 +1,48 @@ +diff --git a/buildifier/runner.bat.template b/buildifier/runner.bat.template +index 13ca3fa..d8ccac9 100644 +--- a/buildifier/runner.bat.template ++++ b/buildifier/runner.bat.template +@@ -11,9 +11,34 @@ set stripped_args=%stripped_args:'=% + rem Get the absolute path to the buildifier executable +-for /f "tokens=2" %%i in ('findstr /r "\" MANIFEST') do (set buildifier_abs_path=%%i) ++if exist MANIFEST ( ++ set manifest_file=MANIFEST ++) else if exist "%RUNFILES_MANIFEST_FILE%" ( ++ set "manifest_file=%RUNFILES_MANIFEST_FILE:/=\%" ++) else ( ++ >&2 echo Error: manifest file not found ++ exit /b 1 ++) ++for /f "tokens=2" %%i in ('findstr /r "\" "%manifest_file%"') do (set buildifier_abs_path=%%i) + + powershell ^ ++function Should-Exclude($Path)^ ++{^ ++ $relPath = '.' + $Path.Substring('%BUILD_WORKSPACE_DIRECTORY%'.Length);^ ++ foreach ($pattern in @(@@EXCLUDE_PATTERNS@@))^ ++ {^ ++ if ($relPath -clike $pattern)^ ++ {^ ++ return $true;^ ++ };^ ++ };^ ++ return $false;^ ++};^ + function Buildify($Root)^ + {^ ++ if (Should-Exclude $Root)^ ++ {^ ++ return;^ ++ };^ + $Folder = (New-Object -Com Scripting.FileSystemObject).GetFolder($Root);^ + $Files = $Folder.Files ^| Where-Object {^ ++ (^ + $_.Name -eq 'BUILD.bazel' `^ +@@ -30,2 +55,3 @@ function Buildify($Root)^ + -or $_.Name -clike 'WORKSPACE.*.oss'^ ++ ) -and -not (Should-Exclude $_.Path)^ + };^ +@@ -44,2 +70,2 @@ function Buildify($Root)^ + };^ +-Buildify('%BUILD_WORKSPACE_DIRECTORY%'); ++Buildify('%BUILD_WORKSPACE_DIRECTORY:/=\%');