spring-io / spring-javaformat

Apache License 2.0
795 stars 110 forks source link

Issue with format task and accents (gradle and intellij) #382

Closed gleidson closed 1 year ago

gleidson commented 1 year ago

We are starting a new project and will use spring boot as a base for this project. As it will be a project based on spring boot, we are considering following the spring code style and using the springformat plugin as a formatting tool.

During the evaluation process, we created a project to do some tests and found a situation where the format executes, however, when we check the formatting, it indicates that exists formatting violations.

gradlew format

11:15:28: Executing 'format'...

Starting Gradle Daemon...
Gradle Daemon started in 1 s 77 ms
> Task :formatMain UP-TO-DATE
> Task :formatTest UP-TO-DATE
> Task :format UP-TO-DATE

BUILD SUCCESSFUL in 4s
2 actionable tasks: 2 up-to-date
11:15:33: Execution finished 'format'.

gradlew check

Execution failed for task ':checkFormatMain'.
> Formatting violations found in the following files:
   * src\main\java\com\example\demo\Response.java

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

I don't know, but is there a log of formatting violations that we could enable?

If we change the accents in the text below (Response.java), the problem stops happening.

image

We have another class (DemoApplication) with accents and doesn't have formatting issues:

<img width="535" alt="image" src="https://github.com/spring-io/spring-javaformat/assets/26634/69c23ecd-e698-40cf-9eb6-f5ef337b034a">

Attached the demo project

demo.zip

Other information gralle: 8.1.1 java: 17 (corretto-17 version 17.0.7) Windows 11 Intellij 2023.1.2

wilkinsona commented 1 year ago

Thanks for the sample. Unfortunately, I can't reproduce the problem you've described although I've only tried on macOS. I suspect it's due to encoding and macOS's UTF-8 default preventing the problem from occurring. I see you've set systemProp.file.encoding=utf-8 in gradle.properties but this may not be sufficient to effect Charset.defaultCharset() that's called in the task:

https://github.com/spring-io/spring-javaformat/blob/7e97f323d96758d58ed57701c36cd2c69f9fc0fe/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/tasks/FormatterTask.java#L105

Please try configuring the encoding explicitly:

import io.spring.javaformat.gradle.tasks.FormatterTask

// …

tasks.withType<FormatterTask> {
  encoding = "UTF-8"
}
gleidson commented 1 year ago

Thanks, @wilkinsona!

It worked by setting the encoding explicitly, like your suggestion!

wilkinsona commented 1 year ago

Thanks for giving it a try. Given that you're already configuring the JavaCompile tasks to use UTF-8, I think it's reasonable to have to do the same for the formatter tasks as well.