-
-
Notifications
You must be signed in to change notification settings - Fork 969
Reformat code #14925
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
Reformat code #14925
Conversation
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
Add the Checkstyle `AvoidStarImport` and Codenarc `NoWildcardImports` rules. https://checkstyle.sourceforge.io/checks/imports/avoidstarimport.html https://codenarc.org/codenarc-rules-imports.html#nowildcardimports-rule
Add the Checkstyle `RedunantImport` and Codenarc `DuplicateImport` and `ImportFromSamePackage` rules. https://checkstyle.sourceforge.io/checks/imports/redundantimport.html https://codenarc.org/codenarc-rules-imports.html#duplicateimport-rule https://codenarc.org/codenarc-rules-imports.html#importfromsamepackage-rule
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
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.
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
# 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
|
I resolved the merge conflicts after #15003 |
jamesfredley
left a comment
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.
@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?
jdaugherty
left a comment
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.
This appears to be an excellent, gradual approach to code style enforcement. Great Job @matrei!
Some concerns:
-
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?
-
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.
-
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?
|
I think we should actually remove the Checkstyle and CodeNarc files from this PR and only squash the code formatting changes. |
|
I am ok with that, but do you agree with separating into a separate action so we can fix issues in parallel? |
|
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 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. |
But shouldn't one preferably run the build locally to test changes before pushing?
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. |
Isn't that why there are separate |
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 |
|
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 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? |
The code style enforcement will be added in another commit that is not added to `.git-blame-ignore-revs`.
|
OK, so if I understand you correctly, you are suggesting:
Perhaps a 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. |
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