Skip to content

Commit 110b4ca

Browse files
committed
Change FindDependencies to optionally follow symlinks
1 parent fed90cd commit 110b4ca

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
@@ -232,8 +232,11 @@ func (vm *VM) evaluateSnippet(diagnosticFileName ast.DiagnosticFileName, filenam
232232
return output, nil
233233
}
234234

235-
func getAbsPath(path string) (string, error) {
235+
func getAbsPath(path string, followSymlinks bool) (string, error) {
236236
var absPath string
237+
238+
var err error
239+
237240
if filepath.IsAbs(path) {
238241
absPath = path
239242
} else {
@@ -243,14 +246,18 @@ func getAbsPath(path string) (string, error) {
243246
}
244247
absPath = strings.Join([]string{wd, path}, string(filepath.Separator))
245248
}
246-
cleanedAbsPath, err := filepath.EvalSymlinks(absPath)
247-
if err != nil {
248-
return "", err
249+
250+
if followSymlinks {
251+
absPath, err = filepath.EvalSymlinks(absPath)
252+
if err != nil {
253+
return "", err
254+
}
249255
}
250-
return cleanedAbsPath, nil
256+
257+
return absPath, nil
251258
}
252259

253-
func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map[string]struct{}, stackTrace *[]traceFrame) (err error) {
260+
func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map[string]struct{}, stackTrace *[]traceFrame, followSymlinks bool) (err error) {
254261
var cleanedAbsPath string
255262
switch i := (*node).(type) {
256263
case *ast.Import:
@@ -261,7 +268,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
261268
}
262269
cleanedAbsPath = foundAt
263270
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
264-
cleanedAbsPath, err = getAbsPath(foundAt)
271+
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
265272
if err != nil {
266273
*stackTrace = append([]traceFrame{{Loc: *i.Loc()}}, *stackTrace...)
267274
return err
@@ -272,7 +279,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
272279
return nil
273280
}
274281
dependencies[cleanedAbsPath] = struct{}{}
275-
err = vm.findDependencies(foundAt, &node, dependencies, stackTrace)
282+
err = vm.findDependencies(foundAt, &node, dependencies, stackTrace, followSymlinks)
276283
if err != nil {
277284
*stackTrace = append([]traceFrame{{Loc: *i.Loc()}}, *stackTrace...)
278285
return err
@@ -285,7 +292,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
285292
}
286293
cleanedAbsPath = foundAt
287294
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
288-
cleanedAbsPath, err = getAbsPath(foundAt)
295+
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
289296
if err != nil {
290297
*stackTrace = append([]traceFrame{{Loc: *i.Loc()}}, *stackTrace...)
291298
return err
@@ -300,7 +307,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
300307
}
301308
cleanedAbsPath = foundAt
302309
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
303-
cleanedAbsPath, err = getAbsPath(foundAt)
310+
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
304311
if err != nil {
305312
*stackTrace = append([]traceFrame{{Loc: *i.Loc()}}, *stackTrace...)
306313
return err
@@ -309,7 +316,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
309316
dependencies[cleanedAbsPath] = struct{}{}
310317
default:
311318
for _, node := range parser.Children(i) {
312-
err = vm.findDependencies(filePath, &node, dependencies, stackTrace)
319+
err = vm.findDependencies(filePath, &node, dependencies, stackTrace, followSymlinks)
313320
if err != nil {
314321
return err
315322
}
@@ -453,7 +460,7 @@ func (vm *VM) EvaluateFileMulti(filename string) (files map[string]string, forma
453460
// FindDependencies returns a sorted array of unique transitive dependencies (via import/importstr/importbin)
454461
// from all the given `importedPaths` which are themselves excluded from the returned array.
455462
// The `importedPaths` are parsed as if they were imported from a Jsonnet file located at `importedFrom`.
456-
func (vm *VM) FindDependencies(importedFrom string, importedPaths []string) ([]string, error) {
463+
func (vm *VM) FindDependencies(importedFrom string, importedPaths []string, followSymlinks bool) ([]string, error) {
457464
var nodes []*ast.Node
458465
var stackTrace []traceFrame
459466
filePaths := make([]string, len(importedPaths))
@@ -467,7 +474,7 @@ func (vm *VM) FindDependencies(importedFrom string, importedPaths []string) ([]s
467474
}
468475
cleanedAbsPath := foundAt
469476
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
470-
cleanedAbsPath, err = getAbsPath(foundAt)
477+
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
471478
if err != nil {
472479
return nil, err
473480
}
@@ -482,7 +489,7 @@ func (vm *VM) FindDependencies(importedFrom string, importedPaths []string) ([]s
482489
}
483490

484491
for i, filePath := range filePaths {
485-
err := vm.findDependencies(filePath, nodes[i], deps, &stackTrace)
492+
err := vm.findDependencies(filePath, nodes[i], deps, &stackTrace, followSymlinks)
486493
if err != nil {
487494
err = makeRuntimeError(err.Error(), stackTrace)
488495
return nil, errors.New(vm.ErrorFormatter.Format(err))

0 commit comments

Comments
 (0)