|
| 1 | +commit 48d799c583061928695056dd2d17a6501bc06002 |
| 2 | +Author: Régis Desgroppes < [email protected]> |
| 3 | +Date: Fri Sep 26 13:08:27 2025 +0200 |
| 4 | + |
| 5 | + Anticipate Windows fixes for buildifier |
| 6 | + |
| 7 | + This applies Windows fixes proposed by: |
| 8 | + https://github.com/keith/buildifier-prebuilt/pull/89 |
| 9 | + |
| 10 | +diff --git a/buildifier/factory.bzl b/buildifier/factory.bzl |
| 11 | +index 6a8098c..f708ea3 100644 |
| 12 | +--- a/buildifier/factory.bzl |
| 13 | ++++ b/buildifier/factory.bzl |
| 14 | +@@ -151,7 +151,7 @@ def buildifier_impl_factory(ctx, *, test_rule): |
| 15 | + |
| 16 | + substitutions = { |
| 17 | + "@@ARGS@@": shell.array_literal(args) if out_ext == ".bash" else shell.array_literal(args)[1:][:-1].replace("'", ""), |
| 18 | +- "@@BUILDIFIER_SHORT_PATH@@": shell.quote(buildifier.short_path) if out_ext == ".bash" else buildifier.path, |
| 19 | ++ "@@BUILDIFIER_SHORT_PATH@@": shell.quote(buildifier.short_path) if out_ext == ".bash" else buildifier.short_path, |
| 20 | + "@@EXCLUDE_PATTERNS@@": exclude_patterns_str, |
| 21 | + "@@WORKSPACE@@": workspace, |
| 22 | + } |
| 23 | +diff --git a/runner.bat.template b/runner.bat.template |
| 24 | +index c27e34d..fc2b873 100644 |
| 25 | +--- a/runner.bat.template |
| 26 | ++++ b/runner.bat.template |
| 27 | +@@ -6,52 +6,80 @@ set EXTRA_ARGS=@@ARGS@@ |
| 28 | + set WORKSPACE=@@WORKSPACE@@ |
| 29 | + |
| 30 | + REM When --enable_runfiles, we should be able to locate buildifier directly through symlink. |
| 31 | +-for %%I in (%BUILDIFIER_SHORT_PATH%) do set "buildifier_short_path=%%~fI" |
| 32 | ++set buildifier_path=%BUILDIFIER_SHORT_PATH:/=\% |
| 33 | ++for %%I in (%buildifier_path%) do ( |
| 34 | ++ set "buildifier_path=%%~fI" |
| 35 | ++) |
| 36 | + |
| 37 | +-REM If we can't find it, then parse the MANIFEST file to find buildifier. |
| 38 | +-if not exist !buildifier_short_path! ( |
| 39 | +- if not exist MANIFEST ( |
| 40 | +- echo Runfiles are not enabled, yet a MANIFEST file cannot be found |
| 41 | +- exit /b 1 |
| 42 | +- ) |
| 43 | +- type MANIFEST |
| 44 | +- for /F "tokens=1" %%F IN ('findstr /L /C:buildifier.exe MANIFEST') DO ( |
| 45 | +- set "buildifier_short_path=%%~fF" |
| 46 | ++set manifest_path=MANIFEST |
| 47 | ++if not exist %manifest_path% ( |
| 48 | ++ set manifest_path=..\MANIFEST |
| 49 | ++) |
| 50 | ++if not exist %manifest_path% ( |
| 51 | ++ echo a MANIFEST file cannot be found |
| 52 | ++ exit /b 1 |
| 53 | ++) |
| 54 | ++ |
| 55 | ++REM find the workspace directory in manifest. |
| 56 | ++REM No good way in bat to read path of a symlink's target |
| 57 | ++set workspace_dir= |
| 58 | ++for /F "tokens=2 delims= " %%F IN ('findstr /l /c:"WORKSPACE" %manifest_path%') DO ( |
| 59 | ++ set "workspace_dir=%%~dpF" |
| 60 | ++) |
| 61 | ++ |
| 62 | ++REM If the buildifier isn't immediately available via short_path from |
| 63 | ++REM current directory, then parse the MANIFEST file to find buildifier. |
| 64 | ++if not exist !buildifier_path! ( |
| 65 | ++ for /F "tokens=2 delims= " %%F IN ('findstr /l /c:"buildifier.exe" %manifest_path%') DO ( |
| 66 | ++ set "buildifier_path=%%F" |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | +-if not exist !buildifier_short_path! ( |
| 71 | +- echo Failed to find buildifier at !buildifier_short_path! |
| 72 | ++if not exist !buildifier_path! ( |
| 73 | ++ echo Failed to find buildifier at !buildifier_path! |
| 74 | + exit /b 1 |
| 75 | + ) |
| 76 | + |
| 77 | +-if defined TEST_WORKSPACE ( |
| 78 | +- if not defined BUILD_WORKSPACE_DIRECTORY ( |
| 79 | +- if defined WORKSPACE ( |
| 80 | +- cd "%WORKSPACE%" |
| 81 | +- ) else ( |
| 82 | +- set follow_links=1 |
| 83 | +- ) |
| 84 | ++if defined BUILD_WORKSPACE_DIRECTORY ( |
| 85 | ++ rem either a build, or a run/build inside a test |
| 86 | ++ echo cd %BUILD_WORKSPACE_DIRECTORY% |
| 87 | ++ cd %BUILD_WORKSPACE_DIRECTORY% |
| 88 | ++) else if defined TEST_WORKSPACE ( |
| 89 | ++ if defined workspace_dir ( |
| 90 | ++ echo cd: %workspace_dir% |
| 91 | ++ cd %workspace_dir% |
| 92 | ++ ) else ( |
| 93 | ++ REM this path doesn't seem required in bazel 7.2; manifest is always present |
| 94 | ++ echo set follow_links=1 |
| 95 | ++ set follow_links=1 |
| 96 | + ) |
| 97 | + ) else ( |
| 98 | +- cd "%BUILD_WORKSPACE_DIRECTORY%" |
| 99 | ++ echo Failed to find workspace to check |
| 100 | + ) |
| 101 | + |
| 102 | + call :collectFiles . |
| 103 | + |
| 104 | +-!buildifier_short_path! %EXTRA_ARGS% !files_to_analyze! |
| 105 | ++if !files_to_analyze! == "" ( |
| 106 | ++ echo Failed to find files to analyze |
| 107 | ++ exit /b 1 |
| 108 | ++) |
| 109 | ++echo !buildifier_path! %EXTRA_ARGS% !files_to_analyze! |
| 110 | ++!buildifier_path! %EXTRA_ARGS% !files_to_analyze! |
| 111 | + |
| 112 | + exit /b !ERRORLEVEL! |
| 113 | + |
| 114 | + REM Utility function to collect all wanted files. |
| 115 | + REM Exclude patterns are not supported by `dir.exe`. |
| 116 | + :collectFiles |
| 117 | +- |
| 118 | + REM First, non-symlinked files |
| 119 | + pushd %~1 |
| 120 | ++REM echo collecting in %cd% |
| 121 | + FOR /F %%f IN ('dir /b/a:-d-l-h *.bzl *.sky *.bazel *.BUILD BUILD BUILD.*.oss WORKSPACE WORKSPACE.bzlmod WORKSPACE.oss WORKSPACE.*.oss 2^>nul') DO ( |
| 122 | +- REM echo File %~1\%%f |
| 123 | +- set files_to_analyze=!files_to_analyze! %~1\%%f |
| 124 | ++ set FILE=%~1\%%f |
| 125 | ++ REM remove .\ prefix |
| 126 | ++ set FILE=!FILE:.\=! |
| 127 | ++ REM echo Found %~1\%%f adding !FILE! |
| 128 | ++ set files_to_analyze=!files_to_analyze! !FILE! |
| 129 | + ) |
| 130 | + popd |
| 131 | + |
0 commit comments