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: ProtoCommentLinter and Spotless Custom Step #108

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

Conversation

thomas-swirlds-labs
Copy link

  • Adds new ProtoLint Spotless FormatterStep
  • Creates ProtoCommentLinter implementation for initial linting and recommendations on comment specifications

Fixes #107

Signed-off-by: Thomas Moran <152873392+thomas-swirlds-labs@users.noreply.github.com>
@thomas-swirlds-labs thomas-swirlds-labs changed the title Add initial ProtoCommentLinter and Spotless Custom Step feat: ProtoCommentLinter and Spotless Custom Step Feb 5, 2025
@jjohannes jjohannes self-requested a review February 6, 2025 07:56
Copy link
Contributor

@jjohannes jjohannes left a comment

Choose a reason for hiding this comment

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

I left some comments. We need to make sure this is working as expected, before it can be merged.

You can add the plugin to the list of plugins in org.hiero.gradle.module.library.gradle.kts

plugins {
    ...
    id("org.hiero.gradle.check.spotless-protobuf")
    ...
}

Then the check is automatically integrated/active when there are proto files in a module.

You can test it now against this branch of hedera-services:
hiero-ledger/hiero-consensus-node#17737


spotless {
format("proto") {
target("src/main/proto/**/*.proto")
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: we would need to make sure that this matches all the locations proto files are located in.

In hedera-services/hapi, There they are not located in src/main/proto. I think the solution is to move the files there. It would be more consistent.

This is also helpful for publishing the protobufs directly from the services repo (hiero-ledger/hiero-consensus-node#17425).

I started on this: hiero-ledger/hiero-consensus-node#17737

You can test this PR in hedera-services against the Branch of hiero-ledger/hiero-consensus-node#17737

return issues;
}

public static String lintAndFix(String protoContent) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method is never called. How is this intended to work in the end? Are you able to auto-format some things but others need to be fixed manually?

Copy link
Author

Choose a reason for hiding this comment

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

The first phase of this will be just to get a check working in a minimalistic way that at least informs the developer that there are styling rules that are not being followed and suggests remediations. This code is not needed yet so I will remove 👍

}

if (!issues.isEmpty()) {
throw new RuntimeException("Proto file linting failed:\n" + String.join("\n", issues));
Copy link
Contributor

Choose a reason for hiding this comment

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

When I test this, all the newlines are swallowed. Spotless has a specific features for Lint errors now.

Suggested change
throw new RuntimeException("Proto file linting failed:\n" + String.join("\n", issues));
throw Lint.shortcut(issues);


public static String lint(String protoContent) {
List<String> lines = Arrays.asList(protoContent.split("\n"));
List<String> issues = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be best to use com.diffplug.spotless.Lint

Suggested change
List<String> issues = new ArrayList<>();
List<Lint> issues = new ArrayList<>();

String line = lines.get(i).trim();

if (INLINE_COMMENT_PATTERN.matcher(line).matches()) {
issues.add("⚠️ Warning: Inline comment detected at line " + (i + 1) + ". Use `/** ... */` instead.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
issues.add("⚠️ Warning: Inline comment detected at line " + (i + 1) + ". Use `/** ... */` instead.");
issues.add(Lint.atLine((i + 1), "comment", "⚠️ Warning: Inline comment detected at line " + (i + 1) + ". Use `/** ... */` instead."));

Other places doing issues.add need to be adjusted accordingly.

plugins { id("com.diffplug.spotless") }

spotless {
format("proto") {
Copy link
Contributor

Choose a reason for hiding this comment

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

There would be the option to combine your custom formatting with the "buf" formatter, for which there is Spotless integration:

https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md#protobuf

But IIRC you already said that's not needed right?

Copy link
Author

Choose a reason for hiding this comment

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

Yes we can integrate the buf formatter for syntax and structural standards such as license header, etc. And that should be relatively straightforward. This PR was intended to address standards and recommendations for comments in the .proto files, however - so I was thinking we may want to keep them separate to simplify things for now 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. Just wanted to mention it to make sure we explore the possibility. We can do it in a separate step. I added a comment here: #107 (comment)

spotless {
format("proto") {
target("src/main/proto/**/*.proto")
addStep(org.hiero.gradle.spotless.ProtoLintStep.create())
Copy link
Contributor

@jjohannes jjohannes Feb 6, 2025

Choose a reason for hiding this comment

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

Should probably also add license header check. It would look like this:

licenseHeader(LicenseHeader.javaFormat(project), "(syntax)").updateYearWithLatest(true)

Which would put the header above the syntax keyword and other comments would then have to go below it.

But if I look at the "proto" files in hedera-services, the license header is not in the top right now but somewhere in the middle. Does not look right....

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.

New Spotless Protobuf Plugin chore: Implement lint and styling utility for Protocol Buffer files
2 participants