diff --git a/generate/generate.go b/generate/generate.go index 1e4c36aac9..f7f697564a 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -194,7 +194,7 @@ func Do( stackReport := doStackGeneration(root, tree, vendorDir, vendorRequests) rootReport := doRootGeneration(root, tree) report := mergeReports(stackReport, rootReport) - return cleanupOrphaned(root, report) + return cleanupOrphaned(root, tree, report) } func doStackGeneration( @@ -1339,21 +1339,21 @@ func loadStackCodeCfgs( return genfilesConfigs, nil } -func cleanupOrphaned(root *config.Root, report Report) Report { +func cleanupOrphaned(root *config.Root, target *config.Tree, report Report) Report { logger := log.With(). Str("action", "generate.cleanupOrphaned()"). + Stringer("dir", target.Dir()). Logger() - // If the root of the tree is a stack then there is nothing to do - // since there can't be any orphans (the root parent stack owns - // the entire project). - if root.Tree().IsStack() { - logger.Debug().Msg("project root is a stack, nothing to do") + + // If the target tree is a stack then there is nothing to do + // as it was already generated at this point. + if target.IsStack() { return report } logger.Debug().Msg("listing orphaned generated files") - orphanedGenFiles, err := ListStackGenFiles(root, root.HostDir()) + orphanedGenFiles, err := ListStackGenFiles(root, target.HostDir()) if err != nil { report.CleanupErr = err return report @@ -1363,7 +1363,7 @@ func cleanupOrphaned(root *config.Root, report Report) Report { deleteFailures := map[project.Path]*errors.List{} for _, genfile := range orphanedGenFiles { - genfileAbspath := filepath.Join(root.HostDir(), genfile) + genfileAbspath := filepath.Join(target.HostDir(), genfile) dir := project.NewPath("/" + filepath.ToSlash(filepath.Dir(genfile))) if err := os.Remove(genfileAbspath); err != nil { if deleteFailures[dir] == nil { diff --git a/generate/generate_test.go b/generate/generate_test.go index 6fd43b3e90..2975b3c317 100644 --- a/generate/generate_test.go +++ b/generate/generate_test.go @@ -799,6 +799,97 @@ func TestGenerateCleanup(t *testing.T) { }, }, }, + { + name: "workdir is respected in cleanup", + layout: []string{ + "s:stacks/stack-1", + "s:stacks/stack-2", + "s:stacks/stack-1/stack-1-a", + "s:stacks/stack-1/stack-1-b", + "s:stacks/stack-3", // only files here will be cleaned up. + genfile("dir/orphan.hcl"), + genfile("stacks/stack-1/a.hcl"), + genfile("stacks/stack-1/subdir/b.hcl"), + genfile("stacks/stack-1/subdir/dir/c.hcl"), + genfile("stacks/stack-1/stack-1-a/e.hcl"), + genfile("stacks/stack-1/stack-1-a/subdir/f.hcl"), + genfile("stacks/stack-1/stack-1-a/subdir/dir/g.hcl"), + genfile("stacks/stack-1/stack-1-b/h.hcl"), + genfile("stacks/stack-2/d.hcl"), + genfile("stacks/stack-3/x.hcl"), + genfile("stacks/stack-3/subdir/y.hcl"), + genfile("stacks/stack-3/subdir/dir/z.hcl"), + }, + fromdir: "/stacks/stack-3", + wantReport: generate.Report{ + Successes: []generate.Result{ + { + Dir: project.NewPath("/stacks/stack-3"), + Deleted: []string{ + "x.hcl", + "subdir/y.hcl", + "subdir/dir/z.hcl", + }, + }, + }, + }, + want: []generatedFile{ + { + dir: "/stacks/stack-1", + files: map[string]fmt.Stringer{ + "a.hcl": Doc(), + }, + }, + { + dir: "/stacks/stack-1/stack-1-a", + files: map[string]fmt.Stringer{ + "e.hcl": Doc(), + }, + }, + { + dir: "/stacks/stack-1/stack-1-a/subdir/dir", + files: map[string]fmt.Stringer{ + "g.hcl": Doc(), + }, + }, + { + dir: "/stacks/stack-1/stack-1-a/subdir", + files: map[string]fmt.Stringer{ + "f.hcl": Doc(), + }, + }, + { + dir: "/stacks/stack-1/stack-1-b", + files: map[string]fmt.Stringer{ + "h.hcl": Doc(), + }, + }, + { + dir: "/stacks/stack-1/subdir", + files: map[string]fmt.Stringer{ + "b.hcl": Doc(), + }, + }, + { + dir: "/stacks/stack-1/subdir/dir", + files: map[string]fmt.Stringer{ + "c.hcl": Doc(), + }, + }, + { + dir: "/stacks/stack-2", + files: map[string]fmt.Stringer{ + "d.hcl": Doc(), + }, + }, + { + dir: "/dir", + files: map[string]fmt.Stringer{ + "orphan.hcl": Doc(), + }, + }, + }, + }, { name: "cleanup ignores dotdirs outside stacks", layout: []string{