Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change FindDependencies to optionally follow symlinks #730

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cmd/jsonnet-deps/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func main() {
}
}

dependencies, err := vm.FindDependencies("", conf.inputFiles)
dependencies, err := vm.FindDependencies("", conf.inputFiles, true)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
Expand Down
35 changes: 21 additions & 14 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,11 @@ func (vm *VM) evaluateSnippet(diagnosticFileName ast.DiagnosticFileName, filenam
return output, nil
}

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

var err error

if filepath.IsAbs(path) {
absPath = path
} else {
Expand All @@ -243,14 +246,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 followSymlinks {
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, followSymlinks bool) (err error) {
var cleanedAbsPath string
switch i := (*node).(type) {
case *ast.Import:
Expand All @@ -261,7 +268,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, followSymlinks)
if err != nil {
*stackTrace = append([]traceFrame{{Loc: *i.Loc()}}, *stackTrace...)
return err
Expand All @@ -272,7 +279,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, followSymlinks)
if err != nil {
*stackTrace = append([]traceFrame{{Loc: *i.Loc()}}, *stackTrace...)
return err
Expand All @@ -285,7 +292,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, followSymlinks)
if err != nil {
*stackTrace = append([]traceFrame{{Loc: *i.Loc()}}, *stackTrace...)
return err
Expand All @@ -300,7 +307,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, followSymlinks)
if err != nil {
*stackTrace = append([]traceFrame{{Loc: *i.Loc()}}, *stackTrace...)
return err
Expand All @@ -309,7 +316,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, followSymlinks)
if err != nil {
return err
}
Expand Down Expand Up @@ -453,7 +460,7 @@ func (vm *VM) EvaluateFileMulti(filename string) (files map[string]string, forma
// 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, followSymlinks bool) ([]string, error) {
var nodes []*ast.Node
var stackTrace []traceFrame
filePaths := make([]string, len(importedPaths))
Expand All @@ -467,7 +474,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, followSymlinks)
if err != nil {
return nil, err
}
Expand All @@ -482,7 +489,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, followSymlinks)
if err != nil {
err = makeRuntimeError(err.Error(), stackTrace)
return nil, errors.New(vm.ErrorFormatter.Format(err))
Expand Down