Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: clang-tidy for linting of C/C++ code #287

Merged
merged 25 commits into from
Jun 16, 2024

Conversation

peakschris
Copy link
Contributor

@peakschris peakschris commented Jun 15, 2024

Support clang-tidy for linting of C++ code.

Inspired by and containing some code from bazel_clang_tidy repo. This integration has been discussed by maintainers of both repos in erenon/bazel_clang_tidy#35.

This PR replaces #284, which had become noisy with too many commits. Since then, the code has been rewritten to return a single report for each target.

Fixes #178


Changes are visible to end-users: yes

  • Searched for relevant documentation and updated as needed: yes
  • Breaking change (forces users to change their own code or config): no
  • Suggested release notes appear below: todo

Test plan

  • New test cases added
  • Extra test cases could be added but need discussion; what is possible in CI?

Code used from bazel_clang_tidy repo

bazel_clang_tidy repo helped:

  • two helper functions toolchain_flags and safe_flags
  • some lines in get_args
  • inspiration & learning

The bulk of the code is implemented by following existing rules_lint code such as ruff.bzl and eslint.bzl.

Input into release notes (will reformat)

  • warning that git bash does not handle arguments correctly and --fix may have issues on Windows. msys64 bash (already recommended by Bazel is ok).

@peakschris
Copy link
Contributor Author

peakschris commented Jun 15, 2024

I'm fairly happy with this, it's getting closer now...

Supported:

  • bash lint.sh //src/... (windows)
  • ./lint.sh //src/... (linux)
  • ./lint.sh --fix //src:hello_cc (linux)
  • ./lint.sh --fix //src/cpp/... (linux)

Needs discussion:

  • some todo statements in clang_tidy_aspect_impl, need to decide which targets are ignored
  • error code handling in clang_tidy_wrapper.sh, I am distinguishing fatal from user-selected errors

Known limitations:

  • no packaged clang-tidy.exe on windows as llvm_toolchain doesn't support windows at present

Separate and general windows issues (to be tackled separately)

  • bash lint.sh --fix //src:hello_cc fails on windows due to missing node.bat: FATAL: aspect_rules_js[js_binary]: node wrapper '/d/udu/b/l2x2bhay/execroot/_main/./external/aspect_rules_lint~/lint/private/patcher_node_bin/node.bat' not found
  • example/.bazeliskrc causes builds to fail as aspect-cli doesn't support windows
  • example/.bazelversion causes builds to fail as bazelisk doesn't support ../ notation
  • bazel run //docs:update_10 fails:
Target //docs:update_10 failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: Analysis of target '//docs:update_10' failed; build aborted: Target //docs:update_10 is incompatible and cannot be built, but was explicitly requested.
Dependency chain:
    //docs:update_10 (2bc110)
    //docs:clang-tidy-docgen.md (2bc110)
    //docs:clang-tidy (2bc110)   <-- target platform (@@local_config_platform//:host) didn't satisfy constraint @@platforms//:incompatible
  • cd example; bazel test //... fails (e.g. buf target, which isn't configured on windows)

@alexeagle
Copy link
Member

alexeagle commented Jun 16, 2024

I'm okay landing this without windows support, we don't have testing there currently https://github.com/aspect-build/rules_lint/blob/main/.bcr/presubmit.yml#L5 so there's an existing project to fix it. Not many OSS contributors enthusiastic about both Bazel and Windows because so much is broken, but I hope to help fix that :)

make example repo support windows by default and remove this

Yes let's make a separate PR to add Windows testing for existing code

@peakschris
Copy link
Contributor Author

@alexeagle thanks, agreed we need separate tracking for windows. Would be good to get this in for linux for now. I've removed the windows_workaround.bat.

Please would you restart CI? I've fixed some of the failures.

Copy link
Member

@alexeagle alexeagle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, excited to have this. /cc @jsharpe

example/.clang-tidy Outdated Show resolved Hide resolved
example/lint.sh Show resolved Hide resolved
example/src/cpp/lib/hello-time.cc Show resolved Hide resolved
example/tools/lint/BUILD.bazel Show resolved Hide resolved
example/tools/lint/linters.bzl Outdated Show resolved Hide resolved
lint/clang_tidy_wrapper.sh Outdated Show resolved Hide resolved
lint/clang_tidy.bzl Outdated Show resolved Hide resolved
lint/clang_tidy.bzl Outdated Show resolved Hide resolved
lint/clang_tidy.bzl Outdated Show resolved Hide resolved
lint/clang_tidy.bzl Outdated Show resolved Hide resolved
@peakschris
Copy link
Contributor Author

@alexeagle looks like there are 3 ci pipelines failing... are they due to this PR? I couldn't see the relationship between this change and the starlark failures... Can I run the integration tests myself, to see what the issue is there?

@peakschris
Copy link
Contributor Author

@alexeagle have pushed a fix for latest ci run

@alexeagle
Copy link
Member

I ran buildifier locally and corrected what it pointed out, see the most recent commit.

@peakschris
Copy link
Contributor Author

Awesome, thanks for your help @alexeagle. What are your thoughts? I'm happy to do more work if needed on this, either before or after merge.

Copy link
Member

@alexeagle alexeagle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM let's ship and get some user feedback 👍🏻

@alexeagle alexeagle merged commit 7a14cfd into aspect-build:main Jun 16, 2024
8 checks passed
@peakschris
Copy link
Contributor Author

Amazing! Thank you for your help with this 🥳

@peakschris peakschris deleted the clang-tidy2 branch June 16, 2024 23:28
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.

[Bug]: lint.sh stops on first issue on Windows
2 participants