Skip to content
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

CRLF build file converted to LF on sorting #106

Open
nebulon42 opened this issue Aug 2, 2024 · 1 comment · May be fixed by #114
Open

CRLF build file converted to LF on sorting #106

nebulon42 opened this issue Aug 2, 2024 · 1 comment · May be fixed by #114
Labels
bug Something isn't working

Comments

@nebulon42
Copy link

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
Copy link
Author

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.

@nebulon42 nebulon42 linked a pull request Sep 17, 2024 that will close this issue
@autonomousapps autonomousapps added the bug Something isn't working label Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants