@@ -44,24 +44,29 @@ func TransitiveImports(dir string) ([]string, error) {
44
44
return nil , errors .Wrap (err , "creating Jsonnet AST" )
45
45
}
46
46
47
- imports := make ([] string , 0 )
48
- if err = importRecursive (& imports , vm , node , "main.jsonnet" ); err != nil {
47
+ imports := make (map [ string ] bool )
48
+ if err = importRecursive (imports , vm , node , "main.jsonnet" ); err != nil {
49
49
return nil , err
50
50
}
51
51
52
- uniq := append ( uniqueStringSlice (imports ), mainFile )
53
- for i := range uniq {
54
- uniq [ i ], _ = filepath . Rel ( rootDir , uniq [ i ] )
52
+ paths := make ([] string , 0 , len (imports )+ 1 )
53
+ for k := range imports {
54
+ paths = append ( paths , k )
55
55
}
56
- sort . Strings ( uniq )
56
+ paths = append ( paths , mainFile )
57
57
58
- return uniq , nil
58
+ for i := range paths {
59
+ paths [i ], _ = filepath .Rel (rootDir , paths [i ])
60
+ }
61
+ sort .Strings (paths )
62
+
63
+ return paths , nil
59
64
}
60
65
61
66
// importRecursive takes a Jsonnet VM and recursively imports the AST. Every
62
67
// found import is added to the `list` string slice, which will ultimately
63
68
// contain all recursive imports
64
- func importRecursive (list * [] string , vm * jsonnet.VM , node ast.Node , currentPath string ) error {
69
+ func importRecursive (list map [ string ] bool , vm * jsonnet.VM , node ast.Node , currentPath string ) error {
65
70
switch node := node .(type ) {
66
71
// we have an `import`
67
72
case * ast.Import :
@@ -73,7 +78,11 @@ func importRecursive(list *[]string, vm *jsonnet.VM, node ast.Node, currentPath
73
78
}
74
79
75
80
abs , _ := filepath .Abs (foundAt )
76
- * list = append (* list , abs )
81
+ if list [abs ] {
82
+ return nil
83
+ }
84
+
85
+ list [abs ] = true
77
86
78
87
if err := importRecursive (list , vm , contents , foundAt ); err != nil {
79
88
return err
@@ -89,7 +98,11 @@ func importRecursive(list *[]string, vm *jsonnet.VM, node ast.Node, currentPath
89
98
}
90
99
91
100
abs , _ := filepath .Abs (foundAt )
92
- * list = append (* list , abs )
101
+ if list [abs ] {
102
+ return nil
103
+ }
104
+
105
+ list [abs ] = true
93
106
94
107
// neither `import` nor `importstr`, probably object or similar: try children
95
108
default :
0 commit comments