square / gradle-dependencies-sorter

A CLI app and Gradle plugin to sort the dependencies in your Gradle build scripts
Apache License 2.0
257 stars 12 forks source link

CRLF build file converted to LF on sorting #106

Open nebulon42 opened 3 months ago

nebulon42 commented 3 months ago

I have a build file with CRLF line endings and am using the autocrlf setting with Git. When running the sorter via the Gradle plugin (sortDependencies) then the dependencies are sorted but the file is then converted to LF and the dependencies block which looked like this before

dependencies {
    api(project(":modules:domains:authorization:shared-types"))
    api(project(":modules:libraries:common:types"))
    implementation(project(":modules:libraries:serialization"))
    implementation(project(":modules:libraries:util:core"))
    api(project(":modules:framework:metainfos:api"))

    implementation("com.fasterxml.jackson.core:jackson-annotations")
    implementation("com.fasterxml.jackson.core:jackson-databind")
    implementation("jakarta.validation:jakarta.validation-api")

    compileOnly(libs.checker.qual)

    testFixturesImplementation(project(":modules:libraries:util:core"))
    testFixturesImplementation("org.apache.commons:commons-collections4")
}

looks like this afterwards

dependencies {

    api(project(":modules:domains:authorization:shared-types"))

    api(project(":modules:framework:metainfos:api"))

    api(project(":modules:libraries:common:types"))

    implementation(project(":modules:libraries:serialization"))

    implementation(project(":modules:libraries:util:core"))

    implementation("com.fasterxml.jackson.core:jackson-annotations")

    implementation("com.fasterxml.jackson.core:jackson-databind")

    implementation("jakarta.validation:jakarta.validation-api")

    testFixturesImplementation(project(":modules:libraries:util:core"))

    testFixturesImplementation("org.apache.commons:commons-collections4")

    compileOnly(libs.checker.qual)
}

So a lot of extra whitespace is introdued. Version is 0.7.

Is there a way to keep the file line endings as they were before and avoid the extra whitespace with sorting?

nebulon42 commented 2 months ago

Maybe the issue is not detected in the tests because of the use of trimmedLinesOf which seems to strip the whitespace away.

A quick fix would be to transform the newline chars that are added by the StringBuilder to the system newline characters and additionally trim away trailing carriage returns (if present) from the tokenization.

Index: sort/src/main/kotlin/com/squareup/sort/kotlin/KotlinSorter.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/sort/src/main/kotlin/com/squareup/sort/kotlin/KotlinSorter.kt b/sort/src/main/kotlin/com/squareup/sort/kotlin/KotlinSorter.kt
--- a/sort/src/main/kotlin/com/squareup/sort/kotlin/KotlinSorter.kt (revision 7496c0a0281bcc61aac8c84db2c80de982efbe20)
+++ b/sort/src/main/kotlin/com/squareup/sort/kotlin/KotlinSorter.kt (date 1723210091951)
@@ -87,7 +87,7 @@

   override fun exitNamedBlock(ctx: NamedBlockContext) {
     if (ctx.isDependencies) {
-      rewriter.replace(ctx.start, ctx.stop, dependenciesBlock())
+      rewriter.replace(ctx.start, ctx.stop, dependenciesBlock().replace("\n", System.lineSeparator()))

       // Whenever we exit a dependencies block, clear this map. Each block will be treated separately.
       mutableDependencies.clear()
@@ -148,10 +148,10 @@
             newOrder += declaration

             // Write preceding comments if there are any
-            if (texts.comment != null) appendLine(texts.comment)
+            if (texts.comment != null) appendLine(texts.comment.replace("\r", ""))

             append(indent.repeat(level))
-            appendLine(texts.declarationText)
+            appendLine(texts.declarationText.replace("\r", ""))
           }
       }

I have tried this out in a snapshot JAR and it fixes the problem for me (in Kotlin syntax, do not have Groovy available). It feels a bit ugly that is why I didn't open a PR for it yet. Since I lack the deeper knowledge about the library I'm unsure if there is a better solution.