Skip to content

gci error format go file with windows line separator #212

Open
@yougg

Description

@yougg
gci -v
#gci version 0.13.4

you can reproduce with following steps

first we have one go file number.go with Windows line separator: CRLF

package main

import (
	"math/rand"
	"fmt"
)

func main() {
	fmt.Println("Number0:", rand.Intn(10))
	fmt.Println("Number1:", rand.Intn(10))
	fmt.Println("Number2:", rand.Intn(10))
}

usually the go source file with Windows line separator CRLF came from git checkout with setting git config --global core.autocrlf true

call cat -Ev to show the line endings

> cat -Ev number.go 
package main^M$
^M$
import (^M$
    "math/rand"^M$
    "fmt"^M$
)^M$
^M$
func main() {^M$
    fmt.Println("Number0:", rand.Intn(10))^M$
    fmt.Println("Number1:", rand.Intn(10))^M$
    fmt.Println("Number2:", rand.Intn(10))^M$
}^M$

try format this go file with gci, it will replaces all of CR to LF after import package lines.

gci diff number.go 
--- number.go
+++ number.go
@@ -1,12 +1,16 @@
-package main
-
-import (
-       "math/rand"
-       "fmt"
-)
-
-func main() {
-       fmt.Println("Number0:", rand.Intn(10))
-       fmt.Println("Number1:", rand.Intn(10))
-       fmt.Println("Number2:", rand.Intn(10))
-}
+package main
+
+import (
+       "fmt"
+       "math/rand"
+)
+
+func main() {
+
+       fmt.Println("Number0:", rand.Intn(10))
+
+       fmt.Println("Number1:", rand.Intn(10))
+
+       fmt.Println("Number2:", rand.Intn(10))
+
+}

except correct format result:

--- number.go
+++ number.go
@@ -1,8 +1,8 @@
 package main

 import (
-       "math/rand"
        "fmt"
+       "math/rand"
 )

 func main() {

maybe this PR #170 can help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions