diff --git a/cmd/gen.go b/cmd/gen.go index 3842f02b37d..97bd6342f1e 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -46,11 +46,6 @@ var genCmd = cli.Command{ config.SchemaStr[filename] = string(schemaRaw) } - if err = config.Check(); err != nil { - fmt.Fprintln(os.Stderr, "invalid config format: "+err.Error()) - os.Exit(1) - } - err = codegen.Generate(*config) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) diff --git a/cmd/init.go b/cmd/init.go index 1e7c18b9327..6ad7e58c811 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -78,11 +78,6 @@ func GenerateGraphServer(config *codegen.Config, serverFilename string) { config.SchemaStr[filename] = string(schemaRaw) } - if err := config.Check(); err != nil { - fmt.Fprintln(os.Stderr, "invalid config format: "+err.Error()) - os.Exit(1) - } - if err := codegen.Generate(*config); err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) diff --git a/codegen/codegen.go b/codegen/codegen.go index 773e3db7cc4..8aadd48cd2d 100644 --- a/codegen/codegen.go +++ b/codegen/codegen.go @@ -1,6 +1,7 @@ package codegen import ( + "fmt" "log" "os" "path/filepath" @@ -19,6 +20,9 @@ func Generate(cfg Config) error { return err } + if err := cfg.check(); err != nil { + return fmt.Errorf("invalid config format: " + err.Error()) + } _ = syscall.Unlink(cfg.Exec.Filename) _ = syscall.Unlink(cfg.Model.Filename) diff --git a/codegen/config.go b/codegen/config.go index f9df24fb3f3..9826ca5dfe5 100644 --- a/codegen/config.go +++ b/codegen/config.go @@ -178,7 +178,7 @@ func (c *PackageConfig) IsDefined() bool { return c.Filename != "" } -func (cfg *Config) Check() error { +func (cfg *Config) check() error { if err := cfg.Models.Check(); err != nil { return errors.Wrap(err, "config.models") } @@ -191,6 +191,29 @@ func (cfg *Config) Check() error { if err := cfg.Resolver.Check(); err != nil { return errors.Wrap(err, "config.resolver") } + + // check packages names against conflict, if present in the same dir + // and check filenames for uniqueness + packageConfigList := []PackageConfig{ + cfg.Model, + cfg.Exec, + cfg.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 nil } @@ -271,3 +294,7 @@ func findCfgInDir(dir string) string { } return "" } + +func stripPath(path string) string { + return filepath.Base(path) +} diff --git a/codegen/config_test.go b/codegen/config_test.go index a97b9d20f56..09a20abf54d 100644 --- a/codegen/config_test.go +++ b/codegen/config_test.go @@ -79,3 +79,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