From 220bf74a9e689f078a80eb00ff0b5726010197a1 Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Wed, 6 Sep 2023 00:00:36 +0000 Subject: [PATCH] feat: better taskfile cycle error handling --- errors/errors.go | 1 + errors/errors_taskfile.go | 18 ++++++++++++++++++ taskfile/reader.go | 6 ++++++ 3 files changed, 25 insertions(+) diff --git a/errors/errors.go b/errors/errors.go index 46ad328b..400c0f16 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -20,6 +20,7 @@ const ( CodeTaskfileVersionCheckError CodeTaskfileNetworkTimeout CodeTaskfileDuplicateInclude + CodeTaskfileCycle ) // Task related exit codes diff --git a/errors/errors_taskfile.go b/errors/errors_taskfile.go index 2951bf90..ba5579c2 100644 --- a/errors/errors_taskfile.go +++ b/errors/errors_taskfile.go @@ -191,3 +191,21 @@ func (err *TaskfileDuplicateIncludeError) Error() string { func (err *TaskfileDuplicateIncludeError) Code() int { return CodeTaskfileDuplicateInclude } + +// TaskfileCycleError is returned when we detect that a Taskfile includes a +// set of Taskfiles that include each other in a cycle. +type TaskfileCycleError struct { + Source string + Destination string +} + +func (err TaskfileCycleError) Error() string { + return fmt.Sprintf("task: include cycle detected between %s <--> %s", + err.Source, + err.Destination, + ) +} + +func (err TaskfileCycleError) Code() int { + return CodeTaskfileCycle +} diff --git a/taskfile/reader.go b/taskfile/reader.go index 790c991a..707c6562 100644 --- a/taskfile/reader.go +++ b/taskfile/reader.go @@ -153,6 +153,12 @@ func (r *Reader) include(node Node) error { Namespaces: []string{namespace, edge.Properties.Data.(*ast.Include).Namespace}, } } + if errors.Is(err, graph.ErrEdgeCreatesCycle) { + return errors.TaskfileCycleError{ + Source: node.Location(), + Destination: includeNode.Location(), + } + } return err }) return nil