From 14c4aeb15dc2b11406da67755f45317a830b10de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Desgroppes?= <7901596+rdesgroppes@users.noreply.github.com> Date: Tue, 7 Oct 2025 16:55:04 +0200 Subject: [PATCH] buildifier: honor `exclude_patterns` on Windows too The `exclude_patterns` parameter passed to `buildifier` macros happens to be ignored by the Windows runner script template, which the present change proposes to address. The change tries to adhere to existing choices, by limiting the impact on `factory.bzl` where the `find`-suitable pattern can't be used by the embedded powershell script, and also by respecting the script's coding style. --- buildifier/internal/factory.bzl | 8 ++++++-- buildifier/runner.bat.template | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/buildifier/internal/factory.bzl b/buildifier/internal/factory.bzl index 0ad56ff9f..c33f0b646 100644 --- a/buildifier/internal/factory.bzl +++ b/buildifier/internal/factory.bzl @@ -159,8 +159,12 @@ def buildifier_impl_factory(ctx, test_rule = False): if ctx.attr.exclude_patterns: if test_rule and not ctx.attr.no_sandbox: 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) workspace = "" if test_rule and ctx.attr.no_sandbox: diff --git a/buildifier/runner.bat.template b/buildifier/runner.bat.template index 1d359cdc0..d7d0db8c2 100644 --- a/buildifier/runner.bat.template +++ b/buildifier/runner.bat.template @@ -12,8 +12,21 @@ rem Get the absolute path to the buildifier executable for /f "tokens=2" %%i in ('findstr /r "\" MANIFEST') 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;^ +};^ $Files = Get-ChildItem -LiteralPath '%BUILD_WORKSPACE_DIRECTORY:/=\%' -Recurse -File -ErrorAction SilentlyContinue ^|^ Where-Object {^ + (^ $_.Name -eq 'BUILD.bazel' `^ -or $_.Name -eq 'BUILD' `^ -or $_.Name -eq 'WORKSPACE' `^ @@ -26,6 +39,7 @@ $Files = Get-ChildItem -LiteralPath '%BUILD_WORKSPACE_DIRECTORY:/=\%' -Recurse - -or $_.Name -clike 'BUILD.*.oss' `^ -or $_.Name -clike 'WORKSPACE.*.bazel' `^ -or $_.Name -clike 'WORKSPACE.*.oss'^ + ) -and -not (Should-Exclude $_.Path)^ };^ ^<# Process files in batches of 100- to avoid exceeding CreateProcess' maximum length of 32,767 characters #^> ^ $i = 0;^