diff --git a/codegen/config/config.go b/codegen/config/config.go index 72525401f96..1638239d003 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -185,9 +185,35 @@ func (c *Config) Check() error { } } + // check packages names against conflict, if present in the same dir + // and check filenames for uniqueness + packageConfigList := []PackageConfig{ + c.Model, + c.Exec, + c.Resolver, + } + filesMap := make(map[string]bool) + pkgConfigsByDir := make(map[string]PackageConfig) + for _, current := range packageConfigList { + _, fileFound := filesMap[current.Filename] + if fileFound { + return fmt.Errorf("filename %s defined more than once", current.Filename) + } + filesMap[current.Filename] = true + previous, inSameDir := pkgConfigsByDir[current.Dir()] + if inSameDir && current.Package != previous.Package { + return fmt.Errorf("filenames %s and %s are in the same directory but have different package definitions", stripPath(current.Filename), stripPath(previous.Filename)) + } + pkgConfigsByDir[current.Dir()] = current + } + return c.normalize() } +func stripPath(path string) string { + return filepath.Base(path) +} + type TypeMap map[string]TypeMapEntry func (tm TypeMap) Exists(typeName string) bool { diff --git a/codegen/config/config_test.go b/codegen/config/config_test.go index 3f6ee668ac7..d4f94f9c973 100644 --- a/codegen/config/config_test.go +++ b/codegen/config/config_test.go @@ -78,3 +78,16 @@ func TestReferencedPackages(t *testing.T) { }) } + +func TestConfigCheck(t *testing.T) { + t.Run("invalid config format due to conflicting package names", func(t *testing.T) { + config, err := LoadConfig("testdata/cfg/conflictedPackages.yml") + require.NoError(t, err) + + err = config.normalize() + require.NoError(t, err) + + err = config.check() + require.EqualError(t, err, "filenames exec.go and models.go are in the same directory but have different package definitions") + }) +} diff --git a/codegen/testdata/cfg/conflictedPackages.yml b/codegen/testdata/cfg/conflictedPackages.yml new file mode 100644 index 00000000000..ec6ceea515a --- /dev/null +++ b/codegen/testdata/cfg/conflictedPackages.yml @@ -0,0 +1,10 @@ +schema: + - schema.graphql +exec: + filename: generated/exec.go + package: graphql +model: + filename: generated/models.go +resolver: + filename: generated/resolver.go + type: Resolver diff --git a/plugin/modelgen/testdata/gqlgen.yml b/plugin/modelgen/testdata/gqlgen.yml index b32289be0d3..9c01c79b685 100644 --- a/plugin/modelgen/testdata/gqlgen.yml +++ b/plugin/modelgen/testdata/gqlgen.yml @@ -2,7 +2,7 @@ schema: - "testdata/schema.graphql" exec: - filename: out/generated.go + filename: out/ignored.go model: filename: out/generated.go