Skip to content

Commit b5c61b6

Browse files
fix(common-lint): resolve clang-format version from submodule
Replace the hardcoded CLANG_FORMAT_VERSION env var with a dynamic resolution step that reads the pinned clang-format version from the lizardbyte-common submodule's pyproject.toml. Falls back to the latest version with a warning if the submodule or pin is unavailable. Also replaces the DoozyX/clang-format-lint-action with a direct clang-format shell invocation for the diff step, removing the dependency on that third-party action.
1 parent 32438dd commit b5c61b6

1 file changed

Lines changed: 76 additions & 11 deletions

File tree

.github/workflows/__call-common-lint.yml

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ jobs:
2828
contents: read
2929
pull-requests: read
3030
runs-on: ${{ (inputs && inputs.runner && fromJson(inputs.runner)) || 'ubuntu-latest' }}
31-
env:
32-
CLANG_FORMAT_VERSION: 20
3331
steps:
3432
- name: Checkout
3533
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
@@ -80,12 +78,62 @@ jobs:
8078
with:
8179
python-version: '3.14'
8280

81+
- name: Resolve clang-format requirement
82+
shell: bash
83+
run: |
84+
requirement="clang-format"
85+
submodule="third-party/lizardbyte-common"
86+
87+
if git config -f .gitmodules --get-regexp path 2>/dev/null | grep -Fq " ${submodule}"; then
88+
if git submodule update --init --depth 1 -- "${submodule}"; then
89+
pinned_requirement=$(SUBMODULE="${submodule}" python - <<'PY'
90+
import os
91+
import re
92+
import tomllib
93+
from pathlib import Path
94+
95+
pyproject = Path(os.environ["SUBMODULE"]) / "pyproject.toml"
96+
try:
97+
config = tomllib.loads(pyproject.read_text(encoding="utf-8"))
98+
except (OSError, tomllib.TOMLDecodeError):
99+
config = {}
100+
101+
dependencies = config.get("dependency-groups", {}).get("lint-c", [])
102+
if not dependencies:
103+
dependencies = config.get("project", {}).get("optional-dependencies", {}).get("lint-c", [])
104+
for dependency in dependencies:
105+
if not isinstance(dependency, str):
106+
continue
107+
if re.fullmatch(r"clang-format==[0-9]+(?:\.[0-9]+)*(?:\.\*)?", dependency):
108+
print(dependency)
109+
break
110+
PY
111+
)
112+
113+
if [ -n "${pinned_requirement}" ]; then
114+
requirement="${pinned_requirement}"
115+
else
116+
echo "::warning::Unable to find the clang-format pin in ${submodule}; using the latest version."
117+
fi
118+
else
119+
echo "::warning::Unable to check out ${submodule}; using the latest clang-format version."
120+
fi
121+
122+
# Keep the targeted submodule checkout out of every lint file search below.
123+
git submodule deinit --force -- "${submodule}" || true
124+
else
125+
echo "${submodule} is not configured; using the latest clang-format version."
126+
fi
127+
128+
echo "Installing ${requirement}"
129+
echo "CLANG_FORMAT_REQUIREMENT=${requirement}" >> "${GITHUB_ENV}"
130+
83131
- name: Install Python dependencies
84132
shell: bash
85133
run: |
86134
# shellcheck disable=SC2102 # this is triggered by the [toolchain] extra
87135
python -m pip install --upgrade \
88-
"clang-format==${CLANG_FORMAT_VERSION}.*" \
136+
"${CLANG_FORMAT_REQUIREMENT}" \
89137
pip \
90138
setuptools \
91139
wheel \
@@ -196,15 +244,32 @@ jobs:
196244
- name: C++ - Clang format (diff)
197245
id: clang_format_diff
198246
if: always() && steps.cpp_files.outputs.found_files
199-
uses: DoozyX/clang-format-lint-action@bcb4eb2cb0d707ee4f3e5cc3b456eb075f12cf73 # v0.20
200-
with:
201-
source: ${{ steps.cpp_files.outputs.found_files }}
202-
clangFormatVersion: '${{ env.CLANG_FORMAT_VERSION }}'
203-
extensions: 'c,cpp,h,hpp,m,mm'
204-
style: file
205-
inplace: false
247+
shell: bash
248+
env:
249+
CPP_FILES: ${{ steps.cpp_files.outputs.found_files }}
250+
run: |
251+
set +e
252+
error=0
253+
# shellcheck disable=SC2086 # split the space-delimited file list
254+
for file in ${CPP_FILES}; do
255+
clang-format --style=file "${file}" |
256+
diff \
257+
--unified=3 \
258+
--label "${file} (original)" \
259+
--label "${file} (reformatted)" \
260+
"${file}" \
261+
-
262+
statuses=("${PIPESTATUS[@]}")
263+
if [ "${statuses[0]}" -ne 0 ]; then
264+
error=2
265+
elif [ "${statuses[1]}" -gt "${error}" ]; then
266+
error="${statuses[1]}"
267+
fi
268+
done
269+
set -e
270+
exit "${error}"
206271
207-
- name: C++ - Clang format (simple)
272+
- name: C++ - Clang format (annotations)
208273
if: always() && steps.clang_format_diff.outcome == 'failure'
209274
shell: bash
210275
run: |

0 commit comments

Comments
 (0)