Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,11 @@ func (vm *VM) evaluateSnippet(diagnosticFileName ast.DiagnosticFileName, filenam
return output, nil
}

func getAbsPath(path string) (string, error) {
func getAbsPath(path string, canonicalPaths bool) (string, error) {
var absPath string

var err error

if filepath.IsAbs(path) {
absPath = path
} else {
Expand All @@ -248,14 +251,18 @@ func getAbsPath(path string) (string, error) {
}
absPath = strings.Join([]string{wd, path}, string(filepath.Separator))
}
cleanedAbsPath, err := filepath.EvalSymlinks(absPath)
if err != nil {
return "", err

if canonicalPaths {
absPath, err = filepath.EvalSymlinks(absPath)
if err != nil {
return "", err
}
}
return cleanedAbsPath, nil

return absPath, nil
}

func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map[string]struct{}, stackTrace *[]TraceFrame) (err error) {
func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map[string]struct{}, stackTrace *[]TraceFrame, canonicalPaths bool) (err error) {
var cleanedAbsPath string
switch i := (*node).(type) {
case *ast.Import:
Expand All @@ -266,7 +273,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
}
cleanedAbsPath = foundAt
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
cleanedAbsPath, err = getAbsPath(foundAt)
cleanedAbsPath, err = getAbsPath(foundAt, canonicalPaths)
if err != nil {
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
return err
Expand All @@ -277,7 +284,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
return nil
}
dependencies[cleanedAbsPath] = struct{}{}
err = vm.findDependencies(foundAt, &node, dependencies, stackTrace)
err = vm.findDependencies(foundAt, &node, dependencies, stackTrace, canonicalPaths)
if err != nil {
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
return err
Expand All @@ -290,7 +297,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
}
cleanedAbsPath = foundAt
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
cleanedAbsPath, err = getAbsPath(foundAt)
cleanedAbsPath, err = getAbsPath(foundAt, canonicalPaths)
if err != nil {
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
return err
Expand All @@ -305,7 +312,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
}
cleanedAbsPath = foundAt
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
cleanedAbsPath, err = getAbsPath(foundAt)
cleanedAbsPath, err = getAbsPath(foundAt, canonicalPaths)
if err != nil {
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
return err
Expand All @@ -314,7 +321,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
dependencies[cleanedAbsPath] = struct{}{}
default:
for _, node := range parser.Children(i) {
err = vm.findDependencies(filePath, &node, dependencies, stackTrace)
err = vm.findDependencies(filePath, &node, dependencies, stackTrace, canonicalPaths)
if err != nil {
return err
}
Expand Down Expand Up @@ -455,10 +462,28 @@ func (vm *VM) EvaluateFileMulti(filename string) (files map[string]string, forma
return output, nil
}

type findDepsConfig struct {
canonicalPaths bool
}

type FindDepsOption func(c *findDepsConfig)

func WithCanonicalPaths(canonicalize bool) FindDepsOption {
return func(c *findDepsConfig) { c.canonicalPaths = canonicalize }
}

// FindDependencies returns a sorted array of unique transitive dependencies (via import/importstr/importbin)
// from all the given `importedPaths` which are themselves excluded from the returned array.
// The `importedPaths` are parsed as if they were imported from a Jsonnet file located at `importedFrom`.
func (vm *VM) FindDependencies(importedFrom string, importedPaths []string) ([]string, error) {
func (vm *VM) FindDependencies(importedFrom string, importedPaths []string, opts ...FindDepsOption) ([]string, error) {
config := findDepsConfig{
canonicalPaths: true,
}

for _, f := range opts {
f(&config)
}

var nodes []*ast.Node
var stackTrace []TraceFrame
filePaths := make([]string, len(importedPaths))
Expand All @@ -472,7 +497,7 @@ func (vm *VM) FindDependencies(importedFrom string, importedPaths []string) ([]s
}
cleanedAbsPath := foundAt
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
cleanedAbsPath, err = getAbsPath(foundAt)
cleanedAbsPath, err = getAbsPath(foundAt, config.canonicalPaths)
if err != nil {
return nil, err
}
Expand All @@ -487,7 +512,7 @@ func (vm *VM) FindDependencies(importedFrom string, importedPaths []string) ([]s
}

for i, filePath := range filePaths {
err := vm.findDependencies(filePath, nodes[i], deps, &stackTrace)
err := vm.findDependencies(filePath, nodes[i], deps, &stackTrace, config.canonicalPaths)
if err != nil {
err = makeRuntimeError(err.Error(), stackTrace)
return nil, errors.New(vm.ErrorFormatter.Format(err))
Expand Down