From e2af2dbbf2045aac3da1c8db3ecfbf77b4cd5aa2 Mon Sep 17 00:00:00 2001 From: milldr Date: Tue, 24 Dec 2024 11:55:55 -0500 Subject: [PATCH] Handle relative paths in StackImport.Path and string imports --- .../orgs/cp/tenant1/test2/_defaults.yaml | 2 +- .../{global-region.yaml => us-east-2.yaml} | 2 +- internal/exec/stack_processor_utils.go | 18 +++++++++++++----- pkg/stack/stack_processor_test.go | 4 ++-- 4 files changed, 17 insertions(+), 9 deletions(-) rename examples/tests/stacks/orgs/cp/tenant1/test2/{global-region.yaml => us-east-2.yaml} (82%) diff --git a/examples/tests/stacks/orgs/cp/tenant1/test2/_defaults.yaml b/examples/tests/stacks/orgs/cp/tenant1/test2/_defaults.yaml index ed81c842c..d32ce9f16 100644 --- a/examples/tests/stacks/orgs/cp/tenant1/test2/_defaults.yaml +++ b/examples/tests/stacks/orgs/cp/tenant1/test2/_defaults.yaml @@ -2,4 +2,4 @@ import: - mixins/stage/test2 - - ./../_defaults # validate relative paths + - ../_defaults # validate relative paths diff --git a/examples/tests/stacks/orgs/cp/tenant1/test2/global-region.yaml b/examples/tests/stacks/orgs/cp/tenant1/test2/us-east-2.yaml similarity index 82% rename from examples/tests/stacks/orgs/cp/tenant1/test2/global-region.yaml rename to examples/tests/stacks/orgs/cp/tenant1/test2/us-east-2.yaml index 8b45cc563..8c42471da 100644 --- a/examples/tests/stacks/orgs/cp/tenant1/test2/global-region.yaml +++ b/examples/tests/stacks/orgs/cp/tenant1/test2/us-east-2.yaml @@ -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 diff --git a/internal/exec/stack_processor_utils.go b/internal/exec/stack_processor_utils.go index baffdd56a..f07ec5b61 100644 --- a/internal/exec/stack_processor_utils.go +++ b/internal/exec/stack_processor_utils.go @@ -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 } @@ -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}) diff --git a/pkg/stack/stack_processor_test.go b/pkg/stack/stack_processor_test.go index 9dc2af898..ed06a6818 100644 --- a/pkg/stack/stack_processor_test.go +++ b/pkg/stack/stack_processor_test.go @@ -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 @@ -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)