Skip to content

fix(ci): lint test files on PRs without breaking push-to-main#27059

Merged
mrpollo merged 1 commit intomainfrom
mrpollo/clang-tidy-diff-compile-db-filter
Apr 11, 2026
Merged

fix(ci): lint test files on PRs without breaking push-to-main#27059
mrpollo merged 1 commit intomainfrom
mrpollo/clang-tidy-diff-compile-db-filter

Conversation

@mrpollo
Copy link
Copy Markdown
Contributor

@mrpollo mrpollo commented Apr 11, 2026

Follow-up to #27055. clang-tidy-diff was flagging gtest/gtest.h file not found on PRs that touched test files because test TUs are not in the compilation database unless BUILD_TESTING=ON.

On PR events the workflow now calls make clang-ci, a new umbrella target that also configures a second build dir (build/px4_sitl_default-clang-test) with -DCMAKE_TESTING=ON. clang-tidy-diff-18.py runs against that dir so test files have resolved gtest includes. The line filter keeps pre-existing issues invisible.

Push-to-main is untouched: same single build, same make clang-tidy, same behavior.

Needs libclang-rt-18-dev installed in the container so fuzztest's ASAN flags don't break the abseil configure. Also replaces the fragile :!*/test/* pathspec with Tools/ci/clang-tidy-diff-filter.py, which drops diff entries that aren't in compile_commands.json.

@mrpollo mrpollo marked this pull request as draft April 11, 2026 03:30
@mrpollo mrpollo changed the title fix(ci): filter clang-tidy-diff by compile_commands.json, not path globs fix(ci): clang-tidy PRs analyze test files with BUILD_TESTING and filter unknown TUs Apr 11, 2026
@mrpollo
Copy link
Copy Markdown
Contributor Author

mrpollo commented Apr 11, 2026

CI verification on 97b68d4 (Clang-Tidy pass in 1m59s):

  • libclang-rt-18-dev installs cleanly in px4-dev container on RunsOn
  • cmake configure with BUILD_TESTING=ON succeeds in 6.3s: ABSL_INTERNAL_AT_LEAST_CXX17 - Success, CMAKE_HAVE_LIBC_PTHREAD - Success, Found Threads: TRUE
  • Second build dir build/px4_sitl_default-clang-test configured successfully (TUs populated with test files)
  • clang-tidy-diff-filter.py ran against the test-enabled build dir: kept 0 of 2 changed files (neither of this PR's files are C/C++ sources)
  • clang-tidy-diff short-circuited cleanly: No analyzable files in diff
  • Artifact: pr_number=27059 event=COMMENT comments=0
  • Poster: No comments in artifact; nothing to post. No spurious reviews posted.

The test-file-lint path itself (BUILD_TESTING=ON → test TUs in compile_commands.json → clang-tidy-diff on a modified test file) was verified in the px4-dev container locally: modifying HysteresisTest.cpp with a new NULL pointer correctly flagged hicpp-use-nullptr and clang-diagnostic-unused-variable on the new line, while pre-existing issues elsewhere in the same file stayed suppressed by clang-tidy-diff's line filter (Suppressed ... 1 due to line filter).

mrpollo added a commit that referenced this pull request Apr 11, 2026
Replace the inline `if [ "${{ github.event_name }}" = "pull_request" ]`
branches with separate workflow steps gated on `github.event_name`.
The push-to-main path is now two plainly named steps
(`Build - px4_sitl_default (Clang)` + `Run Clang-Tidy Analysis (push)`)
that exactly reproduce the pre-PR-#27059 commands, and the pull request
path is its own pair with `clang-ci` + run-clang-tidy-pr.py. Makes it
visually obvious from the workflow file that the push path is
unchanged.

Signed-off-by: Ramon Roche <mrpollo@gmail.com>
@mrpollo mrpollo force-pushed the mrpollo/clang-tidy-diff-compile-db-filter branch from 183d27f to e9a60be Compare April 11, 2026 15:07
@mrpollo mrpollo changed the title fix(ci): clang-tidy PRs analyze test files with BUILD_TESTING and filter unknown TUs fix(ci): lint test files on PRs without breaking push-to-main Apr 11, 2026
@mrpollo mrpollo marked this pull request as ready for review April 11, 2026 15:10
@mrpollo mrpollo force-pushed the mrpollo/clang-tidy-diff-compile-db-filter branch 2 times, most recently from 7e76c5a to 924c2b7 Compare April 11, 2026 15:43
The pr-review-poster was flagging `gtest/gtest.h file not found` on any
PR that added or modified a test file, because clang-tidy-diff-18.py
ran against files that weren't in the compilation database. PR #27004
and PR #26233 both hit this. The root cause is that test TUs only
enter compile_commands.json when BUILD_TESTING is ON, which the
historical clang-tidy build does not enable.

This PR fixes both halves of the problem:

1. Add a second make target `px4_sitl_default-clang-test` that configures
   a separate build dir with -DCMAKE_TESTING=ON. Test TUs land in its
   compile_commands.json with resolved gtest/fuzztest include paths.

2. Add an umbrella `clang-ci` target that depends on both
   `px4_sitl_default-clang` and `px4_sitl_default-clang-test` so the PR
   job prepares both build dirs with one make invocation.

3. On PR events the workflow uses `make clang-ci`, installs
   libclang-rt-18-dev (needed so fuzztest's FUZZTEST_FUZZING_MODE flags
   do not fail the abseil try_compile with a misleading "pthreads not
   found" error), and routes the clang-tidy-diff producer at the
   test-enabled build dir.

4. Push-to-main is left entirely alone: same single build dir, same
   `make px4_sitl_default-clang`, same `make clang-tidy`. Test files
   are not in that DB so run-clang-tidy.py keeps ignoring them exactly
   as before. This preserves green main while ~189 pre-existing
   clang-tidy issues in test files remain untouched; fixing those is
   out of scope for this change.

5. Replace the fragile `:!*/test/*` pathspec filter (which missed flat
   `*Test.cpp` files in module roots) with
   `Tools/ci/clang-tidy-diff-filter.py`, which reads the compilation
   database and drops any changed source file that is not a TU.
   Headers always pass through. Production code that happens to use
   test-like names (src/systemcmds/actuator_test, src/drivers/test_ppm,
   etc.) stays analyzed because those are real px4_add_module targets.

Verified in the ghcr.io/px4/px4-dev:v1.17.0-rc2 container and on the
real CI runner:
- cmake configure with CMAKE_TESTING=ON succeeds after installing
  libclang-rt-18-dev (Found Threads: TRUE)
- compile_commands.json grows from 1333 to 1521 TUs
- Modifying HysteresisTest.cpp with a new `const char *p = NULL`
  correctly flags hicpp-use-nullptr and
  clang-diagnostic-unused-variable on the new line, while pre-existing
  issues on other lines of the same file stay suppressed by
  clang-tidy-diff-18.py's line filter ("Suppressed ... 1 due to line
  filter")
- No gtest/gtest.h false positives
- Push-to-main path unchanged, still green

Signed-off-by: Ramon Roche <mrpollo@gmail.com>
@mrpollo mrpollo force-pushed the mrpollo/clang-tidy-diff-compile-db-filter branch from 924c2b7 to ff3e85f Compare April 11, 2026 15:48
@mrpollo mrpollo marked this pull request as draft April 11, 2026 15:57
@mrpollo mrpollo force-pushed the mrpollo/clang-tidy-diff-compile-db-filter branch from 096bf1a to ff3e85f Compare April 11, 2026 16:01
@mrpollo mrpollo marked this pull request as ready for review April 11, 2026 16:02
@mrpollo mrpollo merged commit 1cf7d75 into main Apr 11, 2026
81 of 117 checks passed
@mrpollo mrpollo deleted the mrpollo/clang-tidy-diff-compile-db-filter branch April 11, 2026 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant