Skip to content

Commit

Permalink
Handle relative paths in StackImport.Path and string imports
Browse files Browse the repository at this point in the history
  • Loading branch information
milldr committed Dec 24, 2024
1 parent 74ab1d8 commit e2af2db
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/tests/stacks/orgs/cp/tenant1/test2/_defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

import:
- mixins/stage/test2
- ./../_defaults # validate relative paths
- ../_defaults # validate relative paths
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

import:
- mixins/region/global-region
- mixins/region/us-east-2
- ./_defaults # validate relative paths
18 changes: 13 additions & 5 deletions internal/exec/stack_processor_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,15 @@ func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.St
var importObj schema.StackImport
err := mapstructure.Decode(imp, &importObj)
if err == nil {
// Handle relative paths in StackImport.Path
if strings.HasPrefix(importObj.Path, "./") || strings.HasPrefix(importObj.Path, "../") {
// Get the directory of the current file
baseDir := filepath.Dir(filePath)
// Join the base directory with the relative path
importObj.Path = filepath.Join(baseDir, importObj.Path)
// Clean the path to resolve any .. segments
importObj.Path = filepath.Clean(importObj.Path)
}
result = append(result, importObj)
continue
}
Expand All @@ -1786,12 +1795,11 @@ func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.St
return nil, fmt.Errorf("invalid empty import in the file '%s'", filePath)
}

// Handle relative paths - only if they explicitly start with "./"
if strings.HasPrefix(s, "./") {
// Get the directory of the current file
// Handle relative paths in string imports
if strings.HasPrefix(s, "./") || strings.HasPrefix(s, "../") {
baseDir := filepath.Dir(filePath)
// Join the base directory with the relative path (removing the "./" prefix)
s = filepath.Join(baseDir, s[2:])
s = filepath.Join(baseDir, s)
s = filepath.Clean(s)
}

result = append(result, schema.StackImport{Path: s})
Expand Down
4 changes: 2 additions & 2 deletions pkg/stack/stack_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestStackProcessor(t *testing.T) {
"../../examples/tests/stacks/orgs/cp/tenant1/prod/us-east-2.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/staging/us-east-2.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/test1/us-east-2.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/test2/global-region.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/test2/us-east-2.yaml",
}

processStackDeps := true
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestStackProcessor(t *testing.T) {
assert.Equal(t, "orgs/cp/tenant1/prod/us-east-2", mapResultKeys[1])
assert.Equal(t, "orgs/cp/tenant1/staging/us-east-2", mapResultKeys[2])
assert.Equal(t, "orgs/cp/tenant1/test1/us-east-2", mapResultKeys[3])
assert.Equal(t, "orgs/cp/tenant1/test2/global-region", mapResultKeys[4])
assert.Equal(t, "orgs/cp/tenant1/test2/us-east-2", mapResultKeys[4])

mapConfig1, err := u.UnmarshalYAML[schema.AtmosSectionMapType](listResult[0])
assert.Nil(t, err)
Expand Down

0 comments on commit e2af2db

Please sign in to comment.