Skip to content

Conversation

@matrei
Copy link
Contributor

@matrei matrei commented Jul 22, 2025

Rules can be introduced gradually, and source files reformatted incrementally to maintain a manageable review process and avoid overwhelming diffs.

Formatting commits will be squashed before merging to minimize noise in the project history.

Related: #14903

matrei added 5 commits July 22, 2025 14:15
Add Checkstyle and Codenarc configuration for the `grails.core.ROOT`
and `grails-gradle` projects to enforce consistent coding standards.

Rules will be introduced gradually, and source files reformatted
incrementally to maintain a manageable review process and avoid
overwhelming diffs.

Formatting commits will be squashed before merging to minimize noise in
the project history.
Add the `NewlineAtEndOfFile` Checkstyle rule and
`FileEndsWithoutNewline` Codenarc rule to ensure all source files end
with a single newline character.

This is a long-standing POSIX convention and is assumed by many Unix
tools. It also helps avoid unnecessary noise in version control diffs
and ensures consistent formatting across editors and operating systems.

Most modern IDEs can be configured to add the final newline
automatically.

https://checkstyle.sourceforge.io/checks/misc/newlineatendoffile.html
https://codenarc.org/codenarc-rules-formatting.html#fileendswithoutnewline-rule
Add the `FileTabCharacter` Checkstyle rule and
`NoTabCharacter` Codenarc rule to ensure that source files
use spaces instead of tabs.

This helps maintain consistent indentation and avoids
formatting issues across different editors and environments.

https://checkstyle.sourceforge.io/checks/whitespace/filetabcharacter.html
https://codenarc.org/codenarc-rules-convention.html#notabcharacter-rule
Allows selective suppression of specific Checkstyle rules where
necessary, improving flexibility for edge cases or legacy code.
Add the Checkstyle `UnusedImports` rule and the Codenarc
`UnusedImport` and `UnnecessaryGroovyImport` rules.

These checks help clean up forgotten or unnecessary import statements,
keeping the codebase tidy and easier to maintain.

https://checkstyle.sourceforge.io/checks/imports/unusedimports.html#UnusedImports
https://codenarc.org/codenarc-rules-imports.html#unusedimport-rule
https://codenarc.org/codenarc-rules-imports.html#unnecessarygroovyimport-rule
matrei added 5 commits July 23, 2025 11:07
Order imports alphabetically within these groups:

  1) java
  2) javax
  3) groovy (groovy, org.apache.groovy, org.codehaus.groovy)
  4) jakarta
  5) all others
  6) org.springframework
  7) grails (grails, org.apache.grails, org.grails)
  8) all static imports

This mirrors Spring Framework’s convention and keeps imports tidy,
predictable, and easy to scan.

Checkstyle can enforce this for Java. I couldn’t find a way to make
CodeNarc or Spotless apply the same grouping for Groovy.

Most IDEs, including IntelliJ, can apply this import ordering and
grouping automatically, either on save or via a shortcut, when
properly configured. The code style settings can be shared in the
repository to ensure consistency.
# Conflicts:
#	grails-common/src/main/groovy/org/apache/grails/common/annotation/RecursiveAnnotationAttributesVisitor.java
#	grails-common/src/main/groovy/org/apache/grails/common/compiler/asm/AnnotationMetadataReader.java
#	grails-common/src/main/groovy/org/apache/grails/common/compiler/asm/ClassReader.java
#	grails-common/src/main/groovy/org/apache/grails/common/compiler/asm/Context.java
#	grails-core/src/main/groovy/grails/boot/config/tools/ClassPathScanner.groovy
#	grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/utils/AnnotationMetadataReaderFactory.java
matrei added 4 commits August 8, 2025 15:57
Description from Codenarc rule `UnnecessaryGString`:
String objects should be created with single quotes, and GString objects
created with double quotes. Creating normal String objects with double
quotes is confusing to readers.
# Conflicts:
#	grails-gradle/docs-core/src/main/groovy/grails/doc/PdfBuilder.groovy
#	grails-gradle/docs-core/src/main/groovy/grails/doc/gradle/PublishPdf.groovy
#	grails-gsp/core/src/main/groovy/org/grails/gsp/compiler/GroovyPageCompiler.groovy
#	grails-test-examples/app1/grails-app/controllers/functionaltests/commandobjects/CommandObjectController.groovy
These were star-imported and we do not do that anymore.
matrei added 7 commits August 8, 2025 17:05
Add the Checkstyle and Codenarc Indentation
rules.

Use the default setting of four spaces.
# Conflicts:
#	grails-core/src/main/groovy/org/grails/compiler/injection/GlobalGrailsClassInjectorTransformation.groovy
#	grails-gradle/plugins/src/e2eTest/groovy/org/grails/gradle/test/GradleSpecification.groovy
#	grails-gradle/plugins/src/e2eTest/groovy/org/grails/gradle/test/GrailsPublishPluginSpec.groovy
#	grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/profiles/GrailsProfilePublishGradlePlugin.groovy
#	grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/publishing/GrailsPublishExtension.groovy
#	grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/publishing/GrailsPublishGradlePlugin.groovy
#	grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/publishing/PublishType.groovy
#	grails-gsp/core/src/main/groovy/org/grails/gsp/compiler/GroovyPageCompiler.groovy
# Conflicts:
#	grails-views-gson/src/main/groovy/grails/plugin/json/view/JsonViewGrailsPlugin.groovy
#	grails-views-markup/src/main/groovy/grails/plugin/markup/view/MarkupViewGrailsPlugin.groovy
@jamesfredley
Copy link
Contributor

@matrei I resolved the merge conflicts with 7.0.x after #14988. I overlooked the single quotes when resolving the conflicts, but pushed another commit to change them back.

# Conflicts:
#	grails-data-hibernate5/dbmigration/src/main/groovy/org/grails/plugins/databasemigration/DatabaseMigrationGrailsPlugin.groovy
#	grails-fields/grails-app/taglib/grails/plugin/formfields/FormFieldsTagLib.groovy
#	grails-scaffolding/src/main/groovy/grails/plugin/scaffolding/ScaffoldingGrailsPlugin.groovy
#	grails-views-gson/src/main/groovy/grails/plugin/json/view/JsonViewGrailsPlugin.groovy
#	grails-views-markup/src/main/groovy/grails/plugin/markup/view/MarkupViewGrailsPlugin.groovy
@jamesfredley
Copy link
Contributor

I resolved the merge conflicts after #15003

Copy link
Contributor

@jamesfredley jamesfredley left a comment

Choose a reason for hiding this comment

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

@matrei Thank you. This makes a massive improvement to the codebase and I am onboard with the format decision you have selected.

How do you propose we keep in sync with these rules and should we apply formatting from the IDE or a Gradle task?

Copy link
Contributor

@jdaugherty jdaugherty left a comment

Choose a reason for hiding this comment

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

This appears to be an excellent, gradual approach to code style enforcement. Great Job @matrei!

Some concerns:

  1. Auto-formatting. I don't see an intellij configuration checked in, editorconfig, or spotless. Do you have a way to auto format to prevent or minimize styling errors?

  2. Clear indication of why the Github action fails. Can we configure the code style validations to have their own Github action & then disable them in the main test runs? My concern is we won't be able to tell what's broken and we can often fix styling issues in parallel to fixing tests. This will also avoid slowing down test builds.

  3. As part of #2, can we generate a single code narc report that's published as part of the code style build? Can the same be generated for checkstyle?

@matrei
Copy link
Contributor Author

matrei commented Aug 24, 2025

I think we should actually remove the Checkstyle and CodeNarc files from this PR and only squash the code formatting changes.
Then we add back Checkstyle, CodeNarc and code formatting helpers in another commit that is not in .git-blame-ignore-revs.
What do you think?

@jdaugherty
Copy link
Contributor

jdaugherty commented Aug 24, 2025

I am ok with that, but do you agree with separating into a separate action so we can fix issues in parallel?

@matrei
Copy link
Contributor Author

matrei commented Aug 24, 2025

As the full build takes like 40 minutes, will it not actually save CI time/resources to fail early, before starting to run the test suites.
The tests will have to run again anyway. I think that is how the Spring repos handles it, it fails directly on formatting errors.

@jdaugherty
Copy link
Contributor

The issue is fixing in parallel, so if it's only a code style error vs a real test error. This allows someone to fix the formatting issues after finding a solution to aprobelm / the tests.

I'm proposing to move the formatting errors to it's own build.

We also need to upload the errors somewhere - they currently reference an html file that can't be viewed if it's from an action.

@matrei
Copy link
Contributor Author

matrei commented Aug 25, 2025

This allows someone to fix the formatting issues after finding a solution to aprobelm / the tests.

But shouldn't one preferably run the build locally to test changes before pushing?

We also need to upload the errors somewhere - they currently reference an html file that can't be viewed if it's from an action.

Yes, any code style error reports should be uploaded to the summary page.

@jdaugherty
Copy link
Contributor

This allows someone to fix the formatting issues after finding a solution to aprobelm / the tests.

But shouldn't one preferably run the build locally to test changes before pushing?

We also need to upload the errors somewhere - they currently reference an html file that can't be viewed if it's from an action.

Yes, any code style error reports should be uploaded to the summary page.

Yes, but third party contributors may not always do that. Having a separate run makes it clear why it's failing and if it's a functionality problem or a best practice/ style issue.

I also wish to use that same functionality locally at times - there have been many times where I'm debugging an issue and don't want to worry about code style locally until a solution has been found. I 100% agree we should eventually be compliant with the code style, but I want a way to opt out locally when testing or making large changes.

@matrei
Copy link
Contributor Author

matrei commented Aug 25, 2025

but I want a way to opt out locally when testing or making large changes.

Isn't that why there are separate test and check Gradle tasks?

@jdaugherty
Copy link
Contributor

but I want a way to opt out locally when testing or making large changes.

Isn't that why there are separate test and check Gradle tasks?

It's my understanding that test only runs the unit tests.

@matrei
Copy link
Contributor Author

matrei commented Aug 25, 2025

but I want a way to opt out locally when testing or making large changes.

Isn't that why there are separate test and check Gradle tasks?

It's my understanding that test only runs the unit tests.

Yes, integration tests are a feature added by the Integration Test Grails Gradle Plugin. This is not wired to the test task but on the check task. So at present you will have to run ./gradlew test iT to run all tests.

@jdaugherty
Copy link
Contributor

I suggest we remove the gradle codenarc / checkstyle config, update the ignored rev list, squash, and merge this PR so we have a starting point on styling. You've done amazing work on this PR and I think it's important to merge.

Concerning the workaround of ./gradlew test iT, I don't see how that's equivalent to running the build task. Build will run other tasks such as process resources & execute our gradle plugins because of the functional test apps. One of the reasons we left gradle plugins applied in the test apps was to ensure their logic is executed, but those tasks won't work if the styling isn't perfect since they aren't a part of an integration run. Styling enforcement then could act as a delay when developing locally.

Concerning turning on enforcement, I'm against this implementation because it will interfere during periods of rapid, local development. We also need to be able to easily identify when a PR fails for a style issue vs logic issue. Having a disable option allows both development styles & it could support a separate workflow to point out styling issues. While integrating prevents that flexibility so I don't see a downside to supporting both vs always enforcing. If someone prefers to enforce always, they can leave it enabled.

I also think we need an autoformat option - like spotless to minimize the styling rules impacts for new contributors & to reduce errors. Do we have a solution for that?

matrei added 2 commits August 26, 2025 07:48
The code style enforcement will be added in another
commit that is not added to `.git-blame-ignore-revs`.
@matrei matrei changed the title Enforce Code Style Reformat code Aug 26, 2025
@matrei
Copy link
Contributor Author

matrei commented Aug 26, 2025

OK, so if I understand you correctly, you are suggesting:

./gradlew build = no code style checks
./gradlew check = no code style checks

Perhaps a ./gradlew codeStyle that only runs checkstyle* and codenarc* tasks?

A separate GitHub workflow for Code Style checks (that uploads the reports to its summary page).

@matrei matrei merged commit 20c3278 into apache:7.0.x Aug 26, 2025
33 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in Apache Grails Aug 26, 2025
@jdaugherty
Copy link
Contributor

OK, so if I understand you correctly, you are suggesting:

./gradlew build = no code style checks

./gradlew check = no code style checks

Perhaps a ./gradlew codeStyle that only runs checkstyle* and codenarc* tasks?

A separate GitHub workflow for Code Style checks (that uploads the reports to its summary page).

I was thinking just a property that enables/disables it. By default, it would run always and then we could toggle locally when needed or in an action as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Code Standards, Reformat and Verification

3 participants