diff --git a/buildengine/build.go b/buildengine/build.go index 43ec874cab..0705f23302 100644 --- a/buildengine/build.go +++ b/buildengine/build.go @@ -34,8 +34,8 @@ func buildModule(ctx context.Context, sch *schema.Schema, module Module, filesTr logger := log.FromContext(ctx).Scope(module.Module) ctx = log.ContextWithLogger(ctx, logger) - // clear stale module errors before extracting schema - if err := os.RemoveAll(filepath.Join(module.AbsDeployDir(), module.Errors)); err != nil { + // clear the deploy directory before extracting schema + if err := os.RemoveAll(module.AbsDeployDir()); err != nil { return fmt.Errorf("failed to clear errors: %w", err) } diff --git a/buildengine/build_go_test.go b/buildengine/build_go_test.go index eda40c6967..f3c4a58f0b 100644 --- a/buildengine/build_go_test.go +++ b/buildengine/build_go_test.go @@ -155,6 +155,21 @@ func Nothing(context.Context) error { }) } +func TestGoBuildClearsBuildDir(t *testing.T) { + sch := &schema.Schema{ + Modules: []*schema.Module{ + schema.Builtins(), + {Name: "test"}, + }, + } + bctx := buildContext{ + moduleDir: "testdata/projects/another", + buildDir: "_ftl", + sch: sch, + } + testBuildClearsBuildDir(t, bctx) +} + func TestMetadataImportsExcluded(t *testing.T) { sch := &schema.Schema{ Modules: []*schema.Module{ diff --git a/buildengine/build_kotlin_test.go b/buildengine/build_kotlin_test.go index f84aa01967..83217582fb 100644 --- a/buildengine/build_kotlin_test.go +++ b/buildengine/build_kotlin_test.go @@ -32,6 +32,21 @@ package ftl.test }) } +func TestKotlinBuildClearsBuildDir(t *testing.T) { + sch := &schema.Schema{ + Modules: []*schema.Module{ + schema.Builtins(), + {Name: "test"}, + }, + } + bctx := buildContext{ + moduleDir: "testdata/projects/echokotlin", + buildDir: "target", + sch: sch, + } + testBuildClearsBuildDir(t, bctx) +} + func TestGenerateAllTypes(t *testing.T) { if testing.Short() { t.SkipNow() diff --git a/buildengine/build_test.go b/buildengine/build_test.go index a417f61365..40655d9614 100644 --- a/buildengine/build_test.go +++ b/buildengine/build_test.go @@ -64,6 +64,35 @@ func testBuild( assert.NoError(t, err, "Error removing build directory") } +func testBuildClearsBuildDir(t *testing.T, bctx buildContext) { + t.Helper() + ctx := log.ContextWithLogger(context.Background(), log.Configure(os.Stderr, log.Config{})) + abs, err := filepath.Abs(bctx.moduleDir) + assert.NoError(t, err, "Error getting absolute path for module directory") + + // build to generate the build directory + module, err := LoadModule(abs) + assert.NoError(t, err) + err = Build(ctx, bctx.sch, module, &mockModifyFilesTransaction{}) + assert.NoError(t, err) + + // create a temporary file in the build directory + buildDir := filepath.Join(bctx.moduleDir, bctx.buildDir) + tempFile, err := os.Create(filepath.Join(buildDir, "test-clear-build.tmp")) + assert.NoError(t, err, "Error creating temporary file in module directory") + tempFile.Close() + + // build to clear the old build directory + module, err = LoadModule(abs) + assert.NoError(t, err) + err = Build(ctx, bctx.sch, module, &mockModifyFilesTransaction{}) + assert.NoError(t, err) + + // ensure the temporary file was removed + _, err = os.Stat(filepath.Join(buildDir, "test-clear-build.tmp")) + assert.Error(t, err, "Build directory was not removed") +} + func assertGeneratedModule(generatedModulePath string, expectedContent string) assertion { return func(t testing.TB, bctx buildContext) error { t.Helper()