diff --git a/codegen/templates/import.go b/codegen/templates/import.go index 17bd96ab2eb..00a82ea5ef1 100644 --- a/codegen/templates/import.go +++ b/codegen/templates/import.go @@ -105,7 +105,7 @@ func (s *Imports) Lookup(path string) string { for s.findByAlias(alias) != nil { alias = imp.Name + strconv.Itoa(i) i++ - if i > 10 { + if i > 1000 { panic(fmt.Errorf("too many collisions, last attempt was %s", alias)) } } diff --git a/codegen/templates/import_test.go b/codegen/templates/import_test.go index 2e8dd5a89aa..a91d2a8ccd3 100644 --- a/codegen/templates/import_test.go +++ b/codegen/templates/import_test.go @@ -1,6 +1,7 @@ package templates import ( + "fmt" "go/types" "os" "testing" @@ -45,6 +46,19 @@ func TestImports(t *testing.T) { }) }) + t.Run("duplicates above 10 are decollisioned", func(t *testing.T) { + a := Imports{destDir: wd, packages: &code.Packages{}} + for i := 0; i < 100; i++ { + cBar := fmt.Sprintf("github.com/99designs/gqlgen/codegen/templates/testdata/%d/bar", i) + if i > 0 { + require.Equal(t, fmt.Sprintf("bar%d", i), a.Lookup(cBar)) + } else { + require.Equal(t, "bar", a.Lookup(cBar)) + + } + } + }) + t.Run("package name defined in code will be used", func(t *testing.T) { a := Imports{destDir: wd, packages: &code.Packages{}}