Skip to content

Commit

Permalink
fix!: incorrect filesystem implicit order due to reloading of objects.
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Natel <[email protected]>
  • Loading branch information
i4ki committed Feb 12, 2024
1 parent fd4bd4c commit 47ea934
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
### Fixed

- Fix language server panic when root directory contain errors.
- (**BREAKING CHANGE**) Fix the execution order when using `tag:` filter in `after/before` in conjunction with implicit filesystem order. Please check the `terramate list --run-order` after
upgrading.

## 0.4.5

Expand Down
51 changes: 51 additions & 0 deletions cmd/terramate/e2etests/core/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,57 @@ func TestCLIRunOrder(t *testing.T) {
),
},
},
{
name: "implicit order with tags - Zied case",
layout: []string{
`s:project:tags=["project"];before=["tag:identity"]`,
`s:iac/cloud-storage/bucket:tags=["bucket"];after=["tag:project", "tag:service-account"]`,
`s:iac/service-accounts:tags=["identity"];before=["/iac/service-accounts/sa-name"]`,
`s:iac/service-accounts/sa-name:tags=["service-account"]`,
},
want: RunExpected{
Stdout: nljoin(
"/project",
"/iac/service-accounts",
"/iac/service-accounts/sa-name",
"/iac/cloud-storage/bucket",
),
},
},
{
name: "before clause pulling a branch of fs ordered stacks",
layout: []string{
`s:project:tags=["project"];before=["tag:parent"]`,
`s:dir/parent:tags=["parent"]`,
`s:dir/parent/child:tags=["child"]`,
`s:dir/other:tags=["other"];after=["tag:project", "tag:child"]`,
},
want: RunExpected{
Stdout: nljoin(
`/project`,
`/dir/parent`,
`/dir/parent/child`,
`/dir/other`,
),
},
},
{
name: "stack pulled to the middle of fs ordering chain",
layout: []string{
`s:parent`,
`s:parent/child:before=["tag:other"]`,
`s:parent/child/grand-child`,
`s:other:tags=["other"]`,
},
want: RunExpected{
Stdout: nljoin(
`/parent`,
`/parent/child`,
`/other`,
`/parent/child/grand-child`,
),
},
},
{
name: "grand parent before parent before child (implicit)",
layout: []string{
Expand Down
14 changes: 14 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Tree struct {
// Parent is the parent node or nil if none.
Parent *Tree

stack *Stack

dir string
}

Expand Down Expand Up @@ -308,6 +310,18 @@ func (tree *Tree) IsStack() bool {
return tree.Node.Stack != nil
}

// Stack returns the stack object.
func (tree *Tree) Stack() (*Stack, error) {
if tree.stack == nil {
s, err := LoadStack(tree.Root(), tree.Dir())
if err != nil {
return nil, err
}
tree.stack = s
}
return tree.stack, nil
}

// Stacks returns the stack nodes from the tree.
// The search algorithm is a Deep-First-Search (DFS).
func (tree *Tree) Stacks() List[*Tree] {
Expand Down
17 changes: 7 additions & 10 deletions config/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,12 @@ func validateWatchPaths(rootdir string, stackpath string, paths []string) (proje
}

// StacksFromTrees converts a List[*Tree] into a List[*Stack].
func StacksFromTrees(root string, trees List[*Tree]) (List[*SortableStack], error) {
func StacksFromTrees(trees List[*Tree]) (List[*SortableStack], error) {
var stacks List[*SortableStack]
for _, tree := range trees {
s, err := NewStackFromHCL(root, tree.Node)
s, err := tree.Stack()
if err != nil {
return List[*SortableStack]{}, err
return nil, err
}
stacks = append(stacks, &SortableStack{s})
}
Expand All @@ -302,9 +302,9 @@ func LoadAllStacks(cfg *Tree) (List[*SortableStack], error) {
stacksIDs := map[string]*Stack{}

for _, stackNode := range cfg.Stacks() {
stack, err := NewStackFromHCL(cfg.RootDir(), stackNode.Node)
stack, err := stackNode.Stack()
if err != nil {
return List[*SortableStack]{}, err
return nil, err
}

logger := logger.With().
Expand Down Expand Up @@ -354,11 +354,8 @@ func TryLoadStack(root *Root, cfgdir project.Path) (stack *Stack, found bool, er
return nil, false, nil
}

s, err := NewStackFromHCL(root.HostDir(), tree.Node)
if err != nil {
return nil, true, err
}
return s, true, nil
s, err := tree.Stack()
return s, true, err
}

// ReverseStacks reverses the given stacks slice.
Expand Down
4 changes: 2 additions & 2 deletions run/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ func BuildDAG(
return err
}

ancestorStacks, err := config.StacksFromTrees(root.HostDir(), root.StacksByPaths(s.Dir, ancestorPaths...))
ancestorStacks, err := config.StacksFromTrees(root.StacksByPaths(s.Dir, ancestorPaths...))
if err != nil {
return errors.E(err, "stack %q: failed to load the \"%s\" stacks",
s, ancestorsName)
}
descendantStacks, err := config.StacksFromTrees(root.HostDir(), root.StacksByPaths(s.Dir, descendantPaths...))
descendantStacks, err := config.StacksFromTrees(root.StacksByPaths(s.Dir, descendantPaths...))
if err != nil {
return errors.E(err, "stack %q: failed to load the \"%s\" stacks",
s, descendantsName)
Expand Down

0 comments on commit 47ea934

Please sign in to comment.