From da927ad5fe9b1d910d59b1df79b62cc4e7c5996d Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Fri, 26 Dec 2025 21:00:02 +0100 Subject: [PATCH] feat(graph): add Root() helper method Add Root() method to TaskfileGraph to get the root vertex (entrypoint Taskfile). This will be used for lazy variable resolution. Note: Tasks already have Location.Taskfile which can be used to find their source Taskfile in the graph, so GetVertexByNamespace is not needed. --- taskfile/ast/graph.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/taskfile/ast/graph.go b/taskfile/ast/graph.go index cb30093d..b6c27438 100644 --- a/taskfile/ast/graph.go +++ b/taskfile/ast/graph.go @@ -45,6 +45,18 @@ func (tfg *TaskfileGraph) Visualize(filename string) error { return draw.DOT(tfg.Graph, f) } +// Root returns the root vertex of the graph (the entrypoint Taskfile). +func (tfg *TaskfileGraph) Root() (*TaskfileVertex, error) { + hashes, err := graph.TopologicalSort(tfg.Graph) + if err != nil { + return nil, err + } + if len(hashes) == 0 { + return nil, fmt.Errorf("task: graph has no vertices") + } + return tfg.Vertex(hashes[0]) +} + func (tfg *TaskfileGraph) Merge() (*Taskfile, error) { hashes, err := graph.TopologicalSort(tfg.Graph) if err != nil {