Skip to content

Commit bcc427f

Browse files
committed
Change FindDependencies to optionally follow symlinks
1 parent 10aef6a commit bcc427f

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

cmd/jsonnet-deps/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func main() {
193193
}
194194
}
195195

196-
dependencies, err := vm.FindDependencies("", conf.inputFiles)
196+
dependencies, err := vm.FindDependencies("", conf.inputFiles, true)
197197
if err != nil {
198198
fmt.Fprintln(os.Stderr, err.Error())
199199
os.Exit(1)

vm.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,11 @@ func (vm *VM) evaluateSnippet(diagnosticFileName ast.DiagnosticFileName, filenam
237237
return output, nil
238238
}
239239

240-
func getAbsPath(path string) (string, error) {
240+
func getAbsPath(path string, followSymlinks bool) (string, error) {
241241
var absPath string
242+
243+
var err error
244+
242245
if filepath.IsAbs(path) {
243246
absPath = path
244247
} else {
@@ -248,14 +251,18 @@ func getAbsPath(path string) (string, error) {
248251
}
249252
absPath = strings.Join([]string{wd, path}, string(filepath.Separator))
250253
}
251-
cleanedAbsPath, err := filepath.EvalSymlinks(absPath)
252-
if err != nil {
253-
return "", err
254+
255+
if followSymlinks {
256+
absPath, err = filepath.EvalSymlinks(absPath)
257+
if err != nil {
258+
return "", err
259+
}
254260
}
255-
return cleanedAbsPath, nil
261+
262+
return absPath, nil
256263
}
257264

258-
func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map[string]struct{}, stackTrace *[]TraceFrame) (err error) {
265+
func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map[string]struct{}, stackTrace *[]TraceFrame, followSymlinks bool) (err error) {
259266
var cleanedAbsPath string
260267
switch i := (*node).(type) {
261268
case *ast.Import:
@@ -266,7 +273,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
266273
}
267274
cleanedAbsPath = foundAt
268275
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
269-
cleanedAbsPath, err = getAbsPath(foundAt)
276+
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
270277
if err != nil {
271278
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
272279
return err
@@ -277,7 +284,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
277284
return nil
278285
}
279286
dependencies[cleanedAbsPath] = struct{}{}
280-
err = vm.findDependencies(foundAt, &node, dependencies, stackTrace)
287+
err = vm.findDependencies(foundAt, &node, dependencies, stackTrace, followSymlinks)
281288
if err != nil {
282289
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
283290
return err
@@ -290,7 +297,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
290297
}
291298
cleanedAbsPath = foundAt
292299
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
293-
cleanedAbsPath, err = getAbsPath(foundAt)
300+
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
294301
if err != nil {
295302
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
296303
return err
@@ -305,7 +312,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
305312
}
306313
cleanedAbsPath = foundAt
307314
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
308-
cleanedAbsPath, err = getAbsPath(foundAt)
315+
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
309316
if err != nil {
310317
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
311318
return err
@@ -314,7 +321,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
314321
dependencies[cleanedAbsPath] = struct{}{}
315322
default:
316323
for _, node := range parser.Children(i) {
317-
err = vm.findDependencies(filePath, &node, dependencies, stackTrace)
324+
err = vm.findDependencies(filePath, &node, dependencies, stackTrace, followSymlinks)
318325
if err != nil {
319326
return err
320327
}
@@ -458,7 +465,7 @@ func (vm *VM) EvaluateFileMulti(filename string) (files map[string]string, forma
458465
// FindDependencies returns a sorted array of unique transitive dependencies (via import/importstr/importbin)
459466
// from all the given `importedPaths` which are themselves excluded from the returned array.
460467
// The `importedPaths` are parsed as if they were imported from a Jsonnet file located at `importedFrom`.
461-
func (vm *VM) FindDependencies(importedFrom string, importedPaths []string) ([]string, error) {
468+
func (vm *VM) FindDependencies(importedFrom string, importedPaths []string, followSymlinks bool) ([]string, error) {
462469
var nodes []*ast.Node
463470
var stackTrace []TraceFrame
464471
filePaths := make([]string, len(importedPaths))
@@ -472,7 +479,7 @@ func (vm *VM) FindDependencies(importedFrom string, importedPaths []string) ([]s
472479
}
473480
cleanedAbsPath := foundAt
474481
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
475-
cleanedAbsPath, err = getAbsPath(foundAt)
482+
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
476483
if err != nil {
477484
return nil, err
478485
}
@@ -487,7 +494,7 @@ func (vm *VM) FindDependencies(importedFrom string, importedPaths []string) ([]s
487494
}
488495

489496
for i, filePath := range filePaths {
490-
err := vm.findDependencies(filePath, nodes[i], deps, &stackTrace)
497+
err := vm.findDependencies(filePath, nodes[i], deps, &stackTrace, followSymlinks)
491498
if err != nil {
492499
err = makeRuntimeError(err.Error(), stackTrace)
493500
return nil, errors.New(vm.ErrorFormatter.Format(err))

0 commit comments

Comments
 (0)