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

fix: Add option to skip target without srcs #447

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

johan-vcc
Copy link

Description

Add option 'skip_if_no_srcs' to lint_clang_tidy aspect.
Set to True to skip target if it provides CcInfo but has no source files. By default this will generate an error.

See issue #406

Adding an option to skip these targets enables us to run ./lint.sh //package//... even if some target has no source files, e.g. code generation targets.

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: yes
    Add option 'skip_if_no_srcs' to lint_clang_tidy aspect.

Test plan

  • Manual testing; please provide instructions so we can reproduce:
    bazel build with lint_clang_tidy_aspect for a target that provides 'CcInfo' but does not have 'srcs' attribute.
    With skip_if_no_srcs set to True:
    Build completes with empty target.AspectRulesLintClangTidy.out file.
    Without new option / new option set to False:
File ".../aspect_rules_lint~/lint/clang_tidy.bzl", line 186, column 38, in _filter_srcs
                return [s for s in rule.files.srcs if _is_source(s)]
Error: No attribute 'srcs' in files. Make sure there is a label or label_list type attribute with this name

Add option 'skip_if_no_srcs' to lint_clang_tidy aspect
Set to True to skip target if it provides CcInfo but has no source files. By default this will generate an error.
@CLAassistant
Copy link

CLAassistant commented Dec 9, 2024

CLA assistant check
All committers have signed the CLA.

@alexeagle
Copy link
Member

Thanks - is there ever a reason users ought to set skip_if_no_srcs=False ? If not, can we make a simpler change to always exclude rather than introduce a new configuration option?

@johan-vcc
Copy link
Author

I am unsure of other use-cases. That is why I added the option in case the old behavior is needed.
If you think a simpler always-exclude is good, that would work for my use-case.

@alexeagle
Copy link
Member

@jsharpe can you think of any reason the aspect might need to visit a library with empty srcs? Or is there a better attribute on CcInfo that tells us it's a no-op?

@jsharpe
Copy link
Contributor

jsharpe commented Dec 17, 2024

I don't think it would need to visit the target if it has an empty srcs since this is where we construct the list of files to apply clang-tidy to anyway so if that was empty there is nothing to do. IMO we can always skip if srcs is empty.

@@ -369,7 +371,7 @@ def _clang_tidy_aspect_impl(target, ctx):
if not CcInfo in target:
return []

files_to_lint = _filter_srcs(ctx.rule)
files_to_lint = _filter_srcs(ctx.rule, ctx.attr._skip_if_no_srcs)
compilation_context = target[CcInfo].compilation_context
Copy link
Contributor

Choose a reason for hiding this comment

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

Now I look at it - compilation_context could be None - see https://bazel.build/rules/lib/providers/CcInfo#CcInfo; I suspect that this will happen precisely when there are no srcs present and no deps? And if so we might need to protect other places further down where we access compilation_context?

Copy link
Member

Choose a reason for hiding this comment

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

@johan-vcc does it fix the issue for you if you check for this being None ?

Copy link
Author

Choose a reason for hiding this comment

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

Tried it out, then that check would need to be before it tries to access rule.files.srcs in _filter_srcs().
However, in my case the targets have compilation_context so that would not avoid the need to check srcs as well.

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.

4 participants