Skip to content

Conversation

@Roasbeef
Copy link
Member

In this PR we introduce ast-grep (sg) for automated style enforcement, encoding many of the formatting patterns documented in docs/development_guidelines.md as machine-checkable rules. The rules cover multi-line function call wrapping, struct literal formatting, switch/select case spacing, inline comments, and function definition wrapping. Log and error calls are correctly excluded from the symmetric wrapping rules since they follow different compact formatting guidelines.

Two new Makefile targets are added: make ast-lint to check for style issues and make ast-grep-fix to auto-fix safe violations. Both support a pkg parameter to focus on specific directories (e.g., make ast-lint pkg=wallet). Generated files like *.pb.go and sqlc output are automatically excluded from scanning.

Here's an example of a violation it finds based on our guidelines:

make ast-grep pkg=htlcswitch 

warning[func-call-asymmetric-wrap]: Multi-line function call has asymmetric wrapping
     ┌─ htlcswitch/link_test.go:2710:34
     │
2710 │       htlcAmt, totalTimelock, hops := generateHops(htlcAmt, testStartingHeight,
     │ ╭─────────────────────────────────────^
2711 │ │         coreLink)
     │ ╰─────────────────^
     │
     = Per docs/development_guidelines.md: When wrapping function calls,
       if the first arg is on the opening line, the closing paren
       should also be on its own line OR all args should fit on one line.

       WRONG:
         value, err := bar(a,
         b, c)

       RIGHT (option 1 - all on new lines):
         value, err := bar(
         a, b, c,
         )

       RIGHT (option 2 - all on one line if fits):
         value, err := bar(a, b, c)

       NOTE: Log and error calls are excluded from this rule per guidelines.

This commit introduces ast-grep (sg) rules for automated style
enforcement, encoding many of the patterns documented in
docs/development_guidelines.md as machine-checkable rules.

The rules/ directory contains two rule files:

go-formatting.yml enforces:
- struct-literal-asymmetric-close: Multi-line struct literals need the
  closing brace on its own line
- func-call-asymmetric-wrap: Multi-line function calls need symmetric
  wrapping (excludes log/error calls which have special compact rules)
- log-error-expanded-form: Hints when log/error calls use expanded form
  instead of the preferred compact form
- switch-case-needs-spacing: Switch cases should have blank lines between
- select-case-needs-spacing: Select cases should have blank lines between
- no-inline-comments: Comments should be on their own line above code

go-func-def.yml enforces:
- func-def-dangling-param-comma: No dangling comma before close paren
- func-def-dangling-return-paren: Return types shouldn't start with open
  paren on its own line

The sgconfig.yml points ast-grep to the rules/ directory for scanning.
This commit adds two new Makefile targets for running ast-grep:

- make ast-lint: Run ast-grep style checks against the codebase
- make ast-grep-fix: Auto-fix safe style issues

Both targets support an optional pkg parameter to focus on a specific
directory (e.g., make ast-lint pkg=lnwallet). Generated files like
*.pb.go are automatically excluded from scanning.
@gemini-code-assist
Copy link

Summary of Changes

Hello @Roasbeef, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request integrates ast-grep into the project's build system to automate the enforcement of Go coding style guidelines. This initiative aims to improve code consistency, reduce manual review burden, and ensure adherence to established formatting patterns by providing both a linter and an auto-fix tool for common style violations, thereby streamlining the development workflow.

Highlights

  • Automated Style Enforcement: Introduces ast-grep (sg) to automatically enforce Go coding style guidelines, encoding patterns from docs/development_guidelines.md into machine-checkable rules.
  • New Makefile Targets: Adds make ast-lint for checking style issues and make ast-grep-fix for automatically correcting safe violations. Both support a pkg parameter for directory-specific scans.
  • Comprehensive Style Rules: Implements rules covering multi-line function call wrapping, struct literal formatting, switch/select case spacing, inline comments, and function definition wrapping, with specific exclusions for log and error calls.
  • Exclusion of Generated Files: Automatically excludes generated files (e.g., *.pb.go, sqlc output) from ast-grep scans to prevent unnecessary warnings.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces ast-grep to automate style guide enforcement, which is an excellent improvement for maintaining code quality and consistency. The new Makefile targets are well-implemented, and the ast-grep rules you've written seem to correctly capture many of the formatting guidelines from docs/development_guidelines.md. My review includes a few suggestions to adjust the severity of some rules to better align with the project's review standards.

# Per guidelines, log and error formatting should minimize lines.
id: log-error-expanded-form
language: go
severity: hint

Choose a reason for hiding this comment

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

medium

The severity for this rule is set to hint. Given the project's code review standards to flag issues of medium severity and higher, this might be too low to be actionable. Consider raising it to warning to ensure these style violations are addressed.1

severity: warning

Style Guide References

Footnotes

  1. The style guide recommends minimizing lines for log and error messages, preferring a compact format over an expanded one.

# Detects consecutive case clauses without blank line spacing.
id: switch-case-needs-spacing
language: go
severity: hint

Choose a reason for hiding this comment

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

medium

The severity for this rule is hint. To better enforce the style guide's preference for spacing between switch cases, please consider increasing the severity to warning. This will make the rule more effective in automated checks, aligning with the goal of flagging medium severity issues and higher.1

severity: warning

Style Guide References

Footnotes

  1. The style guide recommends adding spacing between case stanzas in switch and select statements to improve readability.

# Rule: Select cases should be separated by blank lines.
id: select-case-needs-spacing
language: go
severity: hint

Choose a reason for hiding this comment

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

medium

Similar to the switch-case-needs-spacing rule, the severity here is hint. It is recommended to change this to warning to ensure consistent enforcement of the style guide's spacing rules for select statements.1

severity: warning

Style Guide References

Footnotes

  1. The style guide recommends adding spacing between case stanzas in switch and select statements to improve readability.

Copy link
Member

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

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

TIL, very cool tool!

sg scan $(AST_GREP_EXCLUDE) $(AST_GREP_PATH)

#? ast-grep-fix: Auto-fix ast-grep style issues. Use pkg=<dir> to focus on a specific directory.
ast-grep-fix:
Copy link
Member

Choose a reason for hiding this comment

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

Tried make ast-grep-fix pkg=lnwallet but it didn't fix.

AST_GREP_PATH := $(if $(pkg),$(pkg),.)

#? ast-lint: Run ast-grep style checks. Use pkg=<dir> to focus on a specific directory.
ast-lint:
Copy link
Member

Choose a reason for hiding this comment

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

Looks like it giving false positives, or should we update our guidelines to be more specific?

warning[func-def-dangling-return-paren]: Function return types should not start with open paren on its own line
     ┌─ lnwallet/channel_test.go:1625:1
     │  
1625 │ ╭ func TestHTLCSigNumber(t *testing.T) {
     · │
1775 │ │ }
     │ ╰─^
     │  
     = Per docs/development_guidelines.md: Return type wrapping should continue
       on the same line, not start a new line with open paren.
       
       WRONG:
         func bar(a, b, c) (
         d, error,
         ) {
       
       RIGHT:
         func baz(a, b, c) (d,
         error) {

@saubyk saubyk added this to v0.21 Nov 27, 2025
@saubyk saubyk moved this to In progress in v0.21 Nov 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

2 participants