1
0
mirror of https://github.com/go-task/task.git synced 2025-11-23 22:24:45 +02:00

fix: matrix loops should be deterministic (#1784)

This commit is contained in:
Pete Davison
2024-09-02 22:43:54 +01:00
committed by GitHub
parent a3bdb6c40a
commit e4b4d04abd
2 changed files with 14 additions and 19 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/fingerprint"
"github.com/go-task/task/v3/internal/omap"
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/taskfile/ast"
)
@@ -272,7 +273,7 @@ func itemsFromFor(
var keys []string // The list of keys to loop over (only if looping over a map)
var values []any // The list of values to loop over
// Get the list from a matrix
if f.Matrix != nil {
if f.Matrix.Len() != 0 {
return asAnySlice(product(f.Matrix)), nil, nil
}
// Get the list from the explicit for list
@@ -328,24 +329,16 @@ func itemsFromFor(
}
// product generates the cartesian product of the input map of slices.
func product(inputMap map[string][]any) []map[string]any {
if len(inputMap) == 0 {
func product(inputMap omap.OrderedMap[string, []any]) []map[string]any {
if inputMap.Len() == 0 {
return nil
}
// Extract the keys and corresponding slices
keys := make([]string, 0, len(inputMap))
slices := make([][]any, 0, len(inputMap))
for key, slice := range inputMap {
keys = append(keys, key)
slices = append(slices, slice)
}
// Start with an empty product result
result := []map[string]any{{}}
// Iterate over each slice in the slices
for i, slice := range slices {
_ = inputMap.Range(func(key string, slice []any) error {
var newResult []map[string]any
// For each combination in the current result
@@ -358,14 +351,15 @@ func product(inputMap map[string][]any) []map[string]any {
newComb[k] = v
}
// Add the current item with the corresponding key
newComb[keys[i]] = item
newComb[key] = item
newResult = append(newResult, newComb)
}
}
// Update result with the new combinations
result = newResult
}
return nil
})
return result
}