Skip to content

feat: clang-tidy emit action per file to lint#512

Open
molar wants to merge 2 commits intoaspect-build:mainfrom
molar:molar_per-file-lint-actions
Open

feat: clang-tidy emit action per file to lint#512
molar wants to merge 2 commits intoaspect-build:mainfrom
molar:molar_per-file-lint-actions

Conversation

@molar
Copy link
Contributor

@molar molar commented Apr 3, 2025

Some cc_library/binary targets may contain 10+ files listed in sources and as clang-tidy runs single threaded, this can lead
to long running linting actions. This PR emits a clang-tidy action per source file, so they can be parallelised locally/remotely. The stdout/stderror is merged in unspecified order according to the order of the srcs using a simple
concatenation. The exit_code of the individual action, is merged by sorting them numerically and selecting the highest number.

I did also consider making the splitting optional, in case we are unsure of the impact of this change. Let me know if i should do that.

I also considered updated the progress message to use the source file short path instead, but decided to leave it out here. Currently the progress message will be the same for each individual action and makes it harder to see which source file is being linted.

Test plan

  • Covered by existing test cases
  • Manual testing was also done, both in our own bazel repo and in the examples folder. It has only been tested on linux, so there could be issues with the behaviour of sort and head on macOS.
  • I would like some guidance on testing the fix part as we do not use the aspect wrapper in-house

@molar molar changed the title clang-tidy emit action per file to lint and merge stdout and error codes [feat] clang-tidy emit action per file to lint and merge stdout and error codes Apr 3, 2025
@molar molar changed the title [feat] clang-tidy emit action per file to lint and merge stdout and error codes [feat] clang-tidy emit action per file to lint Apr 3, 2025
@molar molar changed the title [feat] clang-tidy emit action per file to lint feat: clang-tidy emit action per file to lint Apr 9, 2025
@molar
Copy link
Contributor Author

molar commented May 1, 2025

Friendly ping @alexeagle do you have some input or can you kindly suggest a reviewer?

@alexeagle
Copy link
Member

I'm sorry - I looked through this once and then got in my head that it was blocked on something.

It's a bit complex/gross to have to merge the exit codes back together this way. like you say, it's using non-hermetic system tools, plus space delimiting is risky when filenames can have spaces, so may need some quoting work.

Is it possible to move the splitting earlier? I'm imagining the aspect visits a cc_library target and produces an action per file at that point. Bazel sees all the actions and can react to the non-zero exit code(s) itself. I think there's no need to aggregate the results back to the library level, right?

@alexeagle
Copy link
Member

There's a new feature coming in Bazel that's likely useful here, action templates.

https://github.com/bazelbuild/bazel/commit/dcc3f29361b3a29766081828107bdfb1f8f4079d#diff-ae4a1903c6ded3074309089f8[…]81329a14b8b7147bc7edffR832-R837

I think we might want to wait and re-base on that rather than do something difficult and short-term

@alexeagle alexeagle added the need: discussion We have to do some design work in a meeting label Aug 30, 2025
@alexeagle
Copy link
Member

@molar are you still interested here? Bazel 9 has ctx.actions.map_directory and I've just sent a PR to bazel_features so we can detect its presence and do per-file actions conditionally.

@molar
Copy link
Contributor Author

molar commented Jan 6, 2026

@alexeagle Thank you for your time on this. I will not be working on this for the time being unfortunately.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

gergondet-woven added a commit to gergondet-woven/rules_lint that referenced this pull request Jan 27, 2026
This is similar - with similar motivations - to the feature proposed in
aspect-build#512

I worked on this around the same time and stumbled upon that PR only
recently.

The approach this patch follows is different however and I think in line
with what @alexeagle suggested in [this
comment](aspect-build#512 (comment)),
i.e. one action is generated per file.

The way this is introduced should make it possible to re-use this logic
to implement similar per-file logic for other linters.

To do so we:
- add a new parameter to output_files/patch_files so that they can
  declare per-file outputs/patches
- adapt the _lint_test rule so that it can handle multiple
  output/exit_code files
- use the new capability in the clang-tidy aspect implementation

I believe fixing works correctly with this approach although we do not
use the Aspect CLI for this but a shell script similar to the lint.sh
gist.

We have been using this patch for a while on version 1.10.2 with no
issues. I have re-adapted the patch for the version 2.0.0 of the
ruleset but the changes were pretty much the same.

I have also checked the unit tests of this repo and the cpp example.
gergondet-woven added a commit to gergondet-woven/rules_lint that referenced this pull request Jan 27, 2026
This is similar - with similar motivations - to the feature proposed in
aspect-build#512

I worked on this around the same time and stumbled upon that PR only
recently.

The approach this patch follows is different however and I think in line
with what @alexeagle suggested in [this
comment](aspect-build#512 (comment)),
i.e. one action is generated per file.

The way this is introduced should make it possible to re-use this logic
to implement similar per-file logic for other linters.

To do so we:
- add a new parameter to output_files/patch_files so that they can
  declare per-file outputs/patches
- adapt the _lint_test rule so that it can handle multiple
  output/exit_code files
- use the new capability in the clang-tidy aspect implementation

I believe fixing works correctly with this approach although we do not
use the Aspect CLI for this but a shell script similar to the lint.sh
gist.

We have been using this patch for a while on version 1.10.2 with no
issues. I have re-adapted the patch for the version 2.0.0 of the
ruleset but the changes were pretty much the same.

I have also checked the unit tests of this repo and the cpp example.
alexeagle pushed a commit that referenced this pull request Jan 28, 2026
This is similar - with similar motivations - to the feature proposed in
#512

I worked on this around the same time and stumbled upon that PR only
recently.

The approach this patch follows is different however and I think in line
with what @alexeagle suggested in [this

comment](#512 (comment)),
i.e. one action is generated per file.

The way this is introduced should make it possible to re-use this logic
to implement similar per-file logic for other linters.

To do so we:
- add a new parameter to output_files/patch_files so that they can
declare per-file outputs/patches
- adapt the _lint_test rule so that it can handle multiple
output/exit_code files
- use the new capability in the clang-tidy aspect implementation

I believe fixing works correctly with this approach although we do not
use the Aspect CLI for this but a shell script similar to the lint.sh
gist.

### Test plan

- Covered by existing test cases: confirmed the repo unit tests and the
cpp example still work
- Manual testing: we have been using this patch for a while on version
1.10.2 with no issues. I have re-adapted the patch for the version 2.0.0
of the ruleset but the changes were pretty much the same.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

need: discussion We have to do some design work in a meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants