Skip to content

Commit

Permalink
fix: don't attempt to create ftl-project.toml in fs root (#1771)
Browse files Browse the repository at this point in the history
Fixes #1770
  • Loading branch information
safeer authored Jun 13, 2024
1 parent dba835b commit 249ce0e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
19 changes: 16 additions & 3 deletions buildengine/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func TestWatch(t *testing.T) {
waitForEvents(t, events, []WatchEvent{})

// Initiate two modules
err := ftl("init", "go", dir, "one")
err := gitInit(dir)
assert.NoError(t, err)
err = ftl("init", "go", dir, "one")
assert.NoError(t, err)
err = ftl("init", "go", dir, "two")
assert.NoError(t, err)
Expand Down Expand Up @@ -70,7 +72,9 @@ func TestWatchWithBuildModifyingFiles(t *testing.T) {
w := NewWatcher()

// Initiate a module
err := ftl("init", "go", dir, "one")
err := gitInit(dir)
assert.NoError(t, err)
err = ftl("init", "go", dir, "one")
assert.NoError(t, err)

events, topic := startWatching(ctx, t, w, dir)
Expand Down Expand Up @@ -103,7 +107,9 @@ func TestWatchWithBuildAndUserModifyingFiles(t *testing.T) {
dir := t.TempDir()

// Initiate a module
err := ftl("init", "go", dir, "one")
err := gitInit(dir)
assert.NoError(t, err)
err = ftl("init", "go", dir, "one")
assert.NoError(t, err)

one := loadModule(t, dir, "one")
Expand Down Expand Up @@ -204,6 +210,13 @@ func keyForEvent(event WatchEvent) string {
}
}

func gitInit(dir string) error {
cmd := exec.Command("git", "init", dir)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func ftl(args ...string) error {
cmd := exec.Command("ftl", args...)
cmd.Stdout = os.Stdout
Expand Down
4 changes: 4 additions & 0 deletions cmd/ftl/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 in a git repository, run 'git init' at the root of your project", 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 {
Expand Down
12 changes: 7 additions & 5 deletions common/projectconfig/projectconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -90,9 +95,6 @@ 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"))
}
Expand Down
1 change: 1 addition & 0 deletions go-runtime/ftl/ftl_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

func TestLifecycle(t *testing.T) {
in.Run(t, "",
in.GitInit(),
in.Exec("ftl", "init", "go", ".", "echo"),
in.Deploy("echo"),
in.Call("echo", "echo", in.Obj{"name": "Bob"}, func(t testing.TB, response in.Obj) {
Expand Down

0 comments on commit 249ce0e

Please sign in to comment.