diff --git a/cmd/ftl/cmd_init.go b/cmd/ftl/cmd_init.go index 557368335b..bdefc53f7f 100644 --- a/cmd/ftl/cmd_init.go +++ b/cmd/ftl/cmd_init.go @@ -63,6 +63,10 @@ func (i initGoCmd) Run(ctx context.Context, parent *initCmd) error { return fmt.Errorf("module name %q is invalid", i.Name) } + if _, ok := internal.GitRoot(i.Dir).Get(); !ok { + return fmt.Errorf("directory %s is not a git repository", i.Dir) + } + logger := log.FromContext(ctx) logger.Debugf("Initializing FTL Go module %s in %s", i.Name, i.Dir) if err := scaffold(parent.Hermit, goruntime.Files(), i.Dir, i, scaffolder.Exclude("^go.mod$")); err != nil { @@ -71,7 +75,7 @@ func (i initGoCmd) Run(ctx context.Context, parent *initCmd) error { if err := updateGitIgnore(i.Dir); err != nil { return err } - if err := projectconfig.MaybeCreateDefault(ctx, i.Dir); err != nil { + if err := projectconfig.MaybeCreateDefault(ctx); err != nil { return err } logger.Debugf("Running go mod tidy") diff --git a/common/projectconfig/projectconfig.go b/common/projectconfig/projectconfig.go index e389c94772..68ad7a66eb 100644 --- a/common/projectconfig/projectconfig.go +++ b/common/projectconfig/projectconfig.go @@ -12,6 +12,7 @@ import ( "github.com/alecthomas/types/optional" "github.com/TBD54566975/ftl" + "github.com/TBD54566975/ftl/internal" "github.com/TBD54566975/ftl/internal/log" ) @@ -79,8 +80,12 @@ func DefaultConfigPath() optional.Option[string] { if err != nil { return optional.None[string]() } - // Find the first ftl-project.toml file in the parent directories. - for { + // Find the first ftl-project.toml file in the parent directories, up until the gitroot. + root, ok := internal.GitRoot(dir).Get() + if !ok { + root = "/" + } + for dir != root && dir != "." { path := filepath.Join(dir, "ftl-project.toml") _, err := os.Stat(path) if err == nil { @@ -90,16 +95,13 @@ func DefaultConfigPath() optional.Option[string] { return optional.None[string]() } dir = filepath.Dir(dir) - if dir == "/" || dir == "." { - break - } } return optional.Some(filepath.Join(dir, "ftl-project.toml")) } -// MaybeCreateDefault creates the ftl-project.toml file in the given dir if it -// does not already exist in any parent directory. -func MaybeCreateDefault(ctx context.Context, dir string) error { +// MaybeCreateDefault creates the ftl-project.toml file in the Git root if it +// does not already exist. +func MaybeCreateDefault(ctx context.Context) error { logger := log.FromContext(ctx) path, ok := DefaultConfigPath().Get() if !ok { @@ -113,13 +115,6 @@ func MaybeCreateDefault(ctx context.Context, dir string) error { if !errors.Is(err, os.ErrNotExist) { return err } - - // No project file exists, create one in the given dir. - absDir, err := filepath.Abs(dir) - if err != nil { - return err - } - path = filepath.Join(absDir, "ftl-project.toml") logger.Debugf("Creating a new project config file at %q", path) return Save(Config{Path: path}) }