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

Find Importers: Add missing break in relative paths logic #785

Merged
merged 3 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 12 additions & 13 deletions pkg/jsonnet/find_importers.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,23 @@ func findImporters(root string, searchForFile string, chain map[string]struct{})
continue
}

// Remove any `./` or `../` that can be removed just by looking at the given path
// ex: `./foo/bar.jsonnet` -> `foo/bar.jsonnet` or `/foo/../bar.jsonnet` -> `/bar.jsonnet`
importPath = filepath.Clean(importPath)

// Match on relative imports with ..
// Jsonnet also matches all intermediary paths for some reason, so we look at them too
doubleDotCount := strings.Count(importPath, "..")
if doubleDotCount > 0 {
importPath = strings.ReplaceAll(importPath, "../", "")
for i := 0; i <= doubleDotCount; i++ {
dir := filepath.Dir(jsonnetFilePath)
for j := 0; j < i; j++ {
dir = filepath.Dir(dir)
}
testImportPath := filepath.Join(dir, importPath)
isImporter = pathMatches(searchForFile, testImportPath)
}
// Jsonnet also matches relative imports that are one level deeper than they should be
// Example: Given two envs (env1 and env2), the two following imports in `env1/main.jsonnet will work`: `../env2/main.jsonnet` and `../../env2/main.jsonnet`
// This can lead to false positives, but ruling them out would require much more complex logic
if strings.HasPrefix(importPath, "..") {
shallowImport := filepath.Clean(filepath.Join(filepath.Dir(jsonnetFilePath), strings.Replace(importPath, "../", "", 1)))
importPath = filepath.Clean(filepath.Join(filepath.Dir(jsonnetFilePath), importPath))

isImporter = pathMatches(searchForFile, importPath) || pathMatches(searchForFile, shallowImport)
}

// Match on imports to lib/ or vendor/
if !isImporter {
importPath = strings.ReplaceAll(importPath, "./", "")
isImporter = pathMatches(searchForFile, filepath.Join(root, "vendor", importPath)) || pathMatches(searchForFile, filepath.Join(root, "lib", importPath))
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/jsonnet/find_importers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ func TestFindImportersForFiles(t *testing.T) {
absPath(t, "testdata/findImporters/environments/imports-symlinked-vendor/main.jsonnet"),
},
},
{
name: "relative imported environment",
files: []string{"testdata/findImporters/environments/relative-imported/main.jsonnet"},
expectedImporters: []string{
absPath(t, "testdata/findImporters/environments/relative-import/main.jsonnet"),
absPath(t, "testdata/findImporters/environments/relative-imported/main.jsonnet"), // itself, it's a main file
},
},
{
name: "relative imported environment with doubled '..'",
files: []string{"testdata/findImporters/environments/relative-imported2/main.jsonnet"},
expectedImporters: []string{
absPath(t, "testdata/findImporters/environments/relative-import/main.jsonnet"),
absPath(t, "testdata/findImporters/environments/relative-imported2/main.jsonnet"), // itself, it's a main file
},
},
}

for _, c := range cases {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
// jsonnet supports going one level lower than files really are
first: import '../relative-imported/main.jsonnet',
second: import '../../relative-imported2/main.jsonnet',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}