-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
Support multiple configuration files for different modules in checkstyle #464
base: main
Are you sure you want to change the base?
Conversation
lint/checkstyle.bzl
Outdated
for key in keys: | ||
if label.startswith(key): | ||
config = ctx.attr._configs[key] | ||
config_file = config.files.to_list()[0] | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you want longest matching prefix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the use case in the example, both //src/subdir
and //src
package have configuration files. I was thinking of matching the closest one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd expect it to use the config from the nearest ancestor, matching most other tools
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Checkstyle tool seems to retrieve the configuration file from the specified parameter. In our setup, we typically organize all Checkstyle configuration files within the checkstyle directory. It might make more sense to pass the configuration as a map instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a prefactoring where you move your checkstyle configs closer to the folder they govern? Having them all in a single directory seems like it makes it hard to reason about, even without taking Bazel into account. If someone wanted to update the checkstyle config for their code, how would they figure out which one to edit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point but in our case, every folder shares the same checkstyle.xml file, but some have different import-control.xml files. Managing them in a folder like this seems easier.
for key in keys: | ||
if label.startswith(key): | ||
config = ctx.attr._configs[key] | ||
config_file = config.files.to_list()[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this support allow_single_file
so that it's just config.file
?
Maybe not according to bazelbuild/bazel#5355 (comment) but worth asking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem to have allow_single_file
attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a workaround for a flaw in checkstyle, which I would expect to be able to resolve the right configuration for a given source file on its own. We should link to an issue there (creating it if needed) so it's clear that we're working around something, and in the event they fix it upstream we'll be able to remove our workaround.
open this feature request issue to checkstyle repo. checkstyle/checkstyle#16258 |
binary = "@@//tools/lint:checkstyle", | ||
config = "@@//:checkstyle.xml", | ||
configs = { | ||
"@@//:checkstyle.xml": "@@//src,@@//test", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm taking another look at what apple_rules_lint did to permit users to configure linters per-directory, i.e. https://github.com/apple/apple_rules_lint/blob/main/api.md#get_lint_config allows you to set tags
and https://github.com/apple/apple_rules_lint/blob/main/lint/private/package_lint_config.bzl#L14 allows the linter configuration to be selected in the same package being visited.
I'm curious, you must have considered just using apple_rules_lint instead of Aspect, since this checkstyle case is the example on their main README and I assume it motivated their design for selecting configs. Did you come to understand any tradeoffs between these approaches?
In some use case, different modules need different checkstyle configuration files as described in this feature request.
#454
This PR :
string_keyed_label_dict
configs
attribute which is a map of module label and configuration file.Test:
In the test, add one checkstyle_subdir.xml file for src/subdir which only differs with different line length.
Changes are visible to end-users: yes/no
Test plan