From c0251d31d27142b569bd4a16ced6073bbcda6150 Mon Sep 17 00:00:00 2001 From: Nikhil Benesch Date: Mon, 12 Feb 2018 19:48:13 -0500 Subject: [PATCH] imports: handle arbitrarily long lines Previous work to resolve golang/go#18201 increased the maximum line length that goimports could handle to 1MiB (CL83800), but generated code can result in Go files with longer lines. Use a bufio.Reader instead of a bufio.Scanner to support arbitrarily long lines, as permitted by the Go spec. Change-Id: If719e531859804304d60a8c00db6304ab3d5fe5e Reviewed-on: https://go-review.googlesource.com/93439 Reviewed-by: Brad Fitzpatrick --- imports/fix_test.go | 27 --------------------------- imports/imports.go | 20 ++++++++++---------- 2 files changed, 10 insertions(+), 37 deletions(-) diff --git a/imports/fix_test.go b/imports/fix_test.go index 30985528..bb5fcb9e 100644 --- a/imports/fix_test.go +++ b/imports/fix_test.go @@ -5,7 +5,6 @@ package imports import ( - "bufio" "bytes" "flag" "go/build" @@ -2033,29 +2032,3 @@ const x = mypkg.Sprintf("%s", "my package") t.Errorf("Process returned unexpected result.\ngot:\n%v\nwant:\n%v", got, want) } } - -// Ensures a token that is larger that -// https://golang.org/issues/18201 -func TestProcessTokenTooLarge(t *testing.T) { - const largeSize = maxScanTokenSize + 1 - largeString := strings.Repeat("x", largeSize) - - in := `package testimports - -import ( - "fmt" - "mydomain.mystuff/mypkg" -) - -const s = fmt.Sprintf("%s", "` + largeString + `") -const x = mypkg.Sprintf("%s", "my package") - -// end -` - - _, err := Process("foo", []byte(in), nil) - - if err != bufio.ErrTooLong { - t.Errorf("Process did not returned expected error.\n got:\n%v\nwant:\n%v", err, bufio.ErrTooLong) - } -} diff --git a/imports/imports.go b/imports/imports.go index d789bb5b..bd67f91d 100644 --- a/imports/imports.go +++ b/imports/imports.go @@ -259,18 +259,18 @@ func matchSpace(orig []byte, src []byte) []byte { var impLine = regexp.MustCompile(`^\s+(?:[\w\.]+\s+)?"(.+)"`) -// Used to set Scanner buffer size so that large tokens can be handled. -// see https://github.com/golang/go/issues/18201 -const maxScanTokenSize = bufio.MaxScanTokenSize * 16 - func addImportSpaces(r io.Reader, breaks []string) ([]byte, error) { var out bytes.Buffer - sc := bufio.NewScanner(r) - sc.Buffer(nil, maxScanTokenSize) + in := bufio.NewReader(r) inImports := false done := false - for sc.Scan() { - s := sc.Text() + for { + s, err := in.ReadString('\n') + if err == io.EOF { + break + } else if err != nil { + return nil, err + } if !inImports && !done && strings.HasPrefix(s, "import") { inImports = true @@ -291,7 +291,7 @@ func addImportSpaces(r io.Reader, breaks []string) ([]byte, error) { } } - fmt.Fprintln(&out, s) + fmt.Fprint(&out, s) } - return out.Bytes(), sc.Err() + return out.Bytes(), nil }